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

这是我写的最烂的代码,主函数想起一点写一点,还好AC了

Posted by dynamic_study at 2009-07-17 17:40:22 on Problem 1047
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 100
char str[63][3]=
{"0","1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33","34","35","36","37","38","39","40",
"41","42","43","44","45","46","47","48","49","50",
"51","52","53","54","55","56","57","58","59","60","61"
};
void IntAddition(char *augend, char *addend, char *sum)
{
int cAug[MAX] = {0};
int cAdd[MAX] = {0};
int cSum[MAX] = {0}; 
int lenAug = strlen(augend), lenAdd = strlen(addend);
int lenMin = lenAug < lenAdd ? lenAug : lenAdd;
int i;
for(i=0; i<lenAug; i++)
   cAug[i]=augend[lenAug-1-i]-'0';
for (i=0; i<lenAdd; i++)
   cAdd[i]=addend[lenAdd-1-i]-'0';
int carry=0;
int s=0; 

for(i=0;i<lenMin;i++)
{
   s=cAug[i]+cAdd[i]+carry;
   cSum[i]=s%10;
   carry=s/10;
}
while (i<lenAug)
{
   s=cAug[i]+carry;
   cSum[i]=s%10;
   carry=s/10;
   i++;
}
while(i<lenAdd)
{
   s=cAdd[i]+carry;
   cSum[i]=s%10;
   carry=s/10;
   i++;
}
if(carry>0)
   cSum[i++]=carry;
int j;
for(j=0;j<i;j++)
   sum[j]=cSum[i-1-j]+'0';
sum[i]='\0';
}
void IntMultiplication(char *multiplicand, char *multiplier, char *product)
{
int cD[MAX] = {0};
int cR[MAX] = {0};
int cP[MAX] = {0};
char tcP[MAX] = "";
int lenD = strlen(multiplicand), lenR = strlen(multiplier);
int i, j, k;
for (i=0;i<lenD; i++)
   cD[i]=multiplicand[lenD-1-i] - '0';
for (i=0;i<lenR;i++)
   cR[i]=multiplier[lenR-1-i] - '0';

int carry;
int mul=0;
strcpy(product, "0"); 
for (i=0;i<lenR;i++)
{
   carry = 0;
   for (j=0;j<lenD;j++)
   {
    mul=cD[j]*cR[i]+carry;
    cP[j] = mul % 10;
    carry = mul / 10;
   }
   if (carry > 0)
    cP[j++]=carry;
   while(cP[j-1]==0)
    --j;
   for (k=0;k<j;k++)
    tcP[k]=cP[j-1-k]+'0';
   for (j=0;j<i;j++)
    tcP[k++]='0';
   tcP[k]='\0';
  
   IntAddition(product, tcP, product);
} 
}
int main()
{
  char s1[100],temp1[100],s3[100],temp2[100];
  int len,i,flag,len1,len2,loop,biaozhi,k,j;
   while(cin>>s1)
   {
      strcpy(temp2,s1);
      len2=strlen(temp2);
      sort(temp2,temp2+len2);
	  loop=0;
      len=strlen(s1);
      for(i=0;i<len;i++ )
	  {
	    if(s1[i]!='0')
		{
		   loop=1;
		   break;
		}
	  }
             //cout<<len<<endl;
       if(loop==0)
	   {
	      cout<<s1<<" is cyclic"<<endl;
	      continue;
	   }

       flag=0;
       for(i=2;i<=len;i++)
	   {
	       IntMultiplication(s1,str[i], s3);
	       //cout<<s3<<endl;
	       strcpy(temp1,s3);
	       len1=strlen(temp1);
		   biaozhi=0;
		   k=len1;
		  // cout<<len1<<len2<<endl;
		   if(len1>len2)
		   {
			   flag=1;
			   break;
		   }
		 
	       else if(len1<len2)
		   {
			   biaozhi=1;
		      for(j=len1;j<=len2-1;j++)
				  temp1[j]='0';
		   }
		  
		   if(biaozhi==1)
		   {
			   temp1[len2]='\0';
			   k=len2;
		   }
	       sort(temp1,temp1+k);
		  // cout<<temp2<<endl;
		   //cout<<temp1<<endl;
	       if(strcmp(temp1,temp2)!=0)
		   {
		     //cout<<"temp1 "<<temp1<<endl;
		     //cout<<"temp2 "<<temp2<<endl;
		     flag=1;
		     break;
		   }
	       else
		     flag=0;
	   }
       //cout<<flag<<endl;
       if(flag==0)
	       cout<<s1<<" is cyclic"<<endl;
       else
	       cout<<s1<<" is not cyclic"<<endl;
   }
   
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