【ybtoj】【字符串】【例题1】数字反转

【例题1】数字反转


Link

传送门
题目


解题思路

从字符串的后面开始输出
特判

  1. 开头是负号
  2. 前导零

Code

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

string s;
int k, t;

int main() {
    
    
	getline(cin, s);
	if (s[0] == '-')
		k = 1, cout<<'-';//特判负号
	for (int i = s.size() - 1; i >= k; i--) {
    
    
		if (s[i] != '0' || t == 1)//特判前导零
			cout << s[i], t = 1;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39940018/article/details/113000803