HDU1032 模拟

来源:互联网 发布:火箭网络加速器 编辑:程序博客网 时间:2024/05/24 03:37

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1032

  1.      input n

    2.      print n

    3.      if n = 1 then STOP

    4.           if n is偶数 then n= 3n + 1

    5.           else n=- n / 2

    6.      GOTO 2

表示一个循环,输入i,j找出i-j里面循环最大的循环数。注意i可能大于j,很多水题都会设置这个WA。。。

#include<iostream>using namespace std;int fun(int n) {      int count = 0;   while(n!=1)    {        if (n % 2 == 0)            n = n / 2;        else            n = 3 * n + 1;        count++;    }   count++;return count;}int main(){        int a, b;    while (cin >> a >> b)    {        int k,count,temp=0;        int aa = a, bb = b;        if(bb<aa)        {            k = aa;            aa = b;            bb = k;        }        for (int i = aa; i <= bb; i++)        {            count = fun(i);            if (count > temp)                temp = count;        }        cout << a << " " << b << " " << temp << endl;    }    return 0;}



原创粉丝点击