Home12月2日:a-b
阿新 • • 發佈:2018-12-20
Description
Now, Give you two intgers A and B , Please calculate the value of A minus B.Attation: A、Band A−B are all non-negative numbers.
Input
Each line will contain two integers A and B.Process to end of file.(EOF)
Output
For each case, Please output the value of A minus B
Sample Input 1
5 3
4 1
Sample Ouput 1
2
3
毛學姐提供的程式碼
#include <bits/stdc++.h> using namespace std; void rev(char a[], int len) { for(int i = 0; i < len / 2; ++i) swap(a[i],a[len - i - 1]); } char a[1000],b[1000],s[1000]; int main() { int ai, bi, val, bit; int len, len1, len2; while(cin >> a >> b) { len1 = strlen(a); len2 = strlen(b); len = len1 > len2 ? len1 : len2; rev(a,len1); rev(b,len2); bit = 0; for(int i = 0; i < len; ++i){ ai = i < len1 ? a[i] - '0' : 0; bi = i < len2 ? b[i] - '0' : 0; ai -= bit; bit = 0; if(ai < bi){ ai+=10; bit += 1; } s[i] = ai - bi + '0'; } bool flag = true; for(int i = len - 1; i >= 0; --i){ if(s[i] == '0' && flag) continue; flag = false; putchar(s[i]); } puts(""); } return 0; }