PS

[코드업] c++ 100제 (1021 - 1030)

kugnuoy 2024. 1. 31. 17:30

1022.

[풀이]

공백 포함하여 string 으로 읽어 싶다면 getline(cin, string name) 을 사용하면 된다.

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	string str;
	getline(cin, str);
	cout << str;
}

1023.

[풀이]

cin을 사용해도 되지만 scanf를 사용해서 double 타입으로 들어오는 수를 더 쉽게 int 타입 두 개로 나눠 받을 수도 있다.

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	int front, rear;
	scanf("%d.%d", &front, &rear);
    cout << front << '\n' << rear;
}

1024.

[풀이]

string 타입 변수를 길이만큼 출력할 때, size()와 length() 모두 사용 가능 하다.

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	string str;
	cin >> str;
	for (int i = 0; i < str.size(); i++) {
		printf("'%c'\n", str[i]);
	} 
}

1025.

[풀이]

#include "iostream"
#include <stdio.h>
using namespace std;

int main(void) {
	string a;
	cin >> a;

	int k = 10000;
	for (int i = 0; i < 5; i++) {
		printf("[%d]\n", (a[i] - '0') * k);
		k /= 10;
	}
}

1027.

[풀이]

#include "iostream"
using namespace std;

int main(void) {
	int y, m, d;
	char dash;

	cin >> y >> dash >> m >> dash >> d;

	cout.width(2);	cout.fill('0');
	cout << d << '-';

	cout.width(2);	cout.fill('0');
	cout << m << '-';

	cout.width(4);	cout.fill('0');
	cout << y;
}

1028.

[풀이]

int의 표현 범위는 -2,147,483,648 ~ +2,147,483,647 (-20억 ~ +20억) 이고

unsigned int 의 표현 범위는 0 ~ 4,294,967,295 (0 ~ 40억) 이다.

#include "iostream"
using namespace std;

int main(void) {
	unsigned int y;
	cin >> y;
	cout << y;
}

1029.

[풀이]

실수 범위에 관계없이 코딩 테스트에서는 double을 쓰는 게 더 좋다.

#include "iostream"
#include <iomanip>
using namespace std;

int main(void) {
	double y;
	cin >> y;
	cout << setprecision(11) << fixed << y;
}

1030.

[풀이]

#include <iostream>
using namespace std;

int main() {
	long long int a;
	cin >> a;
	cout << a;
}