| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求助高手!ole了一天,想不出还有什么没考虑的了(位数考虑了,开头有0也考虑了)拿同学ac的代码对数据也找不来,无语了。。
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
char *sub(char *p,char *q)
{
char ans[10];
for(int m=3;m>=0;m--)
{
if(p[m]<q[m])
{
ans[m]=p[m]+10-q[m]+'0';
p[m-1]--;
}
else ans[m]=p[m]-q[m]+'0';
}
ans[4]=0;
return ans;
}
int main()
{
char str[10];char stra[10];
while(scanf("%s",stra)&&strcmp(stra,"-1")!=0)
{
int i=0,j=0,k=0;int m1=0;
while(stra[m1]=='0') m1++;//处理开头有0
strcpy(str,stra+m1);
printf("N=%s:\n",str);
if(strlen(str)!=4||(str[0]==str[1]&&str[1]==str[2]&&str[2]==str[3])) cout<<"No!!"<<endl;
else
{
char q[10],p[10];int key=0;
while(strcmp(str,"6174")!=0&&strcmp(str,"0000"))
{
sort(str+j,str+4);
strcpy(q,str);
sort(str+j,str+4,greater<char>());
strcpy(p,str);
k=0;
while(p[k]=='0'&&k<4) k++;
printf("%s",p+k);
strcpy(str,sub(p,q));
i=0;j=0;
while(q[i]=='0'&&i<4) i++;
while(str[j]=='0'&&j<4) j++;
if(j!=4) printf("-%s=%s\n",q+i,str+j);
else printf("-%s=0\n",q+i);
key++;
}
printf("Ok!! %d times\n",key);
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator