首页 > 试题广场 >

倒着输出整数

[编程题]倒着输出整数
  • 热度指数:550 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 62M,其他语言125M
  • 算法知识视频讲解
C写一个输入的整数,倒着输出整数的函数,要求用递归方法。

输入描述:
一个整数


输出描述:
该整数的倒序输出
示例1

输入

-1563

输出

-3651
importjava.util.Scanner;
 
publicclassMain {
 
 
    publicstaticvoidmain(String[] args){
        Scanner in = newScanner(System.in);
        String s = in.next();
        if(s.charAt(0)=='-') {
            System.out.println("-"+Integer.parseInt(newStringBuffer(s).deleteCharAt(0).reverse().toString()));
        }else{
            System.out.println(Integer.parseInt(newStringBuffer(s).reverse().toString()));
        }
 
             
            
         
   
    }
 
 
}
发表于 2018-10-22 18:27:54 回复(0)
#include<stdio.h>
int reverse(int num,int tmp){
    if(num == 0)
        return tmp;
    return reverse(num/10,tmp*10+num%10);
}

int reverse_front(int num){
    return num>=0? reverse(num,0):-reverse(-num,0);
}

int main(){
    int num;
    scanf("%d",&num);
    printf("%d\n",reverse_front(num));
}
指定用C语言,另外有些边界情况未讨论,此递归乃是迭代改过来的。
发表于 2018-07-21 17:57:03 回复(0)
#include <stdio.h>
#include <cmath>

static bool isNotZero = false;

void reverse(int a) {
 
    if (a > 0) {
        
        if (a % 10 == 0) {
            
            if (isNotZero == false) {
                a = a / 10;
                reverse(a);
            } else {
                printf("%d",a % 10);
                a = a / 10;
                reverse(a);
            }
            
        } else {
            isNotZero = true;
            printf("%d",a % 10);
            a = a / 10;
            reverse(a);
        }
        
    } else {
        return;
    }
}

int main () {
    int num;
    scanf("%d",&num);
    if (num < 0) {
        printf("-");
    } 
    
    reverse(fabs(num));
    
    return 0;
}

发表于 2018-07-17 17:02:04 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        StringBuffer sb = new StringBuffer();
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n<0)
                System.out.print("-");
            reverse(sb,Math.abs(n));
            String str = new String(sb);
            String tr = str.replaceFirst("^0*", "");
            System.out.println(tr);
        }
        sc.close();
    }
    public static void reverse(StringBuffer sb, int n){
        if(n!=0){
            sb.append(n%10);
            reverse(sb,n/10);
        }
    }
}

发表于 2018-07-17 13:48:40 回复(2)
#include<iostream>
#include<cmath>
using namespace std;
void reverse(int x)
{
   if(x>0)
{
    cout<<x%10;
   reverse(x/10);
}
else
    return;
}
int main()
{
    int n;
    cin>>n;
    if(n<0) 
    {
    cout<<"-";
   // reverse(-n);
    }
   // else
    reverse(abs(n));
    return 0;
}

发表于 2018-07-17 11:00:26 回复(0)

用自己电脑的python3.6运行都能正确输出,在这里的python3.5输出总是不对,这到底是是为什么?难道是版本的差异?
class solution:
    def reverseOutput(self,x):
        if x >= 0:
            print (x % 10,end = "")
            x = x // 10
            if x != 0:
                return self.reverseOutput(x)
            else:
                print ("\n")
        else:
            print('-',end ="")
            self.reverseOutput(-x)

编辑于 2018-07-17 10:23:09 回复(0)
#include <stdio.h>
#include <stdlib.h>

bool not0 = false;

void doprint(int n) {
    if(n == 0)
        return;
    int x = n % 10;
    if(!not0) {
        if(x)
            not0 = true;
    } 
    if(not0)
        printf("%d",x);
    doprint(n / 10);
}

int main() {
    int n = 0;
    scanf("%d",&n);
    if(n < 0) {
        printf("-");
    }
    doprint(::abs(n));
}

发表于 2018-07-16 20:47:51 回复(0)
#include <stdio.h>
void _printf(int  x)
{
    if(x>=0 && x<=9)
         printf("%d",x);
    else
    {
       printf("%d",x%10);
       _printf(x/10);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    if(n<0)
    {
        n=-n;
        putchar('-');
    }
    while(n%10==0)
        n=n/10;
    _printf(n);
    return 0;
}
发表于 2018-07-16 18:27:09 回复(0)
importjava.util.Scanner;
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner sc = newScanner(System.in);
        intnum = sc.nextInt();
        System.out.println(inverse(num));
    }
 
    publicstaticintinverse(intnum){
        booleanflag = num>=0?true:false;
        intresult = inverse1(0, Math.abs(num));
        if(flag){
            returnresult;
        }
        else{
            return0- result;
        }
    }
    publicstaticintinverse1(intresult,intnum){
        if(num < 10){
            result += num;
            returnresult;
        }
        else{
            result += num % 10;
             returninverse1(result *10, num / 10);
        }
    }
}

发表于 2018-07-16 17:27:16 回复(0)