6174问题
来源:互联网 发布:js true false 编辑:程序博客网 时间:2024/06/01 10:24
6174问题
时间限制:1000 ms | 内存限制:65535 KB
难度:2
描述
假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b,然后用a-b替换原来这个数,并且继续操作。例如,从1234出发,依次可以得到4321-1234=3087、8730-378=8352、8532-2358=6174,又回到了它自己!现在要你写一个程序来判断一个四位数经过多少次这样的操作能出现循环,并且求出操作的次数
比如输入1234执行顺序是1234->3087->8352->6174->6174,输出是4
#include <stdio.h>#include <string.h>int get_next(int x){int a,b,n;char s[10];int i,j;char t;//转换成字符串sprintf(s,"%d",x);n = strlen(s);//冒泡算法for(i = 0; i < n; i++)for(j = i + 1; j < n; j++)if(s[i] > s[j]){t = s[i];s[i] = s[j];s[j] = t;}sscanf(s,"%d",&b);//字符串反转for(i = 0; i < n/2; i++){t = s[i];s[i] = s[n - i - 1];s[n - i - 1] = t;}sscanf(s,"%d",&a);return a - b;}int num[60],count;int main(){int found = 0;int i;scanf("%d",&num[0]);printf("%d",num[0]);count = 1;for(;;){//生成并输出下一个数num[count] = get_next(num[count-1]);printf("-> %d",num[count]);//在数组num中寻早新生成的数for(i = 0; i < count; i++)if(num[i] == num[count]){found = 1; break;}//如果找到,则退出循环if(found) break;count++;}printf("\n");return 0;}
0 0
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题。
- Unity3D脚本18:可视化辅助设置类 Gizmos
- 算法基础(五):二叉树(基础)
- tcp的使用
- node.js 和v8的理解
- matlab2
- 6174问题
- 一个网店的数据库设计
- v1.xmlpullparserexception position:START_TAG <html> @1:7 in ...
- android进程优先级&有了Thread为什么还要使用service
- CareerCup Find the representation as a tree with the least height
- 成为好教练
- linux 下的C函数
- 用GetSystemMetrics函数获取系统分辨率
- 数据统计相关的常用mongo语句