joj 1920解题报告

 

 1920: Jojer

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 733 231 Standard

Mr Jojer is a teacher, and he is crazy in programming, he wants the computer to do every thing for him, one day, one of his colleague Mr NEtiger asks him to write a procedure for him. But Mr Jojer has another difficult problem, so he asks you to do it for him. Do you want a try? Ok, let's begin!

There are two numbers for the pupils to operate, Mr NEtiger wants to know the answers of plus, minus, multiply, divide,so he can check the answers which the pupils hand in.

The input contain two numbers, you should output the answers in the order of plus, minus, multiply, divide. You may assume that they are dividable and no remainder.

Is it easy? But you should know the numbers may as long as 50 digits.

Sample Input

10 2
15 3

Sample Output

12
8
20
5
18
12
45
5

水题一道,就是高精度计算,就是考察高精度四则运算。

代码:

语言:c++

#include<iostream> #include<cstring> #define N 1001 using namespace std; int main() { char m1[100],m2[100],c1[100],c2[100]; char *addition(char *m1,char *m2); char *subtract(char *m1,char *m2); char *multiply(char *m1,char *m2); void high_precise_division(char *c1,char *c2); while(cin>>m1>>m2) { strcpy(c1,m1); strcpy(c2,m2); high_precise_division(c1,c2); cout<<addition(m1,m2)<<endl<<subtract(m1,m2)<<endl<<multiply(m1,m2)<<endl<<c1<<endl; } return 0; } char *addition(char *m1,char *m2) { int *str2int(char *str); char *int2str(int *a,int n); int check(int *a,int n); int len1=strlen(m1),len2=strlen(m2),len,i,*t1,*t2; len=(len1>=len2)?len1:len2; t1=str2int(m1); t2=str2int(m2); for(i=len1;i<=len;++i) t1[i]=0; for(i=len2;i<=len;++i) t2[i]=0; for(i=0;i<len;++i) t1[i]+=t2[i]; len=check(t1,len); return int2str(t1,len); } int *str2int(char *str) { int len=strlen(str); int *a=new int[(len+1)*sizeof(int)]; for(int i=0;i<len;++i) a[i]=(int)str[len-1-i]-'0'; return a; } char *int2str(int *a,int n) { int len=n; char *str=new char[(len+1)*sizeof(char)]; for(int i=0;i<len;++i) str[i]=(char)a[len-1-i]+'0'; str[len]='/0'; return str; } int check(int *a,int n) { int len=n,i; while(len>1&&a[len-1]==0) --len; for(i=0;i<len;++i) if(a[i]>=10) { a[i+1]+=a[i]/10; a[i]%=10; } if(a[i]!=0) len=i+1; return len; } char *subtract(char *m1,char *m2) { int *str2int(char *str); int len1=strlen(m1),len2=strlen(m2),len,i,c,cf=0,begin,*t1,*t2,n; char *temp,*csub; len=(len1>=len2)?len1:len2; temp=new char[(len+2)*sizeof(char)]; csub=new char[(len+3)*sizeof(char)]; if(len>len1||(len1==len2&&strcmp(m1,m2)<0)) { cf=1; strcpy(temp,m1); strcpy(m1,m2); strcpy(m2,temp); len1=strlen(m1); len2=strlen(m2); } t1=str2int(m1); t2=str2int(m2); for(i=len1;i<len;++i) t1[i]=0; for(i=len2;i<len;++i) t2[i]=0; for(i=0;i<len;++i) { if(t1[i]>=t2[i]) t1[i]-=t2[i]; else { t1[i]-=t2[i]; c=(t1[i]*(-1)/10+1); t1[i]+=c*10; t1[i+1]-=c; } } n=i; begin=0; while(n>1&&t1[n-1]==0) --n; if(cf==1) { ++n; begin=1; csub[0]='-'; } for(i=begin;i<n;++i) csub[i]=(char)t1[n-1-i]+'0'; csub[n]='/0'; return csub; } char *multiply(char *m1,char *m2) { int *str2int(char *str); char *int2str(int *a,int n); int check(int *a,int n); int len1=strlen(m1),len2=strlen(m2),len,*t1,*t2,*prod,i,j; len=len1+len2; t1=str2int(m1); t2=str2int(m2); prod=new int[(len+1)*sizeof(int)]; for(i=0;i<=len;++i) prod[i]=0; for(i=0;i<len1;++i) for(j=0;j<len2;++j) prod[i+j]+=t1[i]*t2[j]; len=check(prod,len); return int2str(prod,len); } int compare(char * c1,int s1,int e1,char *c2,int s2,int e2)/*大整数比较大小*/ { while(c1[s1]=='0'&&s1<e1)s1++; while(c2[s2]=='0'&&s2<e2)s2++; if(e1-s1>e2-s2)return 1; if(e1-s1<e2-s2)return -1; int i1;int f11=0; for(i1=0;i1<=e1-s1;i1++) { if(c1[s1+i1]>c2[s2+i1]){f11=1;break;} if(c2[s2+i1]>c1[s1+i1])break; } if(i1>e1-s1)return 0; if(f11)return 1; return -1; } void sub(char *c1,int l1,char *c2,int n,int l)/*两个大整数相减,结果放在c1中*/ { int fig=0; if(l1>l) { fig=1; int i5; for(i5=l;i5>0;i5--) c2[i5]=c2[i5-1]; c2[0]='0';l++; } int jw=0; int i4; char tc[N]; for(i4=l-1;i4>=0;i4--) { tc[i4]=((c2[i4]-48)*n+jw)%10+48; jw=((c2[i4]-48)*n+jw)/10; } jw=0; for(i4=l-1;i4>=0;i4--) { int ttt=jw; if(c1[i4]-tc[i4]-jw>=0) { tc[i4]=c1[i4]-tc[i4]-jw+48; jw=0; } else { jw=(tc[i4]-c1[i4]+jw); if(jw%10==0)jw/=10; else jw=jw/10+1; tc[i4]=jw*10+c1[i4]-tc[i4]-ttt+48; } } if(fig) { for(i4=0;i4<l-1;i4++)c2[i4]=c2[i4+1]; l--; c2[l]='/0'; } tc[l1]='/0'; for(i4=0;i4<=l1;i4++) c1[i4]=tc[i4]; } void high_precise_division(char *c1,char *c2) { int len1,len2; len1=strlen(c1); len2=strlen(c2); int i,j,k,ip; i=0; while(c1[i]=='0'&&i<len1-1)i++; for(j=0;i<len1;j++,i++)c1[j]=c1[i]; len1=j;c1[j]='/0'; i=0; while(c2[i]=='0'&&i<len2-1)i++; for(j=0;i<len2;j++,i++)c2[j]=c2[i]; len2=j;c2[j]='/0'; /*while(c1[0]=='0'&&i<len1-1) { for(k=0;k<len1-1;k++) c1[k]=c1[k+1]; len1--; }去掉前面的0*/ /*while(c2[0]=='0'&&j<len2-1) { for(k=0;k<len2-1;k++) c2[k]=c2[k+1]; len2--; }去掉前面的0*/ c1[len1]='/0'; c2[len2]='/0'; if(strcmp(c2,"0")==0)return ;/*当除数为0时*/ if(strcmp(c1,"0")==0){strcpy(c2,"0");return ;}/*当被除数为0时*/ if(len1<len2||len1==len2&&compare(c1,0,len1-1,c2,0,len2-1)<0) { strcpy(c2,c1); c1[0]='0';c1[1]='/0'; return ; } else { ip=0; char product[N],*pr;/*部分积*/ pr=product; for(ip=0;ip<len2-1;ip++) pr[ip]=c1[ip]; for(i=0;i<=len1-len2;i++) { pr[ip++]=c1[len2-1+i]; if(ip>=len2 && compare(pr,0,ip-1,c2,0,len2-1)>=0) { char tc[N]; for(j=1;j<=9;j++) { for(k=0;k<ip;k++)tc[k]=pr[k]; sub(tc,ip,c2,j,len2);/*pr-c2*j结果放在pr中*/ for(k=0;tc[k]!='/0';k++); if(compare(tc,0,k-1,c2,0,len2-1)<0) break; } strcpy(pr,tc); ip=strlen(pr); c1[i]=j+48; while(pr[0]=='0'&&ip>1) { for(j=0;j<ip-1;j++) pr[j]=pr[j+1]; ip--; } if(ip==1&&pr[0]=='0')ip--; } else c1[i]='0'; } while(c1[0]=='0') { for(j=0;j<i-1;j++) c1[j]=c1[j+1]; i--; } c1[i]='/0'; if(ip==0){pr[0]='0';pr[1]='/0';ip=1;} else { while(pr[0]=='0'&&ip>1) { for(j=0;j<ip-1;j++)pr[j]=pr[j+1]; ip--; } } for(j=0;j<ip;j++) c2[j]=pr[j]; c2[ip]='/0'; return ; } }  

 

 

你可能感兴趣的:(c,input,语言,output,Numbers)