我不明白为什么以下代码总是用来回答0
。
#include <iostream>
using namespace std;
int main() {
int n,a,b;
cin>>n;
b=n%10;
while(n!=0) {
a=n%10;
n=n/10;
}
a=b;
b=a;
cout<<n<<endl;
return 0;
}
要交换两个号码,您需要一个临时寄存器
tmp=a;
a=b;
b=tmp;
但是,如果您尝试交换数字n
,则需要进行更改n
。您在循环中已销毁的对象。事先保留一份副本?
或简单地注意,floor(log(n)/log(10))
第一个数字的乘方为10。
n=23456;
int firstdec = pow(10,floor(log(n)/log(10))); // eg 10000
int firstdig = n/firstdec; // eg 2
int lastdig = n%10; // eg 6
int removed = ((n-firstdig*firstdec)/10)*10 ; // eg 3450
int final = removed + lastdig*firstdec + firstdig; // eg 63452
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句