c290. APCS 2017-0304-1秘密差
c290. APCS 2017-0304-1秘密差 |  題目連結  :  https://zerojudge.tw/ShowProblem?problemid=c290 |  解題思路  : 先將答案設為0,加上奇數項,減掉偶數項(也可分別處理奇偶數的項的和再相減) |  程式碼  :    /* c290. APCS 2017-0304-1秘密差 https://zerojudge.tw/ShowProblem?problemid=c290 skyblue 2023/10/19 AC (2ms, 348KB) */  #include  using namespace std;  int main(){     //加速輸入輸出程式     ios::sync_with_stdio(0);     cin.tie(0);      string a; //用字串儲存輸入的數字     cin >> a;     int ans = 0; //將答案初始值設為0      for(int i = 0; a[i]; i++){         if(i%2) ans += a[i]-'0'; //若是奇數項,加上值         else ans-= a[i]-'0'; //若是偶數項,減掉值     }      ans = (ans>0)?ans:-ans; //將答案轉換為絕對值      cout << ans;     return 0; }