资深守护
#include <bits/stdc++.h>
using namespace std;
/*
加法
*/
string jia(string a, string b)
{
string ans;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
a.push_back('0'); b.push_back('0');
if(a.size() < b.size()) swap(a, b);
while(b.size() < a.size()) b.push_back('0');
int c[1000]={0}, k = a.size();
for(int i = 0; i < a.size(); i++)
{
int s = c[i] + a[i]-'0' + b[i] - '0';
c[i] = s%10;
c[i+1] += s/10;
}
while(c[k]==0 && k>=0) k--;
while(k >= 0)
ans.push_back(c[k--]+'0');
return ans;
}
/*
减法
*/
string jian(string a, string b){
string ans;
bool flag = false;
if(a.size() < b.size() || (a.size()==b.size() && a < b)){
swap(a, b);
flag = true;
}
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
a.push_back('0'); b.push_back('0');
if(a.size() < b.size()) swap(a, b);
while(b.size() < a.size()) b.push_back('0');
int c[1000]={0}, k = a.size();
for(int i = 0; i < a.size(); i++){
if(a[i] < b[i]) a[i]+=10, a[i+1]--;
c[i] = a[i] - b[i];
}
while(c[k]==0 && k>=0) k--;
if(flag) ans.push_back('-');
while(k >= 0)
ans.push_back(c[k--]+'0');
return ans;
}
/*
乘法
*/
string cheng(string a, string b){
string ans;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int c[1000]={0}, k = a.size()+b.size()+1;
for(int i = 0; i < a.size(); i++){
for(int j = 0; j < b.size(); j++){
c[i+j] += (a[i]-'0')*(b[j]-'0');
c[i+j+1] += c[i+j]/10;
c[i+j] %= 10;
}
}
while(c[k]==0 && k>=0) k--;
while(k >= 0){
ans.push_back(c[k--]+'0');
}
return ans;
}
/*
除法
*/
string chu(string a, long long b){
int z[256] = {0}, d = 0;
for(int i = 0; i < a.size(); i++)
{
d = 10*d + a[i] - '0';
z[i] = d / b;
d %= b;
}
string ans;
int len = 0;
while(z[len] == 0 && len < a.size()-1)
len++;
for(int i = len; i < a.size(); i++){
ans.push_back(z[i]+'0');
}
return ans;
}
阿巴在2021-08-06 22:06:24追加了内容
本蒟蒻只会这么多了
缔造者之神
乘法:
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
char a1[10005],b1[10005];
int a[10005],b[10005],c[10005],lena,lenb,lenc,i,j,x;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%s",a1);
scanf("%s",b1);
lena=strlen(a1);
lenb=strlen(b1);
for(int i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48;
for(int i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48;
for(int i=1;i<=lena;i++)
{
x=0;//存放进位
for(int j=1;j<=lenb;j++)//对乘数的每一位进行处理
{
c[i+j-1]=a[i]*b[j]+x+c[i+j-1];//当前乘积+进位+原数
x=c[i+j-1]/10;
c[i+j-1]%=10;
}
c[i+lenb]=x;//进位
}
lenc=lena+lenb;
while(c[lenc]==0&&lenc>1) lenc--;//删除前导0
for(int i=lenc;i>=1;i--) cout<<c[i];
cout<<endl;
return 0;
}
中级天翼
啊这你要哪个
减法有很多种
除法也是
你得注明,不然有人会来蹭帖(
武建豪在2021-08-05 17:39:48追加了内容
完了,图片发不出来
武建豪在2021-08-06 23:02:24追加了内容
1.位数相同且无借位的高精度减法
#include<bits/stdc++.h>
using namespace std;
char a[2000],b[2000];
int a1[2000],b1[2000],c1[2000];
int main(){
cin>>a>>b;
for(int i=0;i<strlen(a);i++){
a1[i]=a[i]-48;
b1[i]=b[i]-48;
c1[i]=a1[i]-b1[i];
}
for(int i=0;i<strlen(b);i++){
cout<<c1[i];
}
return 0;
}
这个还可以改,你自己看着办,代码习惯可能不太好,望海涵
2.位数不同且无借位的高精度减法
#include<bits/stdc++.h>
using namespace std;
char a[2000],b[2000];
int a1[2000],b1[2000],c1[2000],lena,lenb;
int main(){
cin>>a>>b;
lena=strlen(a);
lenb=strlen(b);
for(int i=0;i<=strlen(a)-1;i++){
a1[lena-i-1]=a[i]-'0';
}
for(int i=0;i<=strlen(b)-1;i++){
b1[lenb-i-1]=b[i]-'0';
}
int x=0;
for(int i=0;i<lena;i++){
c1[i]=a1[i]-x-b1[i];
if(c1[i]<0){
c1[i]+=10;
x=1;
}
else{
x=0;
}
}
for(int i=lena-1;i>0;i--){
if(c1[i]==0){
lena--;
}
else{
break;
}
}
for(int i=lena-1;i>=0;i--){
cout<<c1[i];
}
return 0;
}
3.位数不一定相同且有借位的高精度减法
#include<bits/stdc++.h>
using namespace std;
char a[2000],b[2000];
int a1[2000],b1[2000],c1[2000],lena,lenb;
int main(){
cin>>a>>b;
lena=strlen(a);
lenb=strlen(b);
for(int i=0;i<=lena-1;i++){
a1[lena-1-i]=a[i]-'0';
}
for(int i=0;i<=lenb-1;i++){
b1[lenb-1-i]=b[i]-'0';
}
int x=0;
for(int i=0;i<lena;i++){
c1[i]=a1[i]-x-b1[i];
if(c1[i]<0){
c1[i]+=10;
x=1;
}
else{
x=0;
}
}
for(int i=lena-1;i>0;i--){
if(c1[i]==0){
lena--;
}else{
break;
}
}
for(int i=lena-1;i>=0;i--){
cout<<c1[i];
}
return 0;
}
哦,还有,别举报
武建豪在2021-08-06 23:05:26追加了内容
第二个把for循环终点那个strlen(a)-1和
strlen(b)-1
改成lena-1和lenb-1
缔造者之神
初级天翼
减法:
#include<bits/stdc++.h>
using namespace std;
int a[1005]={0},b[1005]={0},c[1005]={0};
string st1,st2;
int main(){
cin>>st1>>st2;
int len1=st1.size();
int len2=st2.size();
int fh=0,l,zh=0,k=0;
//cout<<st1<<endl<<st2<<endl;
if(len1<len2)
{
l=len2;
fh=1;
//swap(st1,st2);
//swap(len1,len2);
}
else
{
if(len1>len2)
{
l=len1;
fh=0;
}
else
{
if(st1<st2)
{
l=len2;
fh=1;
//swap(st1,st2);
//swap(len1,len2);
//cout<<fh<<endl;
}
else
{
l=len1;
fh=0;
}
}
}
if(fh!=0)
{
swap(st1,st2);
swap(len1,len2);
}
reverse(st1.begin(),st1.end());
reverse(st2.begin(),st2.end());
//cout<<st1<<endl<<st2<<endl;
for(int i=0;i<len1;i++)
{
b[i]=st1[i]-'0';
//cout<<b[i]<<" b:"<<len1<<endl;
}
//cout<<len1<<endl;
for(int i=0;i<len2;i++)
{
c[i]=st2[i]-'0';
// cout<<c[i]<<" C:"<<len2<<endl;
}
//cout<<len2<<endl;
for(int i=0;i<l;i++)
{
//cout<<b[i]<<"-"<<c[i]<<"=";
a[i]=b[i]-c[i];
//cout<<a[i]<<endl;
if(a[i]<0)
{
b[i+1]--;
b[i]=b[i]+10;
i--;
}
}
//cout<<endl;
if(fh!=0)
cout<<'-';
for(int i=l-1;i>=0;i--)
{
zh=zh+a[i];
}
//cout<<"l:"<<l<<"zh:"<<zh<<endl;
for(int i=l-1;i>=0;i--)
{
if(zh!=0)
{
if(a[i]!=0 || k==1)
{
cout<<a[i];
k=1;
}
else
{
}
}
}
if(zh==0)
cout<<"0";
return 0;
}
除法:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000+4; //根据题目的最大值。+4为了防止A+B出现进位
char s1[MAXN] = {};//存储字符串
char s2[MAXN] = {};//存储字符串
int tmp[MAXN] = {};//交换用字符串
int a[MAXN] = {};//存储加数A
int b[MAXN] = {};//存储加数B
int c[MAXN] = {};//存储和B
int compare(int a[], int b[]) {
//索引为0的数据为数组长度
if (a[0]>b[0]) {
return 1;
} else if (a[0]<b[0]) {
return -1;
}
//逐位比较
for (int i=a[0]; i>0; i--) {
if (a[i]>b[i]) {
return 1;
} else if (a[i]<b[i]) {
return -1;
}
}
return 0;
}
void numcpy(int a[],int b[],int dest) {
//将数组右移,使两个数组右端对齐,形参q数组储存右移后的结果
for (int i=1;i<=a[0];i++) {
b[i+dest-1] =a[i];
}
b[0] = a[0]+dest-1;
}
int main() {
scanf("%s %s", s1, s2);//读入字符串
//处理负数
bool flaga = false;//乘数a的符号
if ('-'==s1[0]) {
flaga = true;
strcpy(s1, &s1[1]);//删除负号
}
bool flagb = false;//乘数b的符号
if ('-'==s2[0]) {
flagb = true;
strcpy(s2, &s2[1]);//删除负号
}
//处理输出的负号
if (true==flaga && false==flagb) {
//商为负数
printf("-");
}
//处理乘数1
int len = strlen(s1);
a[0] = len;
for (int i=0; i<len; i++) {
a[len-i]=s1[i]-'0';
}
//处理乘数2
len = strlen(s2);
b[0] = len;
for (int i=0; i<len; i++) {
b[len-i]=s2[i]-'0';
}
if (0==compare(a,b)) {
//两数相等
printf("1\n0\n");
return 0;
} else if (-1==compare(a,b)) {
//被除数小,除数大
printf("0\n");//输出除数
if (true==flaga) {
printf("-");
}
printf("%s\n", s1);
return 0;
} else {
c[0] = a[0]-b[0]+1;
for (int i=c[0]; i>0; i--) {
memset(tmp, 0, sizeof(tmp));
//高位对齐
numcpy(b,tmp,i);
//
while (compare(a, tmp)>=0) {
c[i]++;
//减法
for (int j=1; j<=a[0]; j++) {
if (a[j]<tmp[j]) {
a[j+1]--;
a[j]+=10;
}
a[j]-=tmp[j];
}
int k=a[0];
while (a[k]==0) {
k--;
}
a[0]=k;
}
}
//控制最高位的0
while (c[0]>0 && c[c[0]]==0) {
c[0]--;
}
}
//逆序打印输出商和余数
for (int i=c[0]; i>0; i--) {
printf("%d", c[i]);
}
printf("\n");
if (0==a[0]) {
printf("0\n");
} else {
if (true==flaga) {
printf("-");
}
for (int i=a[0]; i>0; i--) {
printf("%d", a[i]);
}
printf("\n");
}
return 0;
}
资深守护
减法
杜承俊在2021-08-05 09:50:02追加了内容
图发不了
#include<bits/stdc++.h>
using name** std;
int main(){
st**ng x,y;
int a[10005]={0},b[10005]={0},i,l,k,w=0,t;
cin>>x>>y;
l=x.size(); k=y.size(); t=max(l,k);
for(i=l-1;i>=0;i--)a[l-i]=(x[i]-48);
for(i=k-1;i>=0;i--)b[k-i]=(y[i]-48);
for(i=1;i<=t;i++){
if(a[i]>=b[i])a[i]-=b[i];
else{ a[i+1]--; a[i]=a[i]+10-b[i]; }
} for(i=t;i>=1;i--){
if(a[i]!=0)w=1;
if(w==1)cout<<a[i]; }
}
缔造者之神
https://blog.**dn.net/u011815404/article/details/79938748(高精除以低精或者两个高精相除)
https://blog.**dn.net/zhhe0101/article/details/51418387(高精度减法,一位一位减就可以了)
王文博在2021-08-05 15:47:14追加了内容
**=**
缔造者之神
高精度加法:
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
char a1[100],b1[100];
int a[100],b[100],c[100],lena,lenb,lenc,i,x;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));//清空
gets(a1);
gets(b1);//字符数组读入,此乃高精度加法,算最简单的一个
lena=strlen(a1);
lenb=strlen(b1);
for(int i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48;
for(int i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48;//将加数放入int数组当中
lenc=1;
x=0;
while(lenc<=lena||lenc<=lenb)
{
c[lenc]=a[lenc]+b[lenc]+x;//两个数加起来
x=c[lenc]/10;
c[lenc]%=10;
lenc++;
}
c[lenc]=x;
if(c[lenc]==0) lenc--;
for(int i=lenc;i>=1;i--) cout<<c[i];
cout<<endl;//好习惯
return 0;//保险起见
}
缔造者之神
高精度加法:
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a[100],b[100],c[100],lena,lenb,lenc,i;
char n[100],n1[100],n2[100];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(n1);
gets(n2);//不多讲
if(strlen(n1)<strlen(n2)||(strlen(n1)==strlen(n2)&&strcmp(n1,n2)<0))//处理被减数和减数,交换它们(其实就是判断谁大)
{
strcpy(n,n1);
strcpy(n1,n2);
strcpy(n2,n);
cout<<"-";//被减数没减数大,肯定是负数啦!
}
lena=strlen(n1);
lenb=strlen(n2);
for(int i=0;i<=lena-1;i++) a[lena-i]=(int)(n1[i]-48);
for(int i=0;i<=lenb-1;i++) b[lenb-i]=(int)(n2[i]-48);//真规整啊!
i=1;
while(i<=lena||i<=lenb)
{
if(a[i]<b[i])
{
a[i]+=10;
a[i+1]--;//向高位借一当十(还不还?)
}
c[i]=a[i]-b[i];
i++;
}
lenc=i;
while((c[lenc]==0)&&(lenc>1)) lenc--;//删除前导0
for(int i=lenc;i>=1;i--) cout<<c[i];
cout<<endl;
return 0;//好习惯
}
自己打的!
王文博在2021-08-07 13:01:50追加了内容
减法!