标签:str else algo div ret for a* lin hellip
此文为博主原创,转载时请通知博主,并把原文链接放在正文醒目位置。
很久不写高精了,虽说我觉得高精也不会考...还是稍微写一写,防止手生。
两个小时过去了……
集合了高精+高精、高精-高精、高精*高精、高精/低精。
目前还没发现什么错误,应该可以应付各种情况。
本来想允许它读入负数的,结果发现减法读负数太麻烦了...所以只能读非负数。
下面贴代码。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cmath> 4 #include<cstring> 5 char s[10002]; 6 long long rest; 7 8 inline int Max(int a,int b) 9 {return a>b?a:b;} 10 11 struct BigInt 12 { 13 int len,flag; 14 int num[10002]; 15 16 BigInt(){ 17 len = 0,flag = 0; 18 memset(num,0,sizeof(num)); 19 } 20 21 void clear() { 22 len = 0,flag = 0; 23 memset(num,0,sizeof(num)); 24 } 25 }a,b,ans; 26 27 void Swap(BigInt &a,BigInt &b) 28 { 29 BigInt t = a; 30 a = b,b = t; 31 } 32 33 inline void Read(BigInt &ans) 34 { 35 ans.clear(); 36 scanf("%s",s); 37 int len = 0,n = strlen(s); 38 for(int i = n;i >= 0;-- i) 39 if(s[i]>=‘0‘ && s[i]<=‘9‘) 40 ans.num[++len] = s[i]-‘0‘; 41 ans.len = len; 42 } 43 44 void Print(BigInt a) 45 { 46 if(a.flag) printf("-"); 47 int len = a.len; 48 for(int i = len;i >= 1;-- i) 49 printf("%d",a.num[i]); 50 printf("\n"); 51 } 52 53 long long CTL(BigInt a) 54 { 55 long long r = 0; 56 for(int i = a.len;i >= 1;-- i) 57 r = r*10+a.num[i]; 58 return r; 59 } 60 61 void Spin(int &a,int &b) 62 {int t = a;a = b,b = t;} 63 64 bool operator == (BigInt a,BigInt b) 65 { 66 if(a.len != b.len) return false; 67 for(int i = 1;i <= a.len;++ i) 68 if(a.num[i] != b.num[i]) 69 return false; 70 return true; 71 } 72 73 bool operator < (BigInt a,BigInt b) 74 { 75 if(a.len > b.len) return false; 76 if(a.len < b.len) return true; 77 for(int i = a.len;i >= 1;-- i) 78 if(a.num[i] > b.num[i]) 79 return false; 80 return true; 81 } 82 83 BigInt operator + (BigInt a,BigInt b) 84 { 85 ans.clear(); 86 int len = Max(a.len,b.len)+1; 87 int x = 0; 88 for(int i = 1;i <= len;++ i) 89 { 90 ans.num[i] = a.num[i]+b.num[i]+x; 91 x = ans.num[i]/10; 92 ans.num[i] %= 10; 93 } 94 if(!ans.num[len]) len --; 95 ans.len = len; 96 return ans; 97 } 98 99 BigInt operator - (BigInt a,BigInt b) 100 { 101 ans.clear(); 102 if(a == b) { 103 ans.num[++ans.len] = 0; 104 return ans; 105 } 106 else if(a < b) Swap(a,b),ans.flag = 1; 107 108 int len = Max(a.len,b.len); 109 for(int i = 1;i <= len;++ i) 110 { 111 ans.num[i] += a.num[i]-b.num[i]; 112 if(ans.num[i] < 0){ 113 ans.num[i] += 10; 114 ans.num[i+1] --; 115 } 116 } 117 while(!ans.num[len]) len --; 118 ans.len = len; 119 return ans; 120 } 121 122 BigInt operator * (BigInt a,BigInt b) 123 { 124 ans.clear(); 125 int len = a.len+b.len+1; 126 int x = 0; 127 for(int i = 1;i <= a.len;++ i) 128 { 129 for(int j = 1;j <= b.len;++ j) 130 { 131 ans.num[i+j-1] += a.num[i]*b.num[j]+x; 132 x = ans.num[i+j-1]/10; 133 ans.num[i+j-1] %= 10; 134 } 135 ans.num[i+b.len] += x; 136 x = 0; 137 } 138 while(!ans.num[len] && len) len --; 139 if(!len) len = 1; 140 ans.len = len; 141 return ans; 142 } 143 144 bool SpeciaL(BigInt a,BigInt b) 145 { 146 ans.clear(); 147 if(a == b) { 148 ans.num[++ans.len] = 1; 149 printf("/: ");Print(ans); 150 printf("Rest = 0\n"); 151 return true; 152 } 153 if(a < b) { 154 printf("/: 0\n"); 155 printf("Rest = ");Print(a); 156 return true; 157 } 158 return false; 159 } 160 161 BigInt operator / (BigInt a,long long b) 162 { 163 ans.clear(); 164 int len = a.len,flag = 0,cnt = 0; 165 long long tmp = a.num[len]; 166 for(int i = len-1;i >= 1;-- i) 167 { 168 if(tmp >= b) 169 { 170 flag = true; 171 ans.num[++cnt] = tmp/b,tmp %= b; 172 } 173 else if(flag) ans.num[++cnt] = 0; 174 tmp = tmp*10+a.num[i]; 175 } 176 ans.num[++cnt] = tmp/b,tmp %= b; 177 ans.len = cnt; 178 rest = tmp; 179 for(int i = 1;i <= cnt/2;++ i) 180 Spin(ans.num[i],ans.num[cnt-i+1]); 181 return ans; 182 } 183 184 int main() 185 { 186 Read(a),Read(b); 187 printf("+: ");Print(a+b); 188 printf("-: ");Print(a-b); 189 printf("*: ");Print(a*b); 190 if(SpeciaL(a,b)); 191 else{ 192 long long B = CTL(b); 193 printf("/: ");Print(a/B); 194 printf("Rest = %lld\n",rest); 195 } 196 return 0; 197 }
标签:str else algo div ret for a* lin hellip
原文地址:http://www.cnblogs.com/shingen/p/7810092.html