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 |
那位好心的大哥帮小弟看一下,有代码,有自编的测试数组,我实在看不出错那里了,只求能找出错误的数据,thx测试数据: 20 12345+67890 12345 +67890 ------ 80235 324-111 324 -111 ---- 213 325*4405 325 *4405 ----- 1625 0 1300 1300 ------- 1431625 1234*4 1234 *4 ---- 4936 1*12345 1 *12345 ------ 5 4 3 2 1 ------ 12345 1234*20 1234 *20 ---- 0 2468 ----- 24680 0*12345 0 *12345 ------ 0 12345*0 12345 *0 ----- 0 1000*100 1000 *100 ---- 0 0 1000 ------ 100000 19-19 19 -19 --- 0 23-22 23 -22 --- 1 13-0 13 -0 -- 13 23+0 23 +0 -- 23 100000000000000000000+1 100000000000000000000 +1 --------------------- 100000000000000000001 123456789*123456789 123456789 *123456789 ---------- 1111111101 987654312 864197523 740740734 617283945 493827156 370370367 246913578 123456789 ----------------- 15241578750190521 代码: //poj 1396 #include <stdio.h> #include <string.h> #include <memory.h> #define N 1000 void add(char *s1,char *s2,char *result) { int a[N],b[N],c[N],i,tmp,len1,len2,len; len1=strlen(s1); len2=strlen(s2); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(i=0;i<len1;i++) a[i]=s1[len1-1-i]-'0'; for(i=0;i<len2;i++) b[i]=s2[len2-1-i]-'0'; memset(c,0,sizeof(c)); len=len1>len2?len1:len2; for(i=0;i<len;i++) { c[i]+=a[i]+b[i]; tmp=c[i]/10; c[i]%=10; c[i+1]+=tmp; } if(tmp>0) len++; for(i=len-1;i>=0;i--) { result[len-1-i]=c[i]+'0'; //putchar(result[len-1-i]); //putchar('\n'); } result[len]='\0'; //puts(result); //printf("%d",c[i]); // printf("\n"); } void substract(char *s1,char *s2,char *result) { int a[N],b[N],c[N],len1,len2,i,len; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); len1=strlen(s1); len2=strlen(s2); for(i=0;i<len1;i++) a[i]=s1[len1-1-i]-'0'; for(i=0;i<len2;i++) b[i]=s2[len2-1-i]-'0'; for(i=0;i<len1;i++) { c[i]+=a[i]-b[i]; if(c[i]<0) { c[i+1]--; c[i]+=10; } } len=len1-1; while(c[len]==0) len--; //滤去0 if(len==-1) result="0"; else { //printf("%d",len); for(i=len;i>=0;i--) result[len-i]=c[i]+'0'; result[len+1]='\0'; //puts(result); } // puts(result); // putchar('\n'); // printf("%d",c[i]); //printf("\n"); } void multi(char *s1,char *s2,char *result) { int a[N],b[N],c[N],len1,len2,i,j,tmp,len,len3,temp[N],tmplen; //char res[500]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); len1=strlen(s1); len2=strlen(s2); for(i=0;i<len1;i++) a[i+1]=s1[len1-1-i]-'0';//下标从1开始,符合实际的习惯 for(i=0;i<len2;i++) b[i+1]=s2[len2-1-i]-'0'; for(i=1;i<=len1;i++) for(j=1;j<=len2;j++) { c[i+j-1]+=a[i]*b[j]; tmp=c[i+j-1]/10; c[i+j-1]%=10; c[i+j]+=tmp; } len=len1+len2-1; if(tmp>0) len++; for(i=len;i>=1;i--) result[len-i]=c[i]+'0'; result[len]='\0'; len3=strlen(result); len=len1>len2?len1:(len2+1); len=len>len3?len:len3; for(i=0;i<len-len1;i++) putchar(' '); puts(s1); for(i=0;i<len-len2-1;i++) putchar(' '); putchar('*'); puts(s2); //printf("%d %d\n",len1,len2); if(len2>1) { tmplen=(len1>(len2+1)?len1:(len2+1)); for(i=0;i<len-tmplen;i++) putchar(' '); for(i=0;i<tmplen;i++) putchar('-'); putchar('\n'); for(i=1;i<=len2;i++) { memset(temp,0,sizeof(temp)); for(j=1;j<=len1;j++) { //printf("%d %d\n",b[i],a[j]); temp[j]+=b[i]*a[j]; tmp=temp[j]/10; temp[j]%=10; temp[j+1]+=tmp;//printf(" %d\n",temp[j]); } if(tmp>0) tmplen=len1+1;//printf("%d\n",tmplen); else tmplen=len1; for(j=0;j<len-tmplen-i+1;j++) putchar(' '); if(temp[tmplen]==0) { for(j=1;j<tmplen;j++) putchar(' '); printf("0\n"); } else { for(j=tmplen;j>=1;j--) printf("%d",temp[j]); putchar('\n'); } } } for(i=0;i<len;i++) putchar('-'); putchar('\n'); if(result[len3]=='0') { for(i=0;i<len-1;i++) putchar(' '); printf("0\n"); } else { for(i=0;i<len-len3;i++) putchar(' '); puts(result); } //puts(result); //printf("%d",c[i]); //printf("\n"); putchar('\n'); } void output(char *s1,char *s2,char *result,char c) { //puts(s1);puts(s2);puts(result); int len1,len2,len3,len,i; len1=strlen(s1); len2=strlen(s2)+1; len3=strlen(result); len=len1>len2?len1:len2; len=len>len3?len:len3; for(i=0;i<len-len1;i++) putchar(' '); puts(s1); for(i=0;i<len-len2;i++) putchar(' '); putchar(c); puts(s2); for(i=0;i<len;i++) putchar('-'); putchar('\n'); for(i=0;i<len-len3;i++) putchar(' '); puts(result); putchar('\n'); } void main() { int t; int len,i,len1,len2; char s[1005],s1[N],s2[N],result[N],c; scanf("%d",&t); //getchar(c); while(t--) { scanf("%s",s); len=strlen(s); for(i=0;i<len;i++) if(s[i]>'9'||s[i]<'0') break; c=s[i]; s[i]='\0'; strcpy(s1,s); strcpy(s2,s+i+1); //puts(s1); //printf("%c\n",c); //puts(s2); len1=strlen(s1); len2=strlen(s2); memset(result,0,sizeof(result)); switch(c) { case '+': { add(s1,s2,result); output(s1,s2,result,c); break; } case '-': { substract(s1,s2,result); if(!strcmp(s1,s2)) { printf(" %s\n",s1); printf("-%s\n",s2); for(i=0;i<=len2;i++) putchar('-'); putchar('\n'); for(i=0;i<len2;i++) putchar(' '); printf("0\n\n"); } else output(s1,s2,result,c); break; } case '*': { if(!strcmp(s1,"0")||!strcmp(s2,"0")) { int len; len=len1>(len2+1)?len1:(len2+1); for(i=0;i<len-len1;i++) putchar(' '); printf("%s\n",s1); for(i=0;i<len-len2-1;i++) putchar(' '); printf("*%s\n",s2); for(i=0;i<len;i++) putchar('-'); putchar('\n'); for(i=0;i<len-1;i++) putchar(' '); printf("0\n\n"); } else multi(s1,s2,result); break; } } } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator