C++ 一个acm问题。什么';我的密码有错吗?

C++ 一个acm问题。什么';我的密码有错吗?,c++,C++,问题案文: 输入:输入流包含一组整数Ai(0≤ 艾岛≤ 1018). 数字由任意数量的空格和换行符分隔。输入流的大小不超过256 KB 输出:对于从最后一个到第一个的每个数字Ai,应输出其平方根。每个平方根应单独打印一行,小数点后至少有四位数字 样本 输入: 1427 0 876652098643267843 5276538 输出: 2297.0716 936297014.1164 0 37.7757 时限:2.0秒 内存限制:16 MB >tmp) 数字。向前推(tmp); std::for_

问题案文:

输入:输入流包含一组整数Ai(0≤ 艾岛≤ 1018). 数字由任意数量的空格和换行符分隔。输入流的大小不超过256 KB

输出:对于从最后一个到第一个的每个数字Ai,应输出其平方根。每个平方根应单独打印一行,小数点后至少有四位数字

样本
输入: 1427 0

876652098643267843 5276538

输出: 2297.0716 936297014.1164 0 37.7757

时限:2.0秒
内存限制:16 MB

<32页的C和C++程序由32位微软Visual C++ 2010在服务器上编译。直到2010年8月3日,英特尔C++编译器7被使用。您可以在此页面下载免费的Microsoft Visual Studio 2010 Express。使用以下参数调用编译器:

// 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" //C cl/TC/MT/EHsc/O2/W3/Za/D“\u CRT\u安全\u无警告” /D“\u CRT\u SECURE\u NO\u DEPRECATE”/D“ONLINE\u JUDGE” //C++ cl/TP/MT/EHsc/O2/W3/Za/D“\u CRT\u安全\u无警告” /D“\u CRT\u SECURE\u NO\u DEPRECATE”/D“ONLINE\u JUDGE” 这是我在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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
无效sqrt_f(双n)
{
printf(“%.4f\n”,sqrt(静态_-cast(n));
}
int main()
{
std::列表编号;
std::字符串输入;
getline(标准::cin,sInput);
std::istringstream解析(sInput);
双tmp;
while(解析>>tmp)
数字。向前推(tmp);
std::for_each(numbers.begin()、numbers.end()、sqrt_f);
返回0;
}
但判断结果是:回答错误(test1)


问题出在哪里?

警告:当我写这篇文章时,这个问题仍然在代码审查网站上,所以它主要是作为代码审查编写的。这里主要应用的部分(解决输入问题)只是顺便提一下

目前,我假设您已经解决了@Jeff Mercado指出的问题,以获得如下代码:

#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;
}
我看不出有任何真正的理由在这里使用
std::getline
getline
主要在您的输入具有面向行的格式时非常有用,但在这里它只是一个由任意数量的空白分隔的双倍流。因此,您似乎增加了额外的复杂性,但没有获得多少功能作为回报

我对循环
(解析>>tmp)
也不太感兴趣。它当然比一些替代方法要好,但我更愿意使用
std::copy
std::istream\u迭代器(在本例中)将值复制到集合中

我对你如何使用这里的收藏也不是特别兴奋。首先,使用
push\u front
可以颠倒顺序。至少对我来说,如果你按照原始顺序将数字添加到集合中,然后向后遍历集合,似乎会更容易理解

    std::for_each(numbers.begin(), numbers.end(), sqrt_f);
…这是导致
sqrt\u f
中责任划分不当的罪魁祸首。自从STL来到C++以来,已经有15年了,在那个时候,我怀疑我已经看到了多达六打好的用法:<代码> STD::FRYEAY/<代码>。它可能很有用,但一般来说,当你找不到其他更好的算法时,它应该是你最后的选择

我想如果我这样做,我会使用
std::vector
作为存储(我们只需要在一端添加项目,这符合
vector
提供的内容)。我会使用
std::istream\u迭代器
std::copy
将数据直接从std::cin复制到该向量中。然后我将使用
std::transform
和向量上的一对反向迭代器,以及输出的
ostream\u迭代器。由于固定标志和精度都是“粘性”的,我们可以在调用
std::transform
之前设置一次格式,以正确的格式获得所有输出:

std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(4);

警告:当我写这篇文章时,这个问题仍然在代码审查网站上,所以它主要是作为代码审查来写的。这里主要应用的部分(解决输入问题)只是顺便提一下

目前,我假设您已经解决了@Jeff Mercado指出的问题,以获得如下代码:

#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;
}
我看不出有任何真正的理由在这里使用
std::getline
getline
主要在您的输入具有面向行的格式时非常有用,但在这里它只是一个由任意数量的空白分隔的双倍流。因此,您似乎增加了额外的复杂性,但没有获得多少功能作为回报

我对循环
(解析>>tmp)
也不太感兴趣。它当然比一些替代方法要好,但我更愿意使用
std::copy
std::istream\u迭代器(在本例中)将值复制到集合中

我对你如何使用这里的收藏也不是特别兴奋。首先,使用
push\u front
可以颠倒顺序。至少对我来说,如果你按照原始顺序将数字添加到集合中,然后向后遍历集合,似乎会更容易理解

    std::for_each(numbers.begin(), numbers.end(), sqrt_f);
…这是导致
sqrt\u f
中责任划分不当的罪魁祸首。自从STL来到C++以来,已经有15年了,在那个时候,我怀疑我已经看到了多达六打好的用法:<代码> STD::FRYEAY/<代码>。它可能很有用,但一般来说,当你找不到其他更好的算法时,它应该是你最后的选择

我想如果我这样做的话,我会使用
std::vector
作为存储(我们只需要
    std::for_each(numbers.begin(), numbers.end(), sqrt_f);
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(4);