
#include <iostream>
using namespace std;
int main(){
int val;
cin >> val;
for (int i=1; i<=val; i++)
{
for (int j = 1; j<= i; j++)
{
if (j == i)
{
cout << "*" << endl;
}
else
{
cout << "*";
}
}
}
}
C++에서 표준 입출력 스트림 라이브러리를 제공하는 헤더 파일입니다.
- std::cout: 화면에 텍스트를 출력합니다 (C의 printf와 비슷).
- std::cin: 사용자로부터 입력을 받습니다 (C의 scanf와 비슷).
- std::endl: 줄 바꿈을 처리합니다.
- std::cerr: 에러 메시지를 출력합니다.
- std::fstream: 파일 입출력을 처리합니다.
namespace에 변수와 함수를 정의할 수 있습니다. namespace에 정의된 함수를 사용할때 std::cout과 같이 (namespace 이름)::(함수)와 같은 꼴로 함수를 불러내어 사용합니다. 이러한 namespace를 통해 함수를 모듈화 시킬 수 있고 다름 변수명이 겹치는 상황에서 자유로워질 수 있습니다. 또한 using을 사용하여 함수명만으로 함수를 호출할 수 있습니다.
namespace란?

#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
vector<int> vec;
while (1){
int n[3] = {};
cin >> n[0] >> n[1] >> n[2];
cin.ignore();
sort(n, n+3);
if (n[0] == 0 && n[1] == 0 && n[2] ==0)
{
break;
}
vec.push_back(n[0]);
vec.push_back(n[1]);
vec.push_back(n[2]);
//cout << "출력" << n[0] << n[1] << n[2] << endl;
}
for (int i = 0; i < vec.size(); i += 3)
{
if (pow(vec[i], 2) + pow(vec[i+1],2) == pow(vec[i+2],2))
{
cout << "right" << endl;
}
else
{
cout << "wrong" << endl;
}
}
return 0;
}
#include <algorithm>sort 함수
- 인수 1 : 배열의 첫 주소
- 인수 2 : 배열의 마지막 주소
sort 알고리즘
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main(){
int n, a, b, c, d, e, f, alpha, beta;
cin >> n;
cin >> a >> b >> c >> d >> e >> f;
cin >> alpha >> beta;
int n_t = 0;
int clothes_size[] = {a, b, c, d, e, f};
for (int i = 0; i < 6; i++)
{
if (clothes_size[i] % alpha != 0)
{
n_t += 1;
}
n_t += clothes_size[i] / alpha;
}
cout << n_t << endl;
cout << n/beta << " " << n%beta;
}
배열 초기화 방법으로는 int a[] = {1,23,45,2};와 같은 식으로 넣어주는 값을 {}안에 직접 넣어주거나 int a[4];와 같이 크기만 정해주는 방법이 있습니다.

#include <iostream>
#include <vector>
#include <math.h>
#include <string>
#include <sstream>
using namespace std;
int main(){
int num;
string temp;
cin >> num;
cin.ignore();
getline(cin, temp, '\n');
vector<int> vals;
string word;
stringstream ss(temp);
while (getline(ss, word, ' '))
{
int num;
stringstream val(word);
val >> num;
vals.push_back(num);
}
int count=0;
bool is_sosu = true;
for (int i = 0; i < num; i++)
{
is_sosu = true;
for (int j = 2; j < 1000; j++)
{
if (vals[i] == 1)
{
is_sosu = false;
break;
}
if (j == vals[i])
{
continue;
}
if (vals[i]%j==0)
{
is_sosu = false;
break;
}
}
if (is_sosu)
{
count++;
}
}
cout << count;
}

#include <iostream>
#include <vector>
using namespace std;
int main()
{
int card_num, card_sum;
cin >> card_num >> card_sum;
vector<int> numbers(0);
while(1)
{
int num=0;
cin >> num;
numbers.emplace_back(num);
card_num -= 1;
if (card_num<=0)
{
break;
}
}
int sum = 0;
for (int i = 0; i<numbers.size()-2; i++ )
{
for (int j = i+1; j < numbers.size()-1; j++)
{
for (int k = j+1; k < numbers.size(); k++)
{
int first = numbers[i];
int second = numbers[j];
int third = numbers[k];
int sum_temp = first + second + third;
if (card_sum-sum_temp<0){continue;}
if (sum <= sum_temp && sum_temp <= card_sum)
{
sum = sum_temp;
}
}
}
}
cout << sum;
}

빈 벡터를 정의하고 두번재 줄의 card num을 emplace_back함수를 통해 벡터의 뒷부분에 추가해주었습니다. 이때 emplace_back함수는 push_back함수와 하는일은 똑같지만 복사연사자를 불러오지 않기 때문에 시간과 메모리면에서 우수합니다. 3중 for문을 통해 숫자사이의 세가지 합의 조합중 card_sum 숫자와 가장 가까우면서 넘지 않는 sum을 나타낼 수 있습니다..
Improved by chatgpt
대부분 sort함수를 통해 vector내의 카드숫자들을 작은순서대로 오름차순으로 정리한 것이 차이점이였습니다. 이를 통해 카드의 수가 많아질때 더 효율적으로 카드사이의 조합을 순회할 수 있습니다.
sort(cards.begin(), cards.end());