https://www.acmicpc.net/problem/6997
๋ฌธ์
After a long trek through treacherous mountains, Bilbo and the dwarfs have arrived at the West Gate of the mines of Puzzlia, constructed by a mathematically inclined Dwarf King. The gate is of course not open (nothing can be that easy for our heroes), and Dwalin explains to Bilbo that they need to guess a 4-digit number (let’s call it “secret”) correctly to open the gate. He points out 8 geometrical shapes on top of the gate, 4 circles and 4 squares. Every time a guess is made (by standing in front of the gate and speaking each of the digits loudly), some of the squares and the circles will light up. If the guess is correct, then the gate will open; but if the number is not guessed correctly in 10 attempts, then... well, best not to think of such things.
Dwalin explains the rules about how the circles and squares will light up. Let guess denote a guess that is made. For every digit in guess: (1) if it is equal to the corresponding digit in secret (at the same position), then a circle will light up; (2) if it is equal to some digit in secret but at a different position, a square will light up. For example:
- secret = 1234, guess = 1357: The “1” in guess gives us one circle, whereas the “3” in guess gives us one square. The other two digits in guess do not have a match.
The secret (and the guess) may contain duplicate digits. The rule for handling duplicates is simple: a digit in secret can only match one digit in guess (and vice versa), and exact matches supersede out-of-position matches. So we first check for exact matches, and then look for the second type.
- secret = 1234, guess = 1122: There is only one exact match, for position 1 (that gives us one circle). The second “1” in guess doesn’t match anything in secret (since the “1” in secret is already matched). Then, we get one square for the first “2” in guess, and that’s it.
- secret = 1311, guess = 1122: One circle for exact match at position 1, and one square for an out-of-position match for digit “1”.
- secret = 0011, guess = 0213: Two circles, no squares.
Before trying to guess the secret of the gate, Bilbo would like to make sure he understands the process and develops some strategy. He would like your help with this. Specifically, he would like you to write a program that, given two numbers, tells him how many circles and squares would light up.
์ ๋ ฅ
The first line in the test data file contains the number of test cases (< 100). After that, each line contains one test case: the first number is the secret (provided as an int), and the following number is the guess (provide as an int). Both the numbers are ≤ 9999. Note that the numbers need to be padded with 0’s. For example, if one of the numbers is 1, it needs to be treated as 0001.
์ถ๋ ฅ
For each test case, you are to output the numbers of circles and squares that will light up. Exact format shown below.
ํด์ค
ํ์ด
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
int n;
cin >> n;
for(int i=0; i<n; i++){
int circle = 0, square = 0;
char secret[5], guess[5];
cin >> secret >> guess;
string secret_origin = secret;
string guess_origin = guess;
//์
for(int j=0; j<4; j++){
if(secret[j] == guess[j]){
circle++;
//์์น์ ๋ชจ์ ๋ชจ๋ ์ผ์นํ๋ฉด x๋ก ๋ฐ๊ฟ์ ์ดํ๋ก ์นด์ดํธ ๋๋๊ฒ์ ๋ฐฉ์งํ๋ค
secret[j] = 'x';
guess[j] = 'x';
}
}
for(int j=0; j<4; j++){
if(secret[j] != 'x'){
for(int k=1; k<=3; k++){
if(secret[j] == guess[(j+k)%4]){
square++;
secret[j] = 'x';
guess[(j+k)%4] = 'x';
break;
}
}
}
}
cout << "For secret = " << secret_origin << " and guess = " << guess_origin << ", " << circle << " circles and " << square << " squares will light up.\n";
}
return 0;
}
'๐ฆ Chango > ๐ฃ EDOC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ][C++] ๋ฐฑ์ค 1259๋ฒ: ํฐ๋ฆฐ๋๋กฌ์ (0) | 2021.09.06 |
---|---|
[C++][BOJ] ๋ฐฑ์ค 15366๋ฒ: Olivander (0) | 2021.08.25 |
[Android Studio / Kotlin] ์ฝํ๋ฆฐ๊ณผ ๋ฆฌ์คํธ๋ทฐ (List View) (0) | 2021.05.02 |
[Android Studio / Kotlin] ์ค์ ์ฑ๊ฐ๋ฐ์์ ์ฌ์ฉํ ์ฝํ๋ฆฐ๋ง ๋ชจ์๋ณด๊ธฐ (๊ธฐ๋ณธ) (0) | 2021.05.02 |
[AndroidStudio / Kotlin] Unresolved reference ์ค๋ฅ ํด๊ฒฐ (0) | 2021.05.01 |