E.准备去爬莫干山的徐老爷

E.准备去爬莫干山的徐老爷
== Description ==
在某饼同学中午吃完方形大饼后,下午去找徐老爷爬莫干山,但秃头的徐老爷在实验室的空调屋里沉迷于"角谷猜想"无法自拔,非要做完题后再跟某饼同学爬莫干山,你能帮帮他吗?
所谓角古猜想是指,对于任意一个正整数n,有规则:
1)若n是奇数,那么对n乘3加1;
2)若n是偶数,那么对n除以2。
得到的结果再按照上述规则重复处理,最终总能够得到1。
例如:给定n=5,计算过程分别为16、8、4、2、1。
要求输入一个整数n,将经过处理得到1的过程输出来。
Input
一个正整数N(0 < N ≤ 2,000,000)。
Output
从输入整数到1的步骤,每一步为一行,每一部中描述计算过程。最后一行输出"End"。如果输入为1,直接输出"End"。

Sample Input

5

Sample Output

5*3+1=16
16/2=8
8/2=4
4/2=2
2/2=1
End

思路
循环+判断即可

【源代码】

#include<iostream>
using namespace std;
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<string>
#include<set>
#include<climits>
#include<cmath>
#include<utility>
#include<algorithm>
int main()
{
    long long n,a,b,c=0;
    cin >> n;
    while(n!=1)
    {
        a=n;
        if(n%2==1)
        {
            n=n*3+1;
            cout << a << "*3+1=" <<n <<endl;
        }else {
            n/=2;
            cout << a << "/2=" << n<<endl;

        }
    }cout << "End" <<endl;
    return 0;
}

发布了9 篇原创文章 · 获赞 12 · 访问量 141

猜你喜欢

转载自blog.csdn.net/qq_45949914/article/details/103807397