51 条题解
-
20
角谷猜想,非常著名的猜想之一,来看看这道题吧~
- 用 while 语句来完成,接下来就是循环内的代码啦
- 如果是奇数:输出 这个数 * 3 + 1 = 这个数 * 3 + 1,并且把 a 的值也重新赋值一下下
- 否则:输出 这个数 / 2 = 这个数 / 2,同样赋值给 a /= 2
- 注意!:每一次输出后都要换行呦!!!!
上代码吧~(AC过!说了多少遍了,相信UP)
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; while (a != 1) { if (a % 2 == 1) { cout << a << "*" << 3 << "+" << 1 << "=" << a * 3 + 1 << endl; a = a * 3 + 1; } else { cout << a << "/" << 2 << "=" << a / 2 << endl; a = a / 2; } } cout << "End"; return 0; }
-
4
赞 -> 看 懂?
#include <iostream> // 你觉得万能头文件这类东西是你该学的吗 using namespace std; int main() { int n; cin >> n; //核心循环 while (n != 1) { if (n % 2 == 1)//是奇数 { cout << n << "*3+1=" << n * 3 + 1 <<endl; n = n*3+1; } else//是偶数 { cout << n << "/2=" << n/2 << endl; n = n/2; } } cout << "End";//结束 return 0; }
已AC,请放心食用
快给瓦点赞!!!!
-
3
递归
#include<bits/stdc++.h> using namespace std; int n; void num(int k); int main(){ scanf("%d",&n); num(n); return 0; } void num(int k){ if(k == 1){ printf("End"); return; } if(k&1){ printf("%d*3+1=%d\n",k,k*3+1); num(k*3+1); return; } printf("%d/2=%d\n",k,k/2); num(k/2); return; }
-
3
-
2
题解
考虑使用 循环解题。当 时,就退出;否则,
- 当 时,根据题面上的格式输出,并让 ;
- 当 时,同样地,根据题面上的格式输出后令 。
最后使用 函数(它可以用来输出一个 C风格字符串并换行),输出 。如
puts("End")
。
在 C++ 的表达式的计算中,它是先计算完表达式的值后再返回的。特别地,对于赋值语句(例如
t=3*n+1
),那么它会先将 的结果计算出来,赋值到 上,然后将 的值作为表达式的返回值。因此我们可以这样简化一下代码。AC代码
#include<bits/stdc++.h> #define up(l,r,i) for(int i=l,END##i=r;i<=END##i;++i) #define dn(r,l,i) for(int i=r,END##i=l;i>=END##i;--i) using namespace std; typedef long long i64; const int INF =2147483647; int n,t; int main(){ scanf("%d",&n); while(n!=1){ if(n%2==1) printf("%d*3+1=%d\n",n,t=n*3+1); else printf("%d/2=%d\n",n,t=n/2); n=t; } puts("End"); return 0; }
-
1
-
1
角谷猜想,非常著名的猜想之一,来看看这道题吧~ 1. 用 while 语句来完成,接下来就是循环内的代码啦 2. 如果是奇数:输出 这个数 \* 3 + 1 = 这个数 \* 3 + 1,并且把 a 的值也重新赋值一下下 3. 否则:输出 这个数 / 2 = 这个数 / 2,同样赋值给 a /= 2 4. 注意!:每一次输出后都要换行呦!!!! ```#include <iostream> using namespace std; int main() { int n; cin >> n; while (n!=1) { if (n%2==1) { cout << n << "*3+1=" << n*3+1; n*=3; n++; } else { cout << n << "/2=" << n/2; n/=2; } cout << endl; } cout << "End"; return 0; } 已AC 快点赞!
-
1
#include <iostream> // 使用"#include"指令导入"iosteeam"库. using namespace std; // 使用标准的命名空间. int main() { int n; // 定义一个正整数"n". cin >> n; // 输入n的值. while (true) // 无限循环. { if (n == 1) // 当n的值为1时输出"End"并结束循环. { cout << "End"; break; } else // 如果n的值不为1,就判断n的值是奇数还是偶数. { if (n % 2 == 1) // 当n的值为奇数时. { cout << n << "*3+1=" << n * 3 + 1 << endl; // 输出n乘3加1及其结果. n = n * 3 + 1; // 将n的值转化为n乘3加1的结果. } else // 如果n的值不为奇数,而是偶数时. { cout << n << "/2=" << n / 2 << endl; // 输出n除以2及其结果. n /= 2; // 将n的值转化为n除以2的结果. } } } return 0; // 返回值为0,程序正常退出; }
-
0
蒟蒻扫盲计划NO.4
(核心)代码:
while法:
while(n!=1){ if(n%2!=0){ n=n*3+1; }else{ n=n/2; } cout<<n<<" "; } cout<<"End"; //编者注:此为核心代码,头文件什么的自己去加!!!忘加了没有AC不怪我!!!
do_while法:
do{ if(n%2!=0){ cout<<n<<"*3+1="<<n*3+1<<endl; n=n*3+1; }else{ cout<<n<<"/2="<<n/2<<endl; n=n/2; } }while(n!=1); cout<<"End"; //编者注:此为核心代码,头文件什么的自己去加!!!忘加了没有AC不怪我!!!
总结:
难点:while和do_while的出口
考点:while和do_while的运用
综合指数:●●
信息
- ID
- 1511
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 2426
- 已通过
- 1140
- 上传者