ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

why am i wrong? on c++
Posted by Cui Xiangfei 2 Nov 2013 14:04
#include <iostream>
#include <vector>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{
    vector<int> v;
    int value;
    while(true)
    {
        cin >> value;
        if(cin.eof())
        {
            break;
        }
        else if(cin.fail())
        {
            cin.clear();    // 清空错误状态
            cin.ignore();   // 忽略掉下一个字符
            continue;
        }
        else if(value >= 0)
        {
            v.push_back(value);
        }
    }
    cout << fixed;
    cout.precision(4);
    for(vector<int>::reverse_iterator iter = v.rbegin(); iter != v.rend(); ++iter)
    {
        if(*iter == 0)
            cout << (double)0 << endl;
        else
            cout << sqrt(*iter) << endl;
    }
    return 0;
}
Re: why am i wrong? on c++
Posted by cures 18 May 2014 10:02
Do not break on cin.eof(), it may be set even if a good value is read (in case the last line is not terminated by newline, I guess). Or maybe they terminate data by non-numeric symbols? Nobody would say :)
Break on if (!cin), do not clear cin.
And why do you check for (*iter==0)? It works fine as in general case.