1.6 编程基础之一维数组 11 大整数减法
生活随笔
收集整理的這篇文章主要介紹了
1.6 编程基础之一维数组 11 大整数减法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.6編程基礎(chǔ)之一維數(shù)組 11 大整數(shù)減法 方法二?
http://noi.openjudge.cn/ch0106/11/
1169:大整數(shù)減法
http://ybt.ssoier.cn:8088/problem_show.php?pid=1169
C++有錯誤的代碼:
//program p5_02 /* 第2課 減法神童(subtract) */ #include <bits/stdc++.h> using namespace std;const int maxL=200+10;string s1,s2; int lena,lenb,lenc; int a[maxL],b[maxL],c[maxL]; void subtract(int a[],int lena,int b[],int lenb,int c[],int &lenc) {memset(c,0,sizeof(c));lenc=lena;for(int i=0;i<lenc;++i){c[i]=a[i]-b[i];if(c[i]<0){//向前一位借1 --c[i+1];c[i]+=10;}}//刪除前導(dǎo)0 for(; lenc>1 && c[lenc-1]==0;--lenc){;} } int main( void ) {cin>>s1>>s2;//1、預(yù)處理部分lena=s1.length();lenb=s2.length();//s1所表示的數(shù)比S2小的情況 if(lena<lenb || (lena==lenb && s1<s2)) {swap(s1,s2);swap(lena,lenb);cout<<'-';}//存儲字符串s1到數(shù)組a for(int i=0;i<lena;++i){a[i]=s1[lena-i-1]-'0';}//存儲字符串s2到數(shù)組b for(int i=0;i<lena;++i){b[i]=s2[lena-i-1]-'0';}subtract(a,lena,b,lenb,c,lenc);for(int i=lenc-1;i>=0;--i){cout<<c[i];}cout<<endl;return 0; }C++ AC代碼一:
/* 1.6編程基礎(chǔ)之一維數(shù)組 11 大整數(shù)減法 方法二 http://noi.openjudge.cn/ch0106/11/1169:大整數(shù)減法 http://ybt.ssoier.cn:8088/problem_show.php?pid=1169C++ 中指針和引用的區(qū)別 https://www.runoob.com/w3cnote/cpp-difference-between-pointers-and-references.html淺談C++中指針和引用的區(qū)別 https://www.cnblogs.com/dolphin0520/archive/2011/04/03/2004869.html */ #include <bits/stdc++.h> using namespace std;const int maxL=200+1;string s1,s2;//gets()函數(shù) 這個函數(shù)盡量不要用 不安全 int lena,lenb,lenc; int a[maxL],b[maxL],c[maxL]; void subtract(int a[],int lena,int b[],int lenb,int c[],int &lenc) {memset(c,0,sizeof(c));lenc=lena;for(int i=0;i<lenc;++i){c[i]=(a[i]-b[i]);if( c[i]<0 ){--a[i+1];c[i]+=10;}}//cout<<"lenc="<<lenc<<endl; }int main( void ) {cin>>s1>>s2;lena=s1.length();lenb=s2.length();//cout<<"lena="<<lena<<<<endl//cout<<lenb<<endl;//s1所表示的數(shù)比s2的情況 if(lena<lenb || (lena==lenb && s1<s2)) {swap(s1,s2);swap(lena,lenb);cout<<'-';}/*for(int i=0;i<lena;++i){a[i]=s1[lena-i-1]-'0';}*/for (int i=0;i<=lena-1;i++) a[i]=s1[lena-i-1]-'0';for (int i=0;i<=lenb-1;i++) b[i]=s2[lenb-i-1]-'0';/*for(int i=0;i<lena;++i){b[i]=s2[lena-i-1]-'0';}*/subtract(a,lena,b,lenb,c,lenc);//刪除前導(dǎo)0 for(; lenc>1 && c[lenc-1]==0;--lenc);for(int i=lenc-1;i>=0;--i){cout<<c[i];}cout<<endl;return 0; }C++ AC代碼二:
/* 1.6編程基礎(chǔ)之一維數(shù)組 11 大整數(shù)減法 方法一 http://noi.openjudge.cn/ch0106/11/1169:大整數(shù)減法 http://ybt.ssoier.cn:8088/problem_show.php?pid=1169 */ #include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() {string a1,b1;//char a1[300],b1[300];int a[300],b[300],c[300],lena,lenb,lenc,i,x;memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));cin>>a1>>b1; //gets(a1);//gets(b1); //輸入加數(shù)與被加數(shù)lena=a1.size();lenb=b1.size();for (i=0;i<=lena-1;i++) a[lena-i]=a1[i]-'0'; //加數(shù)放入a數(shù)組for (i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48; //加數(shù)放入b數(shù)組i=1;while (i<=lena||i<=lenb){if (a[i]<b[i]){a[i]+=10; //不夠減,那么向高位借1當(dāng)10a[i+1]--;}c[i]=a[i]-b[i]; //對應(yīng)位相減i++;}lenc=i;while ((c[lenc]==0)&&(lenc>1)) lenc--; //最高位的0不輸出 for (i=lenc;i>=1;i--) cout<<c[i]; //輸出結(jié)果cout<<endl;return 0; }python3代碼:
"""1.6編程基礎(chǔ)之一維數(shù)組 11 大整數(shù)減法 http://noi.openjudge.cn/ch0106/11/"""a=int(input())b=int(input())print(a-b)總結(jié)
以上是生活随笔為你收集整理的1.6 编程基础之一维数组 11 大整数减法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 的post请求 get请求
- 下一篇: utools插件开发之监听按键的命令和状