牛客网 - 在线编程 - 华为机试 - 数字颠倒

题目描述

描述:

输入一个整数,将这个整数以字符串的形式逆序输出

程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001

输入描述:

输入一个int整数

输出描述:

将这个整数以字符串的形式逆序输出

示例1

输入

1516000

输出

0006151

c++:

#include<iostream>
#include<algorithm> //reverse()
using namespace std;

int main()
{
    int n;
    string s;
    while(cin >> n)
    {
        s = to_string(n);  //转换成字符串
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
    return 0;
}

参考:C++中int与string互相转换

int转string://to_string() (#include <string>)

#include<iostream>
#include<string> // to_string()
using namespace std;

int main()
{
	int n = 345;
	string s ;
	s = to_string(n);
	cout << s + 'a' << endl;
	system("pause");
	return 0;
}
//输出结果:345a

string转int://atoi(str.c_str())

#include<iostream>
using namespace std;

int main()
{
	int n;
	string s = "12";
	n = atoi(s.c_str());
	cout << n + 1 << endl;
	system("pause");
	return 0;
}
//输出结果:13

python:

a = input()
print(a[::-1])

猜你喜欢

转载自blog.csdn.net/qq_39735236/article/details/81984053