6174问题。
来源:互联网 发布:小鸭淘宝客助手 编辑:程序博客网 时间:2024/05/16 10:48
输入一个n位数,把所有数字从大到小排序得到a,从小到大得到b,然后用a-b替换原来这个数并且继续操作。
比如1234->3087->8352->6174.
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#include <queue>#include <map>#include <stack>#include <list>#include <vector>using namespace std;//#define DEBUGmap< int, int > mp;int find(int x){int a,b,n;char s[10];sprintf(s,"%d",x);n=strlen(s);for (int i=0; i<n; i++)for (int j=i+1; j<n; j++)if (s[i] > s[j]){char t=s[i];s[i]=s[j];s[j]=t;}sscanf(s,"%d",&b);for (int i=0; i<n/2; i++){char t=s[i];s[i]=s[n-i-1];s[n-i-1]=t;}sscanf(s,"%d",&a);return a-b;}int main(){#ifdef DEBUGfreopen("cin.txt", "r", stdin);freopen("cout.txt", "w", stdout);#endifint x;while (~scanf("%d",&x)){printf("%d",x);int f=1;mp.clear();while (1){int k=find(x);if (mp[k]==1) break;else {printf("->%d",k);mp[k]=1;x=k;f++;}}printf("\n%d\n",f);}return 0;}
0 0
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题
- 6174问题。
- 《C专家编程》勘误表
- 第四章计算器程序部分代码,结合练习
- [BFS]HOJ2979Escape from Pyramids
- zoj 3471 Most Powerful (状态压缩dp)
- ORA-27102(转载)
- 6174问题。
- The connection to adb is down, and a severe error has occured.错误解决
- 对excel的导出,使用jxt
- [MacOSX]_[Mac Book电脑的快捷键*长期更新]
- hdu 2594 Simpsons’ Hidden Talents(KMP)
- 上下文区别
- tcp-http
- table对象insertRow deleteRow 使用 实例
- 【译】菜鸟学java web(一)-----导航篇