Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ';使用名称空间std';正在引起一种奇怪的行为_C++_C++11_Namespaces_C++14_Format Specifiers - Fatal编程技术网

C++ ';使用名称空间std';正在引起一种奇怪的行为

C++ ';使用名称空间std';正在引起一种奇怪的行为,c++,c++11,namespaces,c++14,format-specifiers,C++,C++11,Namespaces,C++14,Format Specifiers,我在同一组输入上运行了以下两个代码片段,除了第二个输入中使用了“namespace std”之外,其余都是相同的。我无法理解他们的产出不同的原因。有人能指引我吗?谢谢 方案1: #include<cmath> #include<cstdio> //using namespace std; int main() { int i, p, q, r, s; while (scanf("%d %d %d %d", &p, &q, &r, &

我在同一组输入上运行了以下两个代码片段,除了第二个输入中使用了“namespace std”之外,其余都是相同的。我无法理解他们的产出不同的原因。有人能指引我吗?谢谢

方案1:

#include<cmath>
#include<cstdio>
//using namespace std;
int main() {
    int i, p, q, r, s;
    while (scanf("%d %d %d %d", &p, &q, &r, &s) == 4) {
        long double ans = 0;
        if (p - q < q)
            q = p - q;
        if (r - s < s)
            s = r - s;
        for (i = 1; i <= q; i++)
            ans += log(p - q + i) - log(i);
        for (i = 1; i <= s; i++)
            ans -= log(r - s + i) - log(i);
        printf("%.5lf\n", exp(ans));
    }
    return 0;
}

方案2:

#include<cmath>
#include<cstdio>
using namespace std;
int main() {
    int i, p, q, r, s;
    while (scanf("%d %d %d %d", &p, &q, &r, &s) == 4) {
        long double ans = 0;
        if (p - q < q)
            q = p - q;
        if (r - s < s)
            s = r - s;
        for (i = 1; i <= q; i++)
            ans += log(p - q + i) - log(i);
        for (i = 1; i <= s; i++)
            ans -= log(r - s + i) - log(i);
        printf("%.5lf\n", exp(ans));
    }
    return 0;
}

输入:

10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
编辑: 1) 将#包括“bits/stdc++.h”替换为标准标题。
2) 用实际的代码片段替换图像。

在摆弄
日志()
并怀疑
操作符+=()
之后,我终于解决了这个难题

实际错误是由
printf(“%.5lf\n”,…)
引起的

我稍微修改了示例代码以演示:

#include<cmath>
#include<cstdio>
#include<iomanip>
#include<iostream>
using namespace std;
int main() {
    int i, p, q, r, s;
    while (scanf("%d %d %d %d", &p, &q, &r, &s) == 4) {
        long double ans = 0;
        if (p - q < q)
            q = p - q;
        if (r - s < s)
            s = r - s;
        for (i = 1; i <= q; i++)
            ans += log(p - q + i) - log(i);
        for (i = 1; i <= s; i++)
            ans -= log(r - s + i) - log(i);
        printf("printf: %.5lf", exp(ans));
        cout << "\tcout: " << fixed << setprecision(5) << exp(ans) << endl;
    }
    return 0;
}
我查过了。 当我在Windows 10(64位)上使用VS2013编译和运行此文件时,得到的输出相同

我也在Windows 10(64位)上的cygwin中使用gcc进行了检查,并获得了以下输出:

$ g++ -std=c++11 -o test-longdouble test-longdouble.cc ; echo '10 5 14 9
> 93 45 84 59
> 145 95 143 92
> 995 487 996 488
> 2000 1000 1999 999
> 9998 4999 9996 4998
> ' | ./test-longdouble
printf: -0.00000        cout: 0.12587
printf: -4234002535919089587818586571373347278663379000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000        cout: 505606.46055
printf: -0.00000        cout: 1.28223
printf: -65094314467486612925155033958017324735054040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000     cout: 0.48996
printf: nan     cout: 2.00000
printf: nan     cout: 3.99960

$ g++ -std=c++14 -o test-longdouble test-longdouble.cc ; echo '10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
' | ./test-longdouble
printf: -0.00000        cout: 0.12587
printf: -4234002535919089587818586571373347278663379000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000        cout: 505606.46055
printf: -0.00000        cout: 1.28223
printf: -65094314467486612925155033958017324735054040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000     cout: 0.48996
printf: nan     cout: 2.00000
printf: nan     cout: 3.99960

$ g++ --version
g++ (GCC) 5.4.0
我第一眼没有看到它,但是
cout
输出是正确的,只是
printf
输出不同

考虑到
printf
格式化程序选择错误,您不应该对其效果有任何期望

