英文字母ASCII相互轉換
(1) (2) (3) (TA)
解法1(上傳Judge)
| #include<iostream> |
| using namespace std; |
| int main() { |
| char s[2]; |
| cin >> s; |
| if(s[1] == 0) |
| cout << (int)s[0] << endl; |
| else |
| cout << (char)((int)s[0]*10+(int)s[1]-16) << endl; |
| return 0; |
| } |
解法2
| #include<iostream> |
| using namespace std; |
| int main() { |
| char s[2]; |
| cin >> s; |
| if(s[1] == 0) |
| cout << (int)s[0] << endl; |
| else |
| cout << (char)((int)((s[0]-48))*10+(int)(s[1]-48)) << endl; |
| return 0; |
| } |
解法3(相似於助教的解法)
| |
| #include<iostream> |
| using namespace std; |
| int main() { |
| // ASCII 48~57 => '0'~'9' || 65~90 => 'A'~'Z' |
| char s, n; |
| cin >> s; |
| if(s >= 65 && s <= 90) |
| cout << (int)s << endl; |
| else { |
| cin >> n; |
| cout << (char)((s-48)*10+(n-48)) << endl; |
| } |
| return 0; |
| } |
助教解法
| #include<iostream> |
| using namespace std; |
| int main() { |
| char c, d; |
| cin >> c; |
| if(c >= 'A' && c <= 'Z') |
| cout << (int)c << endl; |
| else { |
| cin >> d; |
| c = (c - '0')*10 + d - '0'; |
| cout << c << endl; |
| } |
| return 0; |
| } |