冗长的是我的
#include <iostream>
#include <ctime>
int main() {
srand(unsigned (time(NULL)));
//随机500个数字
unsigned rand_no[500];
for (int i = 0;i < 500;++i) {
rand_no[i] = (rand()%998 + 2);
}
//冒泡啊冒泡
for (int y = 0,temp;y < 500;++y) {
for (int j = 499;j > y;j--) {
if (rand_no[j] > rand_no[j-1]) {
temp = rand_no[j];
rand_no[j] = rand_no[j-1];
rand_no[j-1] = temp;
}
}
}
//排序测试
// for (int x = 0;x < 500;++x){
// std::cout << " " << rand_no[x];
// }
//输出
unsigned sum = 0;
for (int x = 0;x < 10;++x) {
sum += rand_no[x];
}
std::cout << sum << std::endl;
}
这个抄考别人的#include <iostream>
#include <algorithm>
#include <ITERATOR>
#include <vector>
#include <ctime>
struct rand_from_1_to_1000 {
int operator () () {
return (rand()%998 + 2);
}
};
int main() {
srand(unsigned (time(NULL)));
std::vector <int> v;
std::generate_n(std::back_inserter(v), 500, rand_from_1_to_1000());
std::sort(v.begin(), v.end());
unsigned sum = 0;
for (int i = 499;i > 489;i--) {
sum += v[i];
}
std::cout << sum << std::endl;
}
|