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 |
哪位大哥帮忙看一下,程序哪有缺陷?总是wrong answer啊!我觉得都是对的啊!#include<stdio.h> #include<string.h> #define M 81 int sign=0; char a[M]; char b[16][M]; struct kk{ char c; int re; }; kk num[M]; void calculate(char s[M]) { int i,j,k,temp; char s1[M],minc; strcpy(s1,s); for(i=0,k=0;i<strlen(s1);i++) { if(s1[i]=='o') continue; num[k].c=s1[i]; num[k].re=1; for(j=i+1;j<strlen(s1);j++) { if ((s1[i]==s1[j])) { num[k].re++; s1[j]='o'; } } k++; } for(i=0;i<k;i++) for(j=i+1;j<k;j++) { if(num[i].c>num[j].c) { temp=num[i].re; minc=num[i].c; num[i].c=num[j].c; num[i].re=num[j].re; num[j].c=minc; num[j].re=temp; } } for(i=0,j=0;i<k;i++) { if(num[i].re>9) { a[j++]=num[i].re/10+'0'; a[j++]=num[i].re%10+'0'; } else a[j++]=num[i].re+'0'; a[j++]=num[i].c; } a[j]='\0'; } int compare(int n) { int i,j,k; for(i=0;i<n;i++) for(j=i+1,k=1;j<n;j++) { if(strcmp(b[i],b[j])) k++; else return k; } return 0; } int cal(char s[M]) { int n=0; strcpy(b[0],s); sign=0; while(n<15) { calculate(b[n]); if(!strcmp(s,a)&&n==0) return 0; else if(!strcmp(b[n],a)) return n; if(compare(n+1)) { sign=1; return compare(n+1); } strcpy(b[++n],a); } return -1; } int main() { char a[M]; int n; char end[3]={'-','1'}; scanf("%s",a); while(strcmp(a,end)) { n=cal(a); switch(n) { case -1:printf("%s can not be classified after 15 iterations\n",a); break; case 0:printf("%s is self-inventorying\n",a); break; default:if(sign==1) printf("%s enters an inventory loop of length %d\n",a,n ); else printf("%s is self-inventorying after %d steps\n",a,n); break; } scanf("%s",a); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator