#12804: C++ WA (line:6) 小數第三位


tntchn (tntchn)


您的答案為: -351.251
正確答案為: -351.252

這我真的不懂
為什麼會有這個問題
這是我的程式碼

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
    string line;
    while (getline(cin,line)) {
        float y_o = 0;
        float y_e = 0;
        stringstream a(line);
        string temp;
        while(getline(a,temp,':')) {
            float temp_v=0;
            a >> temp_v;
            if (atoi(temp.c_str())%2 != 0) y_o += temp_v;
            else y_e += temp_v;
        }
        cout << y_o-y_e << endl;
    }
    return 0;
}
#13469: Re:C++ WA (line:6) 小數第三位


nkavengertree (LaG)


您的答案為: -351.251
正確答案為: -351.252

這我真的不懂
為什麼會有這個問題
這是我的程式碼

#include 
#include 
#include 
using namespace std;

int main() {
    string line;
    while (getline(cin,line)) {
        float y_o = 0;
        float y_e = 0;
        stringstream a(line);
        string temp;
        while(getline(a,temp,':')) {
            float temp_v=0;
            a >> temp_v;
            if (atoi(temp.c_str())%2 != 0) y_o += temp_v;
            else y_e += temp_v;
        }
        cout << y_o-y_e << endl;
    }
    return 0;
}

我把float 改成double就過了,應該是位數不夠