小编Emi*_*mir的帖子

寻找四分位数

我编写了一个程序,用户可以在向量中输入任意数量的值,它应该返回四分位数,但我不断得到"向量下标超出范围"错误:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <ios>
#include <vector>

int main () {
    using namespace std;

    cout << "Enter a list of numbers: ";

    vector<double> quantile;
    double x;
    //invariant: homework contains all the homework grades so far
    while (cin >> x)
        quantile.push_back(x);

    //check that the student entered some homework grades
    //typedef vector<double>::size_type vec_sz;
    int size = quantile.size();

    if (size == 0) {
        cout << endl << "You must enter your numbers . "
                        "Please …
Run Code Online (Sandbox Code Playgroud)

c++ sorting vector quantile nth-element

8
推荐指数
2
解决办法
1万
查看次数

样式和命名空间

可能重复:
为什么'使用命名空间std;' 在C++中被认为是一种不好的做法?

我已经看过人们使用的一些代码示例,比如说,std::cout而在其他地方,人们using namespace std;为了简单起见而在顶部.哪个通常是首选?

c++ coding-style

6
推荐指数
1
解决办法
192
查看次数

python英镑的近似程序

我正在尝试编写一个简单的程序,打印出1:10整数的第一个Stirling近似值以及1:10阶乘的实际值.这是我的代码:

import math

nf =1 

def stirling(n):
    return math.sqrt(2*math.pi*n)*(n/math.e)**n

print "n","\t", "Stirling","\t\tFactorial"
for x in range (1,11):
    for y in range(1,x):
        nf *=y
    print x,"\t", stirling(x), "\t\t", nf
Run Code Online (Sandbox Code Playgroud)

我得到了因子的错误输出,我在哪里弄乱了代码?

python python-2.7

3
推荐指数
1
解决办法
2879
查看次数