今天模拟
考了一堆高精题
看到题目的一瞬间:awsl 物理意义上的
qaqqqqaqaqaqaqaqaqaqqq
然后强行刚题
首先是高精度加法
要先把短的补位0
然后。。没了
#include<cstdio>
#include<iostream>
#define sev en
using namespace std;
string s,t;
string add(string str1,string str2) {
string str;
int len1 = str1.length();
int len2 = str2.length();
if(len1 < len2) {
for(int i = 1; i <= len2-len1; i++)
str1 = "0" + str1;
} else {
for(int i = 1; i <= len1 - len2; i++)
str2 = "0" + str2;
}
len1 = str1.length();
int cf = 0;
int temp;
for(int i = len1 - 1; i >= 0; i--) {
temp = str1[i] - '0' + str2[i] - '0' + cf;
cf=temp / 10;
temp %= 10;
str = char(temp + '0') + str;
}
if(cf != 0)
str = char(cf + '0') + str;
return str;
}
int main() {
cin >> s >> t;
cout << add(s,t);
return 0;
}
然后是高精度减法
先判断一下先输的数小还是后输的数小
提前输出负号
再用大的减小的
最后别忘了清空前导零
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define sev en
using namespace std;
const int maxn = 10010;
int len1, len2;
int a[maxn], b[maxn];
string s1, s2;
bool compare(string a, string b){
if (a.size() < b.size()) return true;
if (a.size() > b.size()) return false;
for (int i = 0; i < a.size(); ++i){
if (a[i] > b[i]) return false;
if (b[i] > a[i]) return true;
}
return false;
}
int main(){
cin >> s1;
cin >> s2;
if (compare(s1, s2)){
swap(s1, s2);
printf("-");
}
len1 = s1.size();
len2 = s2.size();
for (int i = 0; i < len1; ++i) a[len1 - i] = s1[i] - '0';
for (int i = 0; i < len2; ++i) b[len2 - i] = s2[i] - '0';
for (int i = 1; i <= len1; ++i){
a[i] -= b[i];
if (a[i] < 0){
a[i + 1]--;
a[i] += 10;
}
}
while (a[len1] == 0 && len1 > 1) len1--;
for (int i = len1; i >= 1; --i)
printf("%d", a[i]);
return 0;
}
然后是高精乘
记忆犹新,NOIP前考了一次,写跪了
是学长帮忙改的。。。整个改掉了emmmm
这次重写心理阴影巨大
写了好久www
但挺简单的。。。没啥可说
#include<cstdio>
#include<cstring>
#define sev en
using namespace std;
#define N 100010
char x[N],y[N];
int a[N],b[N],c[N << 1];
int main() {
// freopen("t.in","r",stdin);
// freopen("t.out","w",stdout);
scanf("%s",x);
scanf("%s",y);
a[0] = strlen(x),b[0] = strlen(y);
for(int i = 1; i <= a[0]; i++)
a[i] = x[a[0] - i] - '0';
for(int i = 1; i <= b[0]; i++)
b[i] = y[b[0] - i] - '0';
for(int i = 1; i <= a[0]; i++)
for(int j = 1; j <= b[0]; j++)
c[i + j - 1] += a[i] * b[j];
int len = a[0] + b[0];
for(int i = 1; i <= len; i++)
if(c[i] > 9) {
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
while(c[len] == 0 && len > 1)
len--;
for(int i = len; i >= 1; i--)
printf("%d",c[i]);
return 0;
}
最后还有个阶乘
代码没有
等我到时候找找看吧www