现在的位置: 首页 > 综合 > 正文

Uva 100

2018年01月11日 ⁄ 综合 ⁄ 共 499字 ⁄ 字号 评论关闭
                  3n + 1

水题目而已。。注意细节就好

求n的生成序列。从n开始,如果n是偶数,n除以2,如果n是奇数,n乘以3加1,重复这些步骤,直到n == 1 为止。例如n = 22

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

给俩个数i,j(i,j大小要判断),求i,j中最长的序列 长度最长为多少

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX(a,b) a>b?a:b
int main(){
    int a,b;
    while(~scanf("%d%d",&a,&b)){
        int yy = MAX(a,b);
        int xx = a + b - yy;
        int maxx = -1;
        for(int i = xx;i <= yy;i++){
            int s = 1;
            int x = i;
            while(x != 1){
                if(x % 2 == 1 && x != 1){
                    s++;
                    x = 3 * x + 1;
                }
                else if(x % 2 == 0){
                    s++;
                    x /= 2;
                }
            if(x == 1) break;
            }
            maxx = MAX(maxx,s);
        }
        printf("%d %d %d\n",a,b,maxx);
    }
    return 0;
}

抱歉!评论已关闭.