Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6362071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:51:37+00:00 2026-05-24T23:51:37+00:00

The text of problem: Input : The input stream contains a set of integer

  • 0

The text of problem:

Input: The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.

Output: For each number Ai from the last one till the first one you should output its square root. Each square root should be printed in a separate line with at least four digits after decimal point.

Sample
input:

 1427  0   

876652098643267843 5276538

output:

2297.0716
936297014.1164
0.0000
37.7757

Time Limit: 2.0 second
Memory Limit: 16 MB

C and C++ programs are compiled on the server by the 32-bit Microsoft Visual C++ 2010. Until August 3, 2010 Intel C++ Compiler 7.0 was used. You can download a free copy of Microsoft Visual Studio 2010 Express on this page. The compiler is invoked with the following parameters:

// C
cl /TC /MT /EHsc /O2 /W3 /Za /D "_CRT_SECURE_NO_WARNINGS" 
   /D "_CRT_SECURE_NO_DEPRECATE" /D "ONLINE_JUDGE"

// C++
cl /TP /MT /EHsc /O2 /W3 /Za /D "_CRT_SECURE_NO_WARNINGS" 
   /D "_CRT_SECURE_NO_DEPRECATE" /D "ONLINE_JUDGE"

This is my solution of this problem in gcc 4.3:

#include <iostream>
#include <list>
#include <string>
#include <sstream>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <stdio.h>

void sqrt_f(double n)
{
    printf ( "%.4f\n", sqrt( static_cast<double>( n ) ) );
}

int main()
{
    std::list<double> numbers;
    std::string sInput;
    getline( std::cin, sInput );
    std::istringstream parse( sInput );
    double tmp;
    while ( parse >> tmp )
        numbers.push_front( tmp );
    std::for_each(numbers.begin(), numbers.end(), sqrt_f);
    return 0;
}

But Judgement result is: wrong answer (test1)

What’s the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:51:38+00:00Added an answer on May 24, 2026 at 11:51 pm

    Warning: when I wrote this, the question was still on the code-review site, so it’s written primarily as a code review. The part that applies primarily here on SO (fixing the input problem) is only barely mentioned in passing.

    For the moment, I’m going to assume that you’ve fixed the problem @Jeff Mercado pointed out to get code something like this:

    #include <iostream>
    #include <list>
    #include <string>
    #include <sstream>
    #include <math.h>
    #include <algorithm>
    #include <iomanip>
    #include <stdio.h>
    
    void sqrt_f(double n)
    {
        printf ( "%.4f\n", sqrt( static_cast<double>( n ) ) );
    }
    
    int main()
    {
        std::list<double> numbers;
        std::string sInput;
        while (getline( std::cin, sInput )) {  // added `while` to process all input
            std::istringstream parse( sInput );
            double tmp;
            while ( parse >> tmp )
                numbers.push_front( tmp );
        }
        std::for_each(numbers.begin(), numbers.end(), sqrt_f);
        return 0;
    }
    

    Then, I’m going to ignore the question you actually asked (about correctness) and just review the (rewritten) code, assuming that it meets the requirements.

    void sqrt_f(double n)
    {
        printf ( "%.4f\n", sqrt( static_cast<double>( n ) ) );
    }
    

    I see three things here I don’t particularly like:

    1. Using printf in C++. iostreams are generally preferred (though as we’ll see, they are more verbose in this case).
    2. The static_cast seem to be entirely extraneous (n is already a double).
    3. Poor separation of concerns. You’re combining computation with I/O.

      std::list<double> numbers;

    I don’t see much reason to use std::list here. You’re only inserting at one end, and traversing beginning to end. The primary reason to use std::list is if you’re going to insert/delete somewhere in the middle of the list. Even then it’s not necessarily the best choice, but when you only need to insert at one end, it’s almost certainly the wrong choice.

    std::string sInput;
    while (getline( std::cin, sInput )) {  // added `while` to process all input
        double tmp;
        while ( parse >> tmp )
            numbers.push_front( tmp );
    }
    

    I don’t see any real reason to use std::getline here. getline is useful primarily when your input has line-oriented formatting, but here it’s just a stream of doubles separated by some arbitrary amount of white space. For that, you appear to have added extra complexity without gaining much functionality in return.

    I’m also less than excited about the loop while (parse >> tmp). It’s certainly better than some alternatives, but I’d rather use std::copy with (in this case) an std::istream_iterator<double> to copy the values into a collection.

    I’m not particularly excited about how you’re using the collection here either. First of all, you’re reversing the order by using push_front. At least to me, it seems like it would be easier to understand if you added the numbers to the collection in their original order, then walked through the collection backwards.

        std::for_each(numbers.begin(), numbers.end(), sqrt_f);
    

    …and here’s the culprit responsible for the poor separation of responsibility in sqrt_f. It’s been something like 15 years since the STL came to C++, and in that time I doubt I’ve seen as many as half a dozen good uses of std::for_each. It can be useful, but should generally be your last choice of algorithms, when you just can’t find anything else that works better.

    I think if I were doing this, I’d use an std::vector for the storage (we only need to add items to one end, which fits what vector provides). I’d copy the data directly from std::cin into that vector using std::copy with a std::istream_iterator<double>. I’d then use std::transform with a pair of reverse iterators over the vector, and an ostream_iterator for the output. Since both the fixed flag and the precision are “sticky”, we can get all the output in the correct format by setting the format once before we call std::transform:

    std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
    std::cout.precision(4);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a problem in VBA text box while filling input mask property: I
Atleast defining my problem is simple... I have a Input control (Text) and a
Problem: I've a huge raw text file (assume of 3gig), I need to go
Problem: How can I tell if a selection of text in the CRichEditCtrl has
I have problem when I try insert some data to Informix TEXT column via
A recent problem* left me wondering whether there is a text editor out there
I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
Problem is described and demonstrated on the following links: Paul Stovell WPF: Blurry Text
An XSLT-newbie problem: I need to substitute a text value in XML-file. All other
I’m working on the UVa Online Judge problem set archive as a way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.