用10位数字交换1000位数字(C) [英] Exchange 1000s digit with 10s digit (C)

查看:173
本文介绍了用10位数字交换1000位数字(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图切换例如:输入54321.987,那么4和2应该切换,所以输出将是52341.987。 54321.777应该变成52341.777。如果是2345.777则应该是4325.777。任何比我不在乎的东西。但是如果像888886543.777那样只有第二个和第四个数字应该从逗号前面最右边的部分切换。因此,它将成为888884563.777



因此,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屋!

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