算法打印由用户给定的逆转多少? [英] Algorithm to print the reversed number given by user?

查看:148
本文介绍了算法打印由用户给定的逆转多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C ++编写,提示用户输入一个小程序,用户给出了一个数字,然后电脑显示的数目逆转。

I've written a small program in C++ that prompts the user for input, the user gives a number, then the computer displays the number reversed.

例如:17变为71。123变为321

For example: 17 becomes 71. 123 becomes 321.

这是程序:

#include <iostream>
#include <string>  //for later use.
using namespace std;

int rev(int x)
{
    int r = 0;
    while(x)
    {
        r = (r*10) + (x%10);
        x = x/10;
    }
    return r;
}

int main()
{
    int nr;
    cout << "Give a number: ";
    cin >> nr;
    rev(nr);
    cout << nr; 
    return 0;
}

该方案的最终结果:打印相同的号码,函数没有影响。我究竟做错了什么?我尝试了几种解决方案,但都无济于事。

The final result of the program: prints the same number, function has no effect. What am I doing wrong? I tried several solutions but to no avail.

推荐答案

您需要修改转(NR); NR =转( NR);

或你的函数交替更改为:

or alternately change your function to:

void rev(int& x)
{
    int r = 0;
    while(x)
    {
        r = (r*10) + (x%10);
        x = x/10;
    }
    x = r;
}

这篇关于算法打印由用户给定的逆转多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