不用寫成absolute和secgap也可以處理 其實我寫的很多此一句
hint:ASCII中數字轉int實值是-48
→(char)'0'=(int)48
#include <iostream>
#include <string.h>
using namespace std;
int absolute(int a)
{
if (a<0) return (-a);
else return a;
}
int secgap(string num)
{
int leng,odd=0,even=0,i,j;
leng=num.length();
for (i=0;i<leng;i+=2)
odd=odd+num[i]-48;
for (i=1;i<leng;i+=2)
even=even+num[i]-48;
return odd-even;
}
int main()
{
int a;
string number;
while(cin >> number)
{
cout << absolute(secgap(number)) << endl;
}
return 0;
}