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 |
代码有点乱 习惯也不好,但是数据都试了,就是不知道为什么WA求反例!#include "stdio.h" #include "String.h" #include "malloc.h" #define nMax 61 int judge(char*str,int i); void mutiply(char * str,int k,char * newstr); int roundJudge(char *a,char *b); void main() { char str[nMax]; int i,j,k,p,q; scanf("%s",str); i=strlen(str); if(judge(str,i)) printf("%s is cyclic",str); else printf("%s is not cyclic",str); } int judge(char*str,int i) { int j; char newstr[nMax]; for(j=2;j<=i;j++) { mutiply(str,j,newstr); if(!roundJudge(str,newstr)) { //printf("%d\n",j); return 0; break; } } return 1; } void mutiply(char * str,int k,char * newstr) { int i,j,p,q,*m; char kk[3]; if (k>=10) { kk[0]=k/10+'0'; kk[1]=k%10+'0'; kk[2]='\0'; } else { kk[0]=k+'0'; kk[1]='\0'; } i=strlen(str); j=strlen(kk); m=(int *)malloc(sizeof(int)*(i+j)); for(p=0;p<(i+j);p++) m[p]=0; for (p=0;p<i;p++) for(q=0;q<j;q++) m[p+q+1]+=(str[p]-'0')*(kk[q]-'0'); for(p=i+j-1;p>=0;p--) if(m[p]>=10) { m[p-1]+=m[p]/10; m[p]=m[p]%10; } p=0; while(m[p]==0) p++; for(q=0;p<i+j;q++,p++) newstr[q]=m[p]+'0'; newstr[q]='\0'; j=strlen(newstr); if(j<i) newstr[i]='\0'; for (p=i-1;p>=0;p--) { if(p>=(i-j)) newstr[p]=newstr[p-i+j]; else newstr[p]=0+'0'; } free(m); } int roundJudge(char *a,char *b) { int ca,cb,i,j,k; char *c,*temp; ca=strlen(a); cb=strlen(b); if(ca!=cb) return 0; else { c=(char*)malloc(sizeof(char)*(2*ca+1)); temp=(char *)malloc(sizeof(char)*(ca+1)); for (i=0;i<cb;i++) { c[i]=b[i]; } for(i=0;i<cb;i++) { c[strlen(b)+i]=b[i]; } c[2*ca]='\0'; for (i=0;i<ca;i++) { for (j=0;j<ca;j++) { temp[j]=c[i+j]; } temp[ca]='\0'; if(strcmp(a,temp)==0) { free(c); free(temp); return 1; break; } } free(c); free(temp); if(i==ca) return 0; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator