打印最大的3个数字

时间:2014-11-07 05:54:40

标签: c++

我是c ++的初学者,在这个问题上遇到了障碍。我知道我在使这个程序工作方面有几道路线,但我已尽力而为。我知道它与" else"之后的作业有关。声明。我很感激你的帮助。

我必须制作一个程序,输入10个数字,最后打印出10个数字中最大的3个。这就是我现在所拥有的:

int biggest;
int second;
int third;
int number;
int c;
biggest=0, second=0;
cout<<"Enter 10 numbers: ";
for(c=0;c<=9;c++) {
    cin >>number;
    if(number>biggest) {
        third=second;
        second=biggest;
        biggest=number;
    }else{
        second=number;
        if(number<=second)
            third=second;
    }
}
cout<< "Largest number is: " << biggest << " second largest number is: " <<second <<" third largest is: " << third <<endl;

谢谢!

1 个答案:

答案 0 :(得分:-1)

link:http://ideone.com/4P8AX0

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> vec;
    int value = 0;
    for(int i=0; i<10; i++){
        cin >> value;
        vec.push_back(value);
    }

    std::sort(vec.begin(),vec.end());

    cout << "biggest" << vec[9] << endl;
    cout << "2nd biggest" << vec[8] << endl;
    cout << "3rd biggest" << vec[7] << endl;
    return 0;
}

整个节目享受