We are playing a game of writing numbers backwards.
The first line gives the count n, and each of the next n lines gives one integer.
For each integer, reverse the order of its digits, from the last to the first, and print it on its own line.
For a negative number, the minus sign at the front is not reversed and stays at the front (-45 → -54).
Zeros that end up at the front after reversing are kept, all of them (120 → 021, -100 → -001).
0 stays 0.
Input
1 ≤ n ≤ 100. Each integer is greater than -10^18 and less than 10^18 (at most 18 digits). A nonzero integer does not start with 0, zero is written only as "0", and "-0" never appears. A line holds nothing but the integer
Output
n lines, each a reversed number (sign at the front, leading zeros kept)
Example 1 · stdin
4
120
-45
7
-100
stdout
021
-54
7
-001
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
The key is to reverse the text itself instead of turning it into a number. Collecting x % 10 into a new number, or turning the reversed text into a number as in int(s[::-1]), drops the zeros that came to the front: 120 becomes 21 and -100 becomes -1. The values exceed 2.1 billion, so an int in C, Java or C# cannot even read them, and in JS, TS and Craykon numbers are doubles, so Number("123456789012345678") has already turned into 123456789012345680. Each language reverses differently: Python s[::-1], JS/TS [...s].reverse().join("") (strings have no reverse, so make an array), Java new StringBuilder(s).reverse(), C# turns it into a char[], calls Array.Reverse, then new string(a), C++ std::reverse(s.begin(), s.end()) and Craykon s.Reverse(). C has no reverse function, so a loop swaps the two end characters while moving toward the middle. Handle the sign separately: if the first character is -, set it aside, reverse only the rest (Python s[1:], Java/C# s.substring(1) / s.Substring(1), Craykon s.Substring(1)) and put the sign back in front. In C and C++ you simply start reversing at index 1. Reversing everything turns -45 into 54-.
Pythonmain.py · 8 lines
n = int(input())for _ in range(n): s = input().strip() # 수로 바꾸지 않고 문자열 그대로 뒤집는다(앞에 온 0을 지키려고) if s[0] == "-": print("-" + s[1:][::-1]) else: print(s[::-1])
JavaScriptmain.js · 10 lines
const [n, ...lines] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/);const out = [];for (const s of lines) { // Number로 바꾸면 2^53을 넘는 수가 바뀌고 앞의 0도 사라진다: 문자열 그대로 const neg = s.startsWith("-"); const digits = neg ? s.slice(1) : s; const rev = [...digits].reverse().join(""); out.push(neg ? "-" + rev : rev);}console.log(out.join("\n"));
import java.io.BufferedReader;import java.io.InputStreamReader;public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { String s = in.readLine().trim(); if (s.startsWith("-")) { out.append('-').append(new StringBuilder(s.substring(1)).reverse()); } else { out.append(new StringBuilder(s).reverse()); } out.append('\n'); } System.out.print(out); }}
C#Main.cs · 26 lines
using System;using System.Text;class Program{ static string Reverse(string s) { char[] a = s.ToCharArray(); Array.Reverse(a); return new string(a); } static void Main() { int n = int.Parse(Console.ReadLine().Trim()); var sb = new StringBuilder(); for (int i = 0; i < n; i++) { string s = Console.ReadLine().Trim(); if (s.StartsWith("-")) sb.Append('-').Append(Reverse(s.Substring(1))); else sb.Append(Reverse(s)); sb.Append('\n'); } Console.Write(sb); }}
C++main.cpp · 16 lines
#include <algorithm>#include <iostream>#include <string>int main() { int n; std::cin >> n; for (int k = 0; k < n; k++) { std::string s; std::cin >> s; // 부호가 있으면 둘째 글자부터 끝까지만 뒤집는다 auto from = (s[0] == '-') ? s.begin() + 1 : s.begin(); std::reverse(from, s.end()); std::cout << s << "\n"; }}
Cmain.c · 23 lines
#include <stdio.h>#include <string.h>int main(void) { int n; char s[32]; scanf("%d", &n); for (int k = 0; k < n; k++) { scanf("%31s", s); /* 부호가 있으면 1번 칸부터 뒤집는다 */ int i = (s[0] == '-') ? 1 : 0; int j = (int)strlen(s) - 1; while (i < j) { char t = s[i]; s[i] = s[j]; s[j] = t; i++; j--; } puts(s); } return 0;}
Craykonmain.cray · 14 lines
n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){ s = Console.ReadLine().Trim(); // 수로 바꾸지 않는다: 앞에 온 0이 사라지고, 2^53보다 큰 수는 바뀐다 if (s.StartsWith("-")) { Console.WriteLine("-" + s.Substring(1).Reverse()); } else { Console.WriteLine(s.Reverse()); }}