大佬求助为什么输入 999.9 输出 8.999 ?

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

WOLFFY_HS @ 2022-10-07 18:45:55

#include<iostream>
using namespace std;
int main(){
    int a,ge,shi,bai;
    double b,sfw,bfw,qfw,d;
    cin>>b;
    a=b;
    b-=a;
    ge=a%100%10;
    shi=a%100/10;
    bai=a/100;
    sfw=ge/10.0;
    bfw=shi/100.0;
    qfw=bai/1000.0;
    ge=b*10;
    c=ge+sfw+bfw+qfw;
    cout<<d;
    return 0;

}

by zz_z2Spider @ 2022-10-07 18:54:24

为什么这么做呢亲,输入三个char再倒着输出不香嘛。


by zz_z2Spider @ 2022-10-07 18:56:14

呸 应该是4个。


by CJ_Fu @ 2022-10-07 18:59:37

@c15154691188 柠的 d 赋值了吗...


by Hack3rD @ 2022-10-07 19:04:03

@c15154691188 这样当数计算写起来比较复杂,你可以考虑这样:

#include <cstdio>
int main() {
    char a, b, c, d;
    scanf("%c%c%c.%c", &a, &b, &c, &d);
    printf("%c.%c%c%c\n", d, c, b, a);
    return 0;
}

by WOLFFY_HS @ 2022-10-07 19:05:42

谢谢几位!


by WOLFFY_HS @ 2022-10-07 19:09:54

@CJ_FU 应该是c不是d……写错了。


|