儲存方式:字元陣列
計算方式:
1. 先加偶數
2. 再減奇數
3. 若 X 長度為奇數,則要再減 '0'
加偶數還是減偶數無所謂,反正最後會取絕對值
另外,偶數位 - 奇數位時, '0' 會被消掉,因此只要考慮 X 的長度是否為奇數即可
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char X[1000];
int diff, len;
while(cin >> X){
diff = 0; len = strlen(X);
for(int i = 0; i < len; i+=2)
diff += X[i];
for(int i = 1; i < len; i+=2)
diff -= X[i];
if(len%2)
diff -= '0';
if(diff < 0)
diff = -diff;
cout << diff << endl;
}
return 0;
}
全站熱搜
留言列表