1 #include "stdafx.h" 2 #include<iostream> 3 4 using namespace std; 5 6 unsigned int len1 = 0, len2 = 0, len3 = 0; 7 8 char flag = 0; 9 10 int main(void) 11 { 12 char num1[100] = { 0 }; 13 char num2[100] = { 0 }; 14 char temp[100] = { 0 }; 15 16 cin >> num1 >> num2; 17 18 len1 = strlen(num1); 19 len2 = strlen(num2); 20 21 if (len1 > len2 || (len1 == len2&&strcmp(num1, num2) >= 0)) 22 { 23 int i, j; 24 for (i = len1 - 1, j = len2 - 1; (i >= 0 && j >= 0); i--, j--) 25 { 26 if (num1[i] >= num2[j]) 27 { 28 temp[i] = num1[i] - num2[j] + '0'; 29 } 30 else 31 { 32 33 temp[i] = num1[i] + 10 - num2[j] + '0'; 34 num1[i - 1] -= 1; 35 } 36 37 } 38 for (; i >= 0; i--) 39 { 40 temp[i] = num1[i]; 41 } 42 flag = 0; 43 } 44 else 45 { 46 int i, j; 47 for (i = len1 - 1, j = len2 - 1; (i >= 0 && j >= 0); i--, j--) 48 { 49 if (num1[i] <= num2[j]) 50 { 51 temp[j] = num2[j] - num1[i] + '0'; 52 } 53 else 54 { 55 temp[j] = num2[j] + 10 - num1[i] + '0'; 56 num2[j - 1] -= 1; 57 } 58 } 59 for ( ; j >= 0; j--) 60 { 61 temp[j] = num2[j]; 62 } 63 flag = 1; 64 } 65 if (flag == 1) 66 { 67 cout << "-"; 68 } 69 len3 = strlen(temp); 70 int i = 0; 71 while (temp[i] == '0') 72 { 73 i++; 74 if (i == len3) 75 { 76 cout << "0"; 77 return 0; 78 } 79 80 } 81 82 for (; i < len3; i++) 83 { 84 cout << temp[i]; 85 } 86 87 return 0; 88 } 89 90 #include "stdafx.h" 91 #include<iostream> 92 #include<string> 93 94 using namespace std; 95 96 unsigned int len1 = 0, len2 = 0, len3 = 0; 97 98 char flag = 0; 99 100 int main(void) 101 { 102 string num1 = ""; 103 string num2 = ""; 104 string temp = ""; 105 106 cin >> num1 >> num2; 107 108 len1 = num1.length(); 109 len2 = num2.length(); 110 if (len1 < len2 || (len1 == len2 && num1 < num2)) 111 { 112 temp = num1; 113 num1 = num2; 114 num2 = temp; 115 len1 = num1.length(); 116 len2 = num2.length(); 117 flag = 1; 118 } 119 int i, j; 120 for (i = len1 - 1, j = len2 - 1; (i >= 0 && j >= 0); i--, j--) 121 { 122 if (num1[i] >= num2[j]) 123 { 124 num1[i] = num1[i] - num2[j] + '0'; 125 } 126 else 127 { 128 129 num1[i] = num1[i] + 10 - num2[j] + '0'; 130 num1[i - 1] -= 1; 131 } 132 133 } 134 135 if (flag == 1) 136 { 137 cout << "-"; 138 } 139 i = 0; 140 while (num1[i] == '0') 141 { 142 i++; 143 if (i == len1) 144 { 145 cout << "0"; 146 return 0; 147 } 148 149 } 150 151 for (; i < len1; i++) 152 { 153 cout << num1[i]; 154 } 155 156 return 0; 157 }