본문 바로가기
프로그램/C++ 1000제

C++ 23제] vector 사용, 1~100까지 중 7배수의 합과 개수를 구하시오.

by 건티 2022. 6. 1.
728x90

출처 : 반크_세계유산 고인돌

 

참고풀이]

#include <iostream>
#include <vector>
#include <iomanip> //setw()
#include <numeric> //accumulate(시작위치, 끝위치, 초기값)
using namespace std;

int main()
{
   vector<int> v;
   int i;
   int Sum;

   //vector에 1~100까지 7의 배수를 저장한다.
   for(i=1;i<=100;i++) 
      if(i%7==0) v.push_back(i);

   //vector안의 값들을 더한다.
   Sum=accumulate(v.begin(),v.end(),0);

   //결과출력
   //한 줄에 10개씩 출력한다. 
   cout << "Vector 안에 있는 7배수 값들]" << endl;
   for(i=0;i<v.size();i++)
   {
      cout << setw(3);
      if((i+1)%10) cout << v[i];
      else cout << v[i] << endl;
   }
   cout << endl << endl;

   cout << "1 ~ 100까지 7배수의 개수 :  "  << v.size() << endl;
   cout << "1 ~ 100까지 7배수의   합 :  "  << Sum << endl;

   return 0;
}

 

참고풀이 결과]

 

 

 

 

대한민국의 아름다운 영토, 독도의 여름

 

반응형

댓글