※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 6970번 문제인 Sentences이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/6970
6970번: Sentences
The first line of the input file contains a positive integer n which is the number of data sets which follow. For each of the n data sets, the data begins with three positive integers, one per line, each less than or equal to 20, which represent the number
www.acmicpc.net
문제에서 주어지는 주어, 동사, 목적어들을 나열해 만들 수 있는 모든 가능한 문장을 사전순으로 출력하는 문제이다.
각 주어, 동사, 목적어는 알파벳순으로 주어지므로 주어배열, 동사배열, 목적어배열을 순서대로 도는 3중 반복문을 이용해 모든 경우의 수를 출력하는 것으로 조건을 만족하는 답을 얻을 수 있다.
테스트케이스 사이에 공백 한 줄을 추가로 출력하라는 조건을 잊지 말고 구현하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
string A[20], B[20], C[20];
void solve() {
int a, b, c; cin >> a >> b >> c;
string tmp; getline(cin, tmp);
for (int i = 0; i < a; i++) getline(cin, A[i]);
for (int i = 0; i < b; i++) getline(cin, B[i]);
for (int i = 0; i < c; i++) getline(cin, C[i]);
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
for (int k = 0; k < c; k++) {
cout << A[i] << ' ' << B[j] << ' ' << C[k] << ".\n";
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T; cin >> T;
for (int t = 1; t <= T; t++) {
solve();
if (t < T) cout << '\n';
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 2553 // C++] 마지막 팩토리얼 수 (0) | 2022.08.28 |
---|---|
[BOJ 14645 // C++] 와이버스 부릉부릉 (0) | 2022.08.28 |
[BOJ 14646 // C++] 욱제는 결정장애야!! (0) | 2022.08.28 |
[BOJ 11104 // C++] Fridge of Your Dreams (0) | 2022.08.27 |
[BOJ 17612 // C++] 쇼핑몰 (0) | 2022.08.26 |