反向获得余额? [英] Reverse of Getting a Remainder?

查看:163
本文介绍了反向获得余额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取用户输入的数字,加7,然后除以10,并取余数,并将该值用作我的新值。我们还假设输入数字将始终为4个字符长,因此对于示例,用户将输入1234,或1235或9999等。它将永远不会大于或小于该值。

I am taking a number that the user inputs, adding 7, then dividing by 10, and taking the remainder and using that value as my new value. We are also assuming that the input number will ALWAYS be 4 characters long, so for an example the user would input 1234, or 1235, or 9999 etc. It will never be larger, or less than that.

看起来像这样:

int num;
printf("Input a number: ");
scanf("%d", &num);

num = (num + 7) % 10;

printf("Here is the number after my calculations: %d", num);

现在我需要弄清楚如何做相反的操作。

Now I need to figure out how to do the reverse of that.

如果我只获得剩余的数字,如何获得输入的原始数字?

How do I get the original number that was input if I am only given the remainder?

推荐答案

有许多输入将给出相同的输出(例如,所有的1000,1010,1020,1030等将返回7)。因此,无法恢复四位数字的原件。

There are many inputs that will give the same output (for example, all of 1000, 1010, 1020, 1030 etc will return 7). Therefore, there's no way to restore the four-digit original.

如果,另一方面,如果您知道输入是单位数字,您可以执行以下操作以恢复它:

If, on the other hand, you knew that the input were single-digit, you could do the following to restore it:

num = (num + 3) % 10;

(以模10算法加3,与减7相同。)

(Adding 3 in modulo-10 arithmetic is the same as subtracting 7.)

还有其他情况下,你可以恢复的数字,但不幸的是,在你的四位数的一般情况下,这根本是不可能的。

There are other circumstances where you could restore the number, but unfortunately in your four-digit general case it's simply not possible.

这篇关于反向获得余额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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