标签:include flag == lse while 精度 turn amp cout
#include<iostream> #include<string> using namespace std; const int Max = 20000; int Compare(int a[], int b[]); //比较大小 void Subtract(int a[], int b[]); //数组减法 int main() { int a[Max] = {0}, b[Max] = {0}, i; string str1, str2; cin >> str1 >> str2; int len1 = str1.size(), len2 = str2.size(); a[0] = len1; b[0] = len2; for (i = 1; i <= len1; i ++) a[i] = str1[len1 - i] - ‘0‘; for (i = 1; i <= len2; i ++) b[i] = str2[len2 - i] - ‘0‘; int flag = Compare(a, b); if (flag == 0) cout << "0"; else if (flag == 1) { Subtract(a, b); for (i = a[0]; i > 0; i --) cout << a[i]; } else { Subtract(b, a); cout << "-"; for (i = b[0]; i > 0; i --) cout << b[i]; } return 0; } int Compare(int a[], int b[]) { int i; if (a[0] > b[0]) //a的位数 > b的位数, 则a > b return 1; else if (a[0] < b[0]) //a的位数 < b的位数, 则a < b return -1; for (i = a[0]; i > 0; i --) { if (a[i] > b[i]) return 1; else if (a[i] < b[i]) return -1; } return 0; // 两数相等 } void Subtract(int a[], int b[]) // a被减数, b减数 { int i; for (i =1; i <= a[0]; i ++) { a[i] = a[i] - b[i]; if (a[i] < 0) { a[i] += 10; a[i + 1] --; } } while(a[0] > 0 && a[a[0]] == 0) //去掉高位多余的0,同时修正a的位数 a[0] --; }
标签:include flag == lse while 精度 turn amp cout
原文地址:https://www.cnblogs.com/Es-war/p/12449599.html