[TIL] 211006

Jaewon·2021년 10월 6일
0

TIL

목록 보기
8/11

1. C++ 기초 연습

백준 단계별 학습 - 입출력과 사칙연산, if문 문제를 통해 C++을 처음부터 다시 복습하였다.

#include <iostream>
using namespace std;
int main(){
    cout<<"Hello World!"<< "\n";
    return 0;
}

📌 "\n"
endl를 써도 되지만 빠른 입출력을 요구하는 경우 endl 지양
-> "\n"역할 + 내부 버퍼 비우기

#include <iostream>
using namespace std;

int main(){
    cout << "강한친구 대한육군"<< "\n";
    cout << "강한친구 대한육군";
}
#include <iostream>
using namespace std;

int main(){
    double A,B;
    cin >> A >> B;
    cout.precision(12);
    cout << fixed;
    cout << A/B;
}

📌 cout.precision(n)
소수점 자릿수 조절

cout.precision(n)

-> n자리 나타내준다.
ex) n = 7 , 123.456689 -> 123.4567 , 89는 반올림 된다.

cout << fixed;
cout.precision(n)

-> 소수점 이하의 자릿수만 다룰 수 있다. 소수점 이하의 n개의 수 나타낸다.
ex) n = 7 , 123.45668915 -> 123.4566892 , 5는 반올림 된다.

cout.unsetf(ios::fixed);

cout << fixed; 해제

#include <iostream>
using namespace std;

int main(){
    int A,B;
    cin >> A >> B;
    cout << A+B << "\n";
    cout << A-B << "\n";
    cout << A*B << "\n";
    cout << A/B << "\n";
    cout << A%B;
}

이 문제의 경우 int로 출력 나오므로 int로 변수 선언하기

#include <iostream>
using namespace std;

int main(){
    int A,B,C;
    cin >> A >> B >> C;
    cout << (A+B)%C << "\n";
    cout << ((A%C) + (B%C))%C << "\n";
    cout << (A*B)%C << "\n";
    cout << ((A%C) * (B%C))%C ;

}
  • 2588번
    방법1. 수학적 계산
#include <iostream>
using namespace std;

int main(){
    int A,B;
    cin >> A >> B;
    cout << A * (B%10) << "\n";
    cout << A * ((B%100)/10) << "\n";
    cout << A * (B/100) << "\n";
    cout << A * B;
}

방법2. string이용

#include <iostream>
#include <string>
using namespace std;

int main(){
    int A;
    string B;
    cin >> A >> B;
    cout << A * (B[2] - '0') << "\n";
    cout << A * (B[1] - '0') << "\n";
    cout << A * (B[0] - '0') << "\n";
    cout << A * stoi(B);
}

char n[] = "123";
int num = n[0]; ==> num = 49 , 문자 '1'이 저장되어 아스키 코드의 '1'에 해당되는 49가 저장된다. 따라서 인덱스를 참조한 뒤 -'0'을 통해 '0'에 해당되는 48을 빼주어 1로 사용하기 위함이다.

📌 stoi()

#include <string>

-> string, stoi()을 사용하기 위한 헤더파일
stoi()
==> string -> int(10진수)

#include <iostream>
using namespace std;

int main() {
	int a, b;
	cin >> a >> b;
	if (a < b) {
		cout << "<";
	}
	else if(a == b) {
		cout << "==";
	}
	else {
		cout << ">";
	}

}
#include <iostream>
using namespace std;

int main() {
	int a;
	cin >> a;
	if ((a % 4 == 0 && a % 100 != 0)|| a % 400 == 0){
		cout << "1";
	}
	else{
		cout << "0";
	}

}
#include <iostream>
using namespace std;

int main() {
	int H, M;
	cin >> H >> M;
	if (M < 45){
		M += 15;
		if (--H < 0)
			H = 23;
	}
	else
		M -= 45;
	cout << H << " " << M;
}

2. 알고리즘 연습 (python)

0개의 댓글