求大佬看看哪里错了

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

aexlmx @ 2024-11-24 15:54:18


int main()
{
    float x;
    scanf("%f", &x);
    int a = 10 * x;
    float o, w, t, f;
    o = (a % 10); a /= 10;
    w = (a % 10) / 10; a /= 10;
    t = (a % 10) / 100; a /= 10;
    f = (a % 10) / 1000; 
    printf("%f", o + w + t + f);

by ny_ShuaiQideWo @ 2024-11-24 16:10:10

c++直接整数除以整数会默认结果为整数,就把小数部分抹掉了,在除数后加个 .0 就行了


by ny_ShuaiQideWo @ 2024-11-24 16:10:43

int main()
{
    float x;
    scanf("%f", &x);
    int a = 10 * x;
    float o, w, t, f;
    o = (a % 10); a /= 10;
    w = (a % 10) / 10.0; a /= 10;
    t = (a % 10) / 100.0; a /= 10;
    f = (a % 10) / 1000.0; 
    printf("%.3f", o + w + t + f);

by ny_ShuaiQideWo @ 2024-11-24 16:11:28

.3f是保留三位,不知道不加能不能过


by ny_ShuaiQideWo @ 2024-11-24 16:12:43

事实证明不加过不了


by ny_ShuaiQideWo @ 2024-11-24 16:13:31

@aexlmx


by aexlmx @ 2024-11-24 20:50:31

@Shuai_Qi_de_Wo 谢谢大佬


by yangtianshu @ 2024-12-13 21:49:27

a在过程中变了吧


|