#include <iostream> #include <vector> #include <algorithm> #include <random> std::vector<int> MakeRandomArray(std::size_t N){ std::random_device rd; std::mt19937 mt(rd()); //std::uniform_int<> ui(0, N); std::vector<int> vec(N); //for (auto& o : vec) o = ui(mt); for (auto& o : vec) o = mt()%N+1;//don't write like this code!! std::sort(vec.begin(), vec.end()); return vec; } int CountN(std::vector<int> vec){ int N = vec.size(); int C = 0; for (std::size_t i = 0; i < vec.size(); i++) { if (vec[i] == N) C++; } return C; } int main(){ int N = 100; auto V = MakeRandomArray(N); auto R = CountN(V); for (auto& o : V) std::cout << o << ','; std::cout << std::endl; std::cout << "よくわからんけど" << R << "個あったような気がする。" << std::endl; return 0; }
Standard input is empty
1,4,5,5,7,7,9,11,11,14,14,15,17,17,18,18,18,19,22,22,23,23,23,25,25,26,27,28,28,28,28,29,29,30,30,30,31,32,32,32,33,34,34,35,39,40,40,45,46,47,51,52,53,54,57,58,59,59,60,60,62,64,65,67,67,69,69,71,73,74,75,75,76,77,77,77,78,78,79,80,81,83,84,85,85,87,87,88,89,89,90,91,93,94,94,97,97,98,98,100, よくわからんけど1個あったような気がする。