1086:角谷猜想
1.5编程基础之循环控制 21:角谷猜想
【题目描述】
谓角谷猜想,是指对于任意一个正整数,如果是奇数,则乘3加1,如果是偶数,则除以2,得到的结果再按照上述规则重复处理,最终总能够得到1。如,假定初始整数为5,计算过程分别为16、8、4、2、1。程序要求输入一个整数,将经过处理得到1的过程输出来。
【输入】
一个正整数N(N <= 2,000,000)。
【输出】
从输入整数到1的步骤,每一步为一行,每一部中描述计算过程。最后一行输出"End"。如果输入为1,直接输出"End"。
【输入样例】
5
【输出样例】
5*3+1=16
16/2=8
8/2=4
4/2=2
2/2=1
End
【提示】
no
C++代码一:
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n;
cin>>n;
while(n!=1)
{
if(n%2==1)
{
cout<<n<<"*3+1="<<n*3+1<<endl;
n=n*3+1;
}
else if(n%2==0)
{
cout<<n<<"/2="<<n/2<<endl;
n/=2;
}
}
cout<<"End";
return 0;
}
C++代码二:
#include<iostream>
#include<string>
using namespace std;
int main()
{
long long a,b,c,d;
cin>>a;
while(a!=1)
{
cout<<a;
if(a%2==0){
a/=2;
cout<<"/2="<<a<<endl;
}
else
{
a=a*3+1;
cout<<"*3+1="<<a<<endl;
}
//cout<<endl;
}
cout<<"End";
return 0;
}
python3代码:
n = int(input())
while n != 1:
if n % 2 == 0:
n //= 2
print(str(int(n*2)) + '/2=' + str(int(n)))
else:
n = 3 * n + 1
print(str(int((n-1)/3)) + '*3+1=' + str(int(n)))
print("End")
电子学会 青少年编程等级考试 C语言 1级-10级 学习资料集
电子学会青少年编程等级考试
中国电子学会考评中心
软件编程(C语言)
电子学会 青少年编程等级考试 C语言 1级
电子学会青少年等级考试C语言(二级)
电子学会 全国青少年软件编程等级考试(三级--八级)
电子学会 全国青少年软件编程等级考试标准 十级标准及相关资料
全国青少年软件编程等级考试标准(C语言1级-10级)-20190927.pdf
全国青少年软件编程等级考试标准(C语言1级-10级)-20190927.pdf_电子学会c语言等级考试标准-教育文档类资源-CSDN下载
linux学习(青少年学Linux)
linux学习(青少年学Linux)_dllglvzhenfeng的博客-CSDN博客
NOI2022宣传片
【NOI2022】PV「什么是信息学精神?」
【NOI2022】PV「什么是信息学精神?」_dllglvzhenfeng的博客-CSDN博客