새소식

💻 Programming (프로그래밍)/C++ | 백준

[C++][백준] - 줄번호 (4470번)

  • -
https://www.acmicpc.net/problem/4470
 

4470번: 줄번호

텍스트에서 줄을 입력받은 뒤, 줄 번호를 출력하는 프로그램을 작성하시오.

www.acmicpc.net

🔔 문제 : 

첫째 줄에 줄의 수 N이 주어집니다.

 

각 줄에 출력으로 줄의 수를 덧붙혀주는 문제입니다.

 

예제 입력 

5
Lionel Cosgrove
Alice
Columbus and Tallahassee
Shaun and Ed
Fido

 

예제 출력

1. Lionel Cosgrove
2. Alice
3. Columbus and Tallahassee
4. Shaun and Ed
5. Fido

🔔 Kick Point :

 

새롭게 줄의 수를 카운트하는 int 변수를 두어서 출력해줍니다.


🔔 Code :

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

int main() {
	int n, cnt(0);
	cin >> n; cin.ignore();
	string str;
	while (n--) {
		cnt++;
		getline(cin, str);
		cout << cnt << ". " << str << '\n';
	}
}

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.