C#中数字的反向 [英] reverse of a number in c#

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

问题描述

我想对c#中的数字求逆.

Hi i want to get reverse of a number in c#

推荐答案

让我帮助您进行思考.您可以尝试自己编写代码.

将数字除以10,然后取mod(余数)值.您得到第一个数字.
现在将数字除以10,然后忽略小数点后的部分.
再次将数字除以10并取mod值.您得到第二个数字.
现在将数字除以10,然后忽略小数点后的部分.
依此类推...直到获得所有数字.

现在,将它们全部以相反的顺序放置并打印出来(如果要使用字符串操作).

如果不想使用字符串运算,可以开始将最后一位乘以10.
然后倒数第二个10 * 10 = 100
然后倒数第三乘10 * 10 * 10 = 1000
依此类推....

最后添加此结果,您将获得相反的数字.
Let me help you get thinking. You can try writing the code yourself.

Divide the number by 10 and take the mod (remainder) value. You get the first digit.
Now divide the number by 10 and ignore the part after the decimal.
Divide the number by 10 again and take the mod value. You get the second digit.
Now divide the number by 10 and ignore the part after the decimal.
And so on...till you have got all the digits.

Now place them all in the reverse order and print them out (if you want to use string operations).

If you don''t want to use string operations, you can start multiplying the last digit by 10.
Then the second last by 10*10 = 100
Then the third last by 10*10*10= 1000
And so on....

Finally add this result and you will get the reversed number.




此链接可以帮助您 http://social. msdn.microsoft.com/Forums/zh-CN/csharplanguage/thread/220e694a-4613-47e4-99ec-1f25893db1b0/ [
Hi,

This link may help you http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/220e694a-4613-47e4-99ec-1f25893db1b0/[^]



试试吧;
Hi,
Try this;
int n = 12345;
int left = n;
int rev = 0;
while(left>0)
{
  r = left % 10;
  rev = rev * 10 + r;
  left = left / 10;
}

Console.WriteLine(rev);



以及以下链接上带有快照的最佳示例:
反向 [ ^ ]



And the best example with snaps on following link:
Reverse[^]


这篇关于C#中数字的反向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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