Ç - 反转数 [英] C - reverse a number

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

问题描述

我在Linux上的编码在C,我需要扭转一些。 (EG:12345将变成54321),我打算只用itoa将其转换为字符串,然后扭转,因为它可能有很多与字符串操作更容易,但事实证明itoa是非标准,不包括在gcc。是否有这样做的二进制旋转式的东西放在十进制数的一种方式,如果没有什么办法,我应该走?

解决方案

  INT N;
scanf函数(%d个,和放大器; N);
INT REV = 0,REM;
而(正大于0)
{
    REM = N%10; //取出剩下的..所以就变成5 12345
    REV = REV * 10 + REM; //乘以10的当前数量,并添加这个余数。
    N = N / 10; //划分的数目。因此,它成为1234。
}
的printf(%D,REV);
 

I am coding in C on linux, and I need to reverse a number. (EG: 12345 would turn into 54321), I was going to just convert it into a string using itoa and then reverse that, as it's probably a lot easier with string manipulation, however it turns out itoa is non standard and isn't included in gcc. Is there a way of doing a binary rotation style thing on decimal numbers and if not what approach should I take?

解决方案

int n;
scanf("%d",&n);
int rev=0,rem;
while(n>0)
{
    rem=n%10; //take out the remainder .. so it becomes 5 for 12345
    rev=rev*10+rem; //multiply the current number by 10 and add this remainder.
    n=n/10; //divide the number. So it becomes 1234.
}
printf("%d",rev);

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

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