程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
数据范围:
n=input() for i in range(len(n)-1,-1,-1): print(n[i],end='')
#include<stdio.h> #include<string> #include<map> #include<iostream> #include<math.h> using namespace std; int main(){ int n=0; int temp =0; int leftnum=0; cin>>n; temp = n; if(n==0) cout<<0; while(temp>0){ leftnum = temp%10; temp = temp/10; cout<<leftnum; } cout<<endl; }
#include<stdio.h> #include<string.h> int main() { int n; char ch; while(scanf("%d",&n)!=EOF) { while(n!=0) { ch = n%10+'0'; n/=10; printf("%c",ch); } printf("\n"); } return 0; }
不管输入的是什么,都可以直接用字符串的方式读取,因此我们直接把输入的数字转化成字符串。然后倒序输出即可。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); for(int i =s.length()-1;i>=0;--i) System.out.print(s.charAt(i)); } }
#include<stdio.h> #include<stdlib.h> int main() { int num, i=0; scanf("%d", &num); while(num) { printf("%d", num % 10); num /= 10; } return 0; }
#include<iostream> using namespace std; #include <string> #include <stack> int main(){ string number; cin>>number; stack<char> Stack; for(int i = 0; i < number.size();i++){ Stack.push(number[i]); } while(!Stack.empty()){ cout<< Stack.top(); Stack.pop(); } }倒序符合栈的先入后出的特点,还蛮好想的