[問題] 高中程式解題的C290

作者: tyjh (....)   2018-09-18 21:28:28
https://zerojudge.tw/ShowProblem?problemid=c290
題目如上
找到參考解答 請問 A=A+s[i]-'0'; 中 -'0' 的意義是什麼?
#include <iostream>
using namespace std;
int main(){
string s;
int A,B;
while(cin >> s){
A=0,B=0;
for(int i=0;i<s.length();i++){
if (i%2){
A=A+s[i]-'0';
}else{
B=B+s[i]-'0';
}
}
if (A>B) cout << A-B << endl;
else cout << B-A << endl;
}
}
作者: cutekid (可愛小孩子)   2018-09-18 21:44:00
ascii code to 整數: 0x30 ~ 0x39 對應 0 ~ 9
作者: Sanvean   2018-09-18 21:45:00
我好像有點理解錯問題……回錯XD
作者: Ori185 (Ori185)   2018-09-18 23:59:00
一樓的意思是說,利用數字的ASCII碼做運算利用s[i]的ASCII碼減掉0的ASCII碼可以得到s[i]是什麼數字
作者: tyjh (....)   2018-09-19 00:08:00
懂了~

Links booklink

Contact Us: admin [ a t ] ucptt.com