为什么最后结果不对?(c++)

P5705 【深基2.例7】数字反转

hw20王文浩 @ 2020-12-27 13:00:04

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,s,g,x,k;
    cout<<"原数是";
    cin>>a;
    b=(10*a)/1000;
    s=(10*a)/100%10;
    g=(10*a)/10%10;
    x=(10*a)%100;
    k=x+0.1*g+0.01*s+0.001*b;
    cout<<"反向值为"<<k<<endl;
    return 0;
}

by Surelysuper @ 2020-12-27 13:09:28

@hw20王文浩 第六行和第十三行去掉


by xzllll07 @ 2020-12-27 13:44:04

调试信息去了


by 123456zmy @ 2020-12-27 13:44:24

int 不能存小数


by 123456zmy @ 2020-12-27 13:53:19

int main(){int c=getchar();if(c>13)main(),putchar(c);


by lyh080125 @ 2020-12-27 13:54:11

用double或者浮点应该可以


by hw20王文浩 @ 2021-01-09 21:40:55

@123456zmy 已解决,感谢!


by hw20王文浩 @ 2021-01-09 21:42:15

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int o;
    float a,b,s,g,k,x;
    cout<<"原数是";
    cin>>a;
    o=a*10;
    b=o/1000;
    s=o/100%10;
    g=o/10%10;
    x=o%10;
    k=(1*x+0.1*g+0.01*s+0.001*b);
    cout<<"反向值为"<<k<<endl;
    return 0;
}

@lyh080125 好的!已解决,感谢!


|