113383会出问题~
调试中~~~
注要是会有数据溢出现象~~~
调试中~~~
注要是会有数据溢出现象~~~
[此贴子已经被作者于2017-3-12 22:44编辑过]
[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
#include<stdio.h> #include<conio.h> int main() { unsigned int n=0; int i=0; scanf("%u",&n); while (n!=1) { if (n%2==0) n/=2; else n=n*3+1; ++i; printf("%u %d\n",n,i); getch(); } return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 5000000 int a[MAX]={0}; void initlize(); void print(); int Count(unsigned _int64 n); int main() { unsigned _int64 i=0; unsigned _int64 j=0; unsigned _int64 temp=0; unsigned _int64 p=0; unsigned _int64 maxnumber=0; int sum=0; int max=0; initlize(); while(scanf("%I64d%I64d",&i,&j) == 2 && (i != 0 && j != 0)) { if(i > j)//i比j大,交换一下 { temp = i; i = j; j = temp; } sum = 0; max = 0; for(p = i; p <= j; p++) { sum =Count(p); if(sum > max) { max=sum; maxnumber = p; } } printf("Between %I64d and %I64d,", i, j); printf("%I64d generates the longest sequence of %d values.\n",maxnumber,max); } return 0; } void initlize() { int i=0; unsigned _int64 j=0; int count=0; memset(a,0,sizeof(a)); for (i=2;i<MAX;++i) { for (j=i,count=0;j!=1;++count) { if (j<MAX&&a[j]!=0) { a[i]=a[j]+count; break; } if ((j&1)==0) j>>=1; else j=j*3+1; } if (a[i]==0) a[i]=count; } } int Count(unsigned _int64 n) { int i=0; if (n<MAX) return a[n]; while(n>=MAX) { if((n&1)==0) n>>=1; else n = n*3 + 1; i++; } return i+a[n]; }
[此贴子已经被作者于2017-3-13 00:31编辑过]
#include <stdio.h> #define M 1000001 int ss[M] = {0}; int fun(long long n) { if (1 == n || n < M && ss[n]) return ss[n]; int tmp = n % 2 ? fun(n * 3 + 1) : fun(n / 2); if (n < M) ss[n] = tmp + 1; return tmp + 1; } int main() { int l, h, v, s, tmp; for (int i = 1; i < M; ++i) fun(i); while (2 == scanf("%d%d", &l, &h) && l && h) { s = 0, v = l; if (l > h) tmp = l, l = h, h = tmp; for (int i = l; i <= h; ++i) if (s < ss[i]) s = ss[i], v = i; printf("Between %d and %d, %d generates the longest sequence of %d values.\n", l, h, v, s); } return 0; return 0; }
[此贴子已经被作者于2017-3-13 10:11编辑过]
[此贴子已经被作者于2017-3-13 12:52编辑过]