然后我修复了
printf()
中的错误格式:


<>所以,我只能重复我在我的评论中已经说过的话:你真的应该使用C++流输出。代码> Prtff()/Cyto>由于格式化程序中的最轻微错误,可能会导致奇怪的行为。

并包含适当的标头,因此我们有一个标准的C++程序,可以正确地分析。@ Piykr,我相信在<代码> PrtTf< /C>中的格式是正确的。你看流媒体运营商真的有进步。毫无冒犯之意,为什么这个问题会被否决?答案是如此明显以至于不值得提问,还是我没有在我得到的指导下投入足够的努力来研究这个问题?张贴代码图片是不受欢迎的。发布代码,而不是图片。@piyukr:是什么阻止您使用CTRL+C复制输出?即使ideone.com确实阻止您将输出复制为文本(事实并非如此),也并不意味着您无法将自己的源代码复制并粘贴为文本。您的代码在ideone上运行良好。但是,当我在计算机上运行时,输出不正确。见@piyukr OK。在代码中,您没有使用标准头。这能解释不同的行为吗?对不起,这是个错误。我再次运行了这个程序,这次使用了标准头。看@piyukr我在cygwin身上做了同样的事。那又怎么样?我试过
-std=c++11
-std=c++14
。没有区别。我的gcc版本:5.4.0。表意文字:6.3。这可能是区别吗?@piyukr我也用VS2013(部分是C++11、Windows 10、64位)测试了这一点。这就像我在回答中所说的那样。我们是否发现了gcc<6.3(或相应的标准库)中的错误?
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<iostream>
using namespace std;
int main() {
    int i, p, q, r, s;
    while (scanf("%d %d %d %d", &p, &q, &r, &s) == 4) {
        long double ans = 0;
        if (p - q < q)
            q = p - q;
        if (r - s < s)
            s = r - s;
        for (i = 1; i <= q; i++)
            ans += log(p - q + i) - log(i);
        for (i = 1; i <= s; i++)
            ans -= log(r - s + i) - log(i);
        printf("printf: %.5lf", exp(ans));
        cout << "\tcout: " << fixed << setprecision(5) << exp(ans) << endl;
    }
    return 0;
}
printf: 0.00000 cout: 0.12587
printf: 0.00000 cout: 505606.46055
printf: 0.00000 cout: 1.28223
printf: 0.00000 cout: 0.48996
printf: 0.00000 cout: 2.00000
printf: 0.00000 cout: 3.99960
$ g++ -std=c++11 -o test-longdouble test-longdouble.cc ; echo '10 5 14 9
> 93 45 84 59
> 145 95 143 92
> 995 487 996 488
> 2000 1000 1999 999
> 9998 4999 9996 4998
> ' | ./test-longdouble
printf: -0.00000        cout: 0.12587
printf: -4234002535919089587818586571373347278663379000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000        cout: 505606.46055
printf: -0.00000        cout: 1.28223
printf: -65094314467486612925155033958017324735054040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000     cout: 0.48996
printf: nan     cout: 2.00000
printf: nan     cout: 3.99960

$ g++ -std=c++14 -o test-longdouble test-longdouble.cc ; echo '10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
' | ./test-longdouble
printf: -0.00000        cout: 0.12587
printf: -4234002535919089587818586571373347278663379000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000        cout: 505606.46055
printf: -0.00000        cout: 1.28223
printf: -65094314467486612925155033958017324735054040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000     cout: 0.48996
printf: nan     cout: 2.00000
printf: nan     cout: 3.99960

$ g++ --version
g++ (GCC) 5.4.0
#include<cmath>
#include<cstdio>
#include<iomanip>
#include<iostream>
using namespace std;
int main() {
  int i, p, q, r, s;
  while (scanf("%d %d %d %d", &p, &q, &r, &s) == 4) {
    long double ans = 0;
    if (p - q < q)
      q = p - q;
    if (r - s < s)
      s = r - s;
    for (i = 1; i <= q; i++)
      ans += log(p - q + i) - log(i);
    for (i = 1; i <= s; i++)
      ans -= log(r - s + i) - log(i);
    printf("printf: %.5Lf", exp(ans));
    cout << "\tcout: " << fixed << setprecision(5) << exp(ans) << endl;
  }
  return 0;
}
$ g++ -std=c++14 -o test-longdouble test-longdouble.cc ; echo '10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
' | ./test-longdouble
printf: 0.12587 cout: 0.12587
printf: 505606.46055    cout: 505606.46055
printf: 1.28223 cout: 1.28223
printf: 0.48996 cout: 0.48996
printf: 2.00000 cout: 2.00000
printf: 3.99960 cout: 3.99960