程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
import java.util.*; public class Main{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int i=0; String res=""; while(num!=0){ int t1=num%10; int t2=num/10; res=res+String.valueOf(t1); i++; num=t2; } System.out.println(res); } }
[思路] a%10取个位然后输出,a/=10去掉个位,循环直到输出所有数字。
P.S. 没必要转成字符再输出,直接按int类型输出即可,不影响判题程序/脚本依然按照字符(串)类型比较程序输出与测试用例
#include int main() { int a; scanf("%d", &a); do printf("%d", a%10), a /= 10; // 注意这里只有一条语句 不用加花括号 while(a); // do while才能在输入等于0时输出 感谢@牛客122438009号指正 return 0; // P.P.S. 然而牛客的测试用例并没有考察输入为0的边界情况 }
#include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { string str; vector<char> vec; char temp; cin>>str; int size = str.length(); for(int i=0;i<size;i++) { vec.push_back(str[i]); } for(int i=0;i<size/2;i++) { temp=vec[i]; vec[i]=vec[size-i-1]; vec[size-i-1]=temp; } for(int i=0;i<size;i++) { cout<<vec[i]; } return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNext()){ String s=scan.nextLine(); StringBuffer sb=new StringBuffer(); /*或者可以直接利用stringBuffer对象的reverse函数*/ for (int i = 0; i < s.length(); i++) { sb.append(s.charAt(s.length()-1-i)); } System.out.println(sb.toString()); } } }
#include<stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { char buf[8] = {0}; int pos = 0; while (n > 0) { buf[pos++] = n % 10 + '0'; n /= 10; } printf("%s", buf); } return 0; }