用10位数字交换1000位数字(C) [英] Exchange 1000s digit with 10s digit (C)
问题描述
因此,LearningC建议,我试图只交换与10位数千位数字。
但是无论我尝试什么,我都会遇到错误。我不能通过错误。我应该如何做到这一点?
到目前为止,实际运作的只是这个:
int main(int argc,char ** argv){
double x;
scanf(%lf,& x);
double tens =((int)(x / 10))%10;
double thousand =((int)(x / 1000))%10;
printf(%09.3f,x + =(ten - thousand)* 990.0);
返回0;
$ b $ p
$ b 上面的代码现在可以工作了。首先,你必须确定这些数字。
您可以使用
double tens =((int)(x / 10))%10;
double thousand =((int)(x / 1000))%10;
可以让您做
$ $ $ (十* 10.0) - (千* 1000.0)+(十* 1000.0)+(千*十*);
在原始位置减去它们,并以交换的方式重新添加它们。
您可以优化这个
pre $ x = x +十*(1000.0 - 10.0 ) - 千*(1000.0 - 10.0);
同样,这也是
x + =(ten - thousand)* 990.0;
I am trying to switch for example: input 54321.987, then 4 and 2 should switch, so output would be 52341.987. 54321.777 should become 52341.777. If it is 2345.777 it should be 4325.777. Anything less than that I do not care about. But if it was like 888886543.777 only the second and fourth numbers should be switch from the most right part before the comma. So it would become 888884563.777
So as LearningC suggested, I am trying to exchange only the 1000s digit with the 10s digit.
But whatever I try, I get errors. I can't pass the errors. How shall I do this?
What I have so far that actually works is just this:
int main(int argc, char** argv) {
double x;
scanf("%lf", &x);
double tens = ((int) (x / 10)) % 10;
double thousands = ((int) (x / 1000)) % 10;
printf("%09.3f", x += (tens - thousands) * 990.0);
return 0;
}
The code above now works.
First, you have to determine these digits.
You can do so with
double tens = ((int)(x / 10)) % 10;
double thousands = ((int)(x / 1000)) % 10;
which enables you to do
x = x - (tens * 10.0) - (thousands * 1000.0) + (tens * 1000.0) + (thousands * 10.0);
which subtracts them at their original place and re-adds them in a swapped way.
You can optimize this to
x = x + tens * (1000.0 - 10.0) - thousands * (1000.0 - 10.0);
and, again, this to
x += (tens - thousands) * 990.0;
这篇关于用10位数字交换1000位数字(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!