题解 P1932 【A+B A-B A*B A/B A%B Problem】
难道我谷这么多大佬没有人知道bc吗?
自从蒟蒻了解了bc,就像哥伦布发现新大陆,赶紧就用它来水高精模板题了。
#include<bits/stdc++.h>
using namespace std;
int main(){
char a[100000],b[100000],c[100000];
scanf("%s%s",a,b);
sprintf(c,"echo %s+%s | bc | tr '\n' ' ' | tr '\\' ' ' | sed 's/ //g'",a,b);
system(c);
cout<<endl;
sprintf(c,"echo %s-%s | bc | tr '\n' ' ' | tr '\\' ' ' | sed 's/ //g'",a,b);
system(c);
cout<<endl;
sprintf(c,"echo %s*%s | bc | tr '\n' ' ' | tr '\\' ' ' | sed 's/ //g'",a,b);
system(c);
cout<<endl;
sprintf(c,"echo %s/%s | bc | tr '\n' ' ' | tr '\\' ' ' | sed 's/ //g'",a,b);
system(c);
cout<<endl;
sprintf(c,"echo %s %% %s | bc | tr '\n' ' ' | tr '\\' ' ' | sed 's/ //g'",a,b);
system(c);
cout<<endl;
return 0;
}
但是这个做法有一个很大的缺陷,就是不能把计算的结果用一个变量(字符数组)保存,而是只能直接输出。这就直接导致这种做法只能水高精度的模板题,而不是模板的高精度题是很难使用这个方法的
虽然有这个局限,但也能让C党20行切掉高精度,享受Python的快感!
对了,如果有哪个大佬知道如何将高精度的运算结果保存下来麻烦私信我,谢谢