NYOJ 57 6174问题
2024-10-16 16:02:20
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
- 输入
- 第一行输入n,代表有n组测试数据。
接下来n行每行都写一个各位数字互不相同的四位数 - 输出
- 经过多少次上面描述的操作才能出现循环
- 样例输入
-
1
1234 - 样例输出
-
4
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char a[],b[];int ia,ib; int main()
{
int N,count,temp=;
cin>>N;
while(N--)
{
count=;
for(int i=;i<;i++)
cin>>a[i];
loop:
ia=ib=;
sort(a,a+);
for(int i=;i<;i++)
{
ia*=;
ia+=(a[i]-'');
}
for(int i=;i>=;i--)
{
ib*=;
ib+=(a[i]-'');
}
ib=ib-ia;
count++;
if(temp!=ib)
{
temp=ib;
for(int i=;i<;i++)
{
a[i]=(ib%+'');
ib/=;
}
goto loop;
}
else
{
cout<<count<<endl;
}
} }#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
int k;
cin>>k;
while(k--)
{
int n,a[],n1,n2;
scanf("%d",&n);
int s=;
while(n!=)
{
a[]=n%;
a[]=n/;
a[]=n/%;
a[]=n/%;
sort(a,a+);
n1=*a[]+*a[]+*a[]+a[];
n2=*a[]+*a[]+*a[]+a[];
n=n1-n2;
s++;
}
printf("%d\n",s);
}
}
最新文章
- Eclipse代码和xml文件的智能提示
- OSG中的HUD
- [算法导论]红黑树实现(插入和删除) @ Python
- 基于h5的图片无刷新上传(uploadifive)
- P1119 灾后重建
- [转载]MongoDB 常用命令
- PHP计算中文字符串长度 、截取相应中文字符串
- Python中的日期和时间
- 【shell mysql 导出数据到csv脚本,完美解决乱码转义符等问题】-费元星
- UNIX网络编程——客户/服务器程序设计示范(六)
- Java-正则使用
- linux文件系统启动流程、启动脚本
- javascript与jquery的区别
- __getitem__ __setitem__ __delitem__ 使用
- pip更新到18版本后使用pycharm更新问题:module &#39;pip&#39; has no attribute &#39;main&#39;
- springmvc 动态加载配置文件
- python记录_day22 序列化
- java 中 enum 枚举的使用
- zookeeper应用 - leader选举 锁
- 奇怪的等待事件“enq: ss - contention”