Naohiro19

C++で3の倍数を数えるプログラム

Jan 20th, 2022
1,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <numeric>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8.     std::vector<int> v(101);
  9.     std::vector<int> result;
  10.     int count = 0;
  11.  
  12.     std::iota(v.begin(), v.end(), 1);
  13.     for (const auto& e : v) {
  14.         if (e % 3 == 0) {
  15.             result.push_back(e);
  16.             count++;
  17.         }
  18.     }
  19.     std::cout << "3の倍数の個数は" << count << "個" << std::endl;
  20.     // 表示結果: "3の倍数の個数は33個"
  21. }
Advertisement
Add Comment
Please, Sign In to add comment