求解,234都没通过

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

Shiki_wooi @ 2022-02-10 20:49:32

#include <iostream>
#include <cstdio>

using namespace std;

int main(){
    double a;
    int res[128];
    int length;

    while(cin>>a){
        length = 0;
        int n = a/100;
        while(n != 0){
            res[length++] = n;
            a = (a-n*100)*10;
            n = a/100;
        }
        for(int i = length-1; i >= 0; i--){
            if(i == 2){
                cout<<'.'; 
            }
            cout<<res[i];
        }
        cout<<endl;
    }

    return 0;
}

by Herbert @ 2022-02-11 10:59:25

此题小数点后只有一位,手动输出小数点后一位和小数点,再用for倒输出不就好了吗?快乐~


by feng_nuoyiman @ 2022-03-22 13:30:10

有简单的解法,适合新手

#include <iostream>
using namespace std;
int main()
{
    float m,n,m1,m2,m3,m4;
    int a;
    cin>>m;
    a=m*10;
    m1=a/1000;
    m2=a/100%10;
    m3=a/10%10;
    m4=a%10;
    m1=m1*1/1000;
    m2=m2*1/100;
    m3=m3*1/10;
    m=m1+m2+m3+m4;
    cout<<m;
    return 0;
}

|