Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求助高手!ole了一天,想不出还有什么没考虑的了(位数考虑了,开头有0也考虑了)

Posted by luckyowen at 2008-12-24 13:52:52 on Problem 1350
拿同学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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator