二分模板 模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 vector<int > sub (vector<int > &A, vector<int > &B) { vector<int > C; for (int i = 0 , t = 0 ; i < A.size (); i ++ ) { t = A[i] - t; if (i < B.size ()) t -= B[i]; C.push_back ((t + 10 ) % 10 ); if (t < 0 ) t = 1 ; else t = 0 ; } while (C.size () > 1 && C.back () == 0 ) C.pop_back (); return C; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 void subtract (char s[], char s1[], char s2[], int len) { int i = 0 ; while (i < len) { if (s1[i] < s2[i]) { s1[i] += 10 ; s1[i + 1 ]--; } s[i] = s1[i] - s2[i]; i++; } while (!s[i]) i--; if (s[0 ] == 0 ) printf ("0" ); else for ( ; i >= 0 ; i--) printf ("%d" , s[i]); puts ("" ); }
题目描述: 给定两个正整数(不含前导 0),计算它们的差,计算结果可能为负数。
输入格式 共两行,每行包含一个整数。
输出格式 共一行,包含所求的差。
数据范围 1≤整数长度≤10^5
输入样例: 32 11
输出样例: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #include <iostream> #include <vector> using namespace std;bool cmp (vector<int >& A, vector<int > &B) { if (A.size () != B.size ()) return A.size () > B.size (); for (int i = A.size (); i >= 0 ; i--) if (A[i] != B[i]) return A[i] > B[i]; return true ; } vector <int > sub (vector<int >& A, vector<int > &B) { vector<int > C; int t = 0 ; for (int i = 0 ; i < A.size (); i++) { t = A[i] - t; if (i < B.size ()) t -= B[i]; C.push_back ((t + 10 ) % 10 ); if (t < 0 ) t = 1 ; else t = 0 ; } while (C.size () > 1 && C.back () == 0 ) C.pop_back (); return C; } int main () { string a ,b; vector<int > A, B; cin >> a >> b ; for (int i = a.size () - 1 ; i >= 0 ; i--) A.push_back (a[i] - '0' ); for (int i = b.size () - 1 ; i >= 0 ; i--) B.push_back (b[i] - '0' ); if (cmp (A,B)) { auto C = sub (A, B); for (int i = C.size () - 1 ; i >= 0 ; i--) printf ("%d" , C[i]); return 0 ; } else { auto C = sub (B, A); printf ("-" ); for (int i = C.size () - 1 ; i >= 0 ; i--) printf ("%d" , C[i]); return 0 ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <stdio.h> #include <string.h> #define N 100010 char s[N], s1[N], s2[N];void reverse (char s[], int len) { for (int i = 0 ; i < len; i++) s[i] -= '0' ; int i = 0 , j = len - 1 ; while (i < j) { char c = s[i]; s[i] = s[j]; s[j] = c; i++, j--; } } void subtract (char s[], char s1[], char s2[], int len) { int i = 0 ; while (i < len) { if (s1[i] < s2[i]) { s1[i] += 10 ; s1[i + 1 ]--; } s[i] = s1[i] - s2[i]; i++; } while (!s[i]) i--; if (s[0 ] == 0 ) printf ("0" ); else for ( ; i >= 0 ; i--) printf ("%d" , s[i]); puts ("" ); } int main () { scanf ("%s%s" , s1, s2); int len1 = strlen (s1); int len2 = strlen (s2); int len = len1 >= len2 ? len1 : len2; int sign = 1 ; if (len2 > len1) sign = 0 ; else if (len1 == len2) { for (int i = 0 ; i < len; i++) { if (s1[i] != s2[i]) { if (s1[i] < s2[i]) { sign = 0 ; break ; }else break ; } } } if (!sign) printf ("-" ); reverse(s1, len1); reverse(s2, len2); if (!sign) subtract(s, s2, s1, len); else subtract(s, s1, s2, len); return 0 ; }
版权声明: 此文章版权由chen-yisen所有,如有转载,请注明明來自原作者