首页 > 学院 > 开发设计 > 正文

华为OJ:数字颠倒

2019-11-08 02:16:09
字体:
来源:转载
供稿:网友

描述: 输入一个整数,将这个整数以字符串的形式逆序输出 程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001

输入描述: 输入一个int整数

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

输入例子: 1516000

输出例子: 0006151

解答1:#include <iostream>#include <vector>using namespace std;int main(){ int n; vector<char>v; while(cin>>n) { v.clear(); while(n) { v.push_back(n%10+'0'); n/=10; } for(vector<char>::iterator it=v.begin();it!=v.end();++it) { cout<<*it; } cout<<endl; } return 0;}解答2:#include <stdio.h>int main() {int a;scanf("%d", &a);while (a > 0){PRintf("%d", a % 10);a /= 10;}return 0;}解答3:#include<iostream>using namespace std;int main(){ int n; cin>>n; char tmp; while(n){ tmp=n%10 + '0'; cout<<tmp; n/=10; } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表