SMALL
문제
4명의 Player 1, 2, 3, 4가 순서를 돌아가며 369 게임을 진행한다.
1부터 100까지의 범위에서 30의 배수는 "ahh"를 출력, 3의 배수는 "clap"을 출력, 10의 배수는 "rool"을 출력, 나머지는 숫자를 출력한다.
clap, rool, ahh의 총 개수를 출력한다.
게임당 clap을 했는지 출력한다.
가장 많은 clap을 한 player를 출력한다.
실행 코드
void main() {
// 변수 초기화
int totalClaps = 0;
Map<String, int> playerClaps = {'Player 1': 0, 'Player 2': 0, 'Player 3': 0, 'Player 4': 0};
List<String> players = playerClaps.keys.toList();
int currentPlayerIndex = 0;
// 369 게임을 위해 1부터 100까지 반복하는 for문
for (int i = 1; i <=100; i++) {
// 인덱스를 사용하여 현재 플레이어를 결정
String currentPlayer = players[currentPlayerIndex];
bool isClap = false;
// 30, 3, 10의 배수인지 확인 및 출력하는 if 조건문
if (i % 30 == 0) {
print("$currentPlayer: ahh");
} else if (i % 3 == 0) {
print("$currentPlayer: clap");
isClap = true; // 3의 배수에 isClap
} else if (i % 10 == 0){
print("$currentPlayer: rool");
} else {
print("$currentPlayer: $i");
}
// 3의 배수에 isClap True. totalClap 1씩 증가
if (isClap) {
totalClaps++;
// 현재 플레이어의 박수 횟수가 null이면 0 아니면 1씩 증가
playerClaps[currentPlayer] = (playerClaps[currentPlayer] ?? 0) + 1;
}
// currentPlayerIndex를 다음 플레이어로 저장. Player1부터 2, 3, 4까지 한 번 끝나면(Player4, players[3] 진행 후) players[0]인 Player1로 순환.
currentPlayerIndex = (currentPlayerIndex + 1) % players.length;
}
// 전체 박수 횟수 출력
print("\nTotal claps: $totalClaps");
// 가장 많은 박수를 친 플레이어 찾기
String mostClapsPlayer = "";
int maxClaps = 0;
playerClaps.forEach((player, claps) {
if (claps > maxClaps) {
mostClapsPlayer = player;
maxClaps = claps;
}
});
// 가장 많은 박수를 친 플레이어 출력
print("Player with most claps: $mostClapsPlayer");
}
실행 결과
Player 1: 1
Player 2: 2
Player 3: clap
Player 4: 4
Player 1: 5
Player 2: clap
Player 3: 7
Player 4: 8
Player 1: clap
Player 2: rool
Player 3: 11
Player 4: clap
Player 1: 13
Player 2: 14
Player 3: clap
Player 4: 16
Player 1: 17
Player 2: clap
Player 3: 19
Player 4: rool
Player 1: clap
Player 2: 22
Player 3: 23
Player 4: clap
Player 1: 25
Player 2: 26
Player 3: clap
Player 4: 28
Player 1: 29
Player 2: ahh
Player 3: 31
Player 4: 32
Player 1: clap
Player 2: 34
Player 3: 35
Player 4: clap
Player 1: 37
Player 2: 38
Player 3: clap
Player 4: rool
Player 1: 41
Player 2: clap
Player 3: 43
Player 4: 44
Player 1: clap
Player 2: 46
Player 3: 47
Player 4: clap
Player 1: 49
Player 2: rool
Player 3: clap
Player 4: 52
Player 1: 53
Player 2: clap
Player 3: 55
Player 4: 56
Player 1: clap
Player 2: 58
Player 3: 59
Player 4: ahh
Player 1: 61
Player 2: 62
Player 3: clap
Player 4: 64
Player 1: 65
Player 2: clap
Player 3: 67
Player 4: 68
Player 1: clap
Player 2: rool
Player 3: 71
Player 4: clap
Player 1: 73
Player 2: 74
Player 3: clap
Player 4: 76
Player 1: 77
Player 2: clap
Player 3: 79
Player 4: rool
Player 1: clap
Player 2: 82
Player 3: 83
Player 4: clap
Player 1: 85
Player 2: 86
Player 3: clap
Player 4: 88
Player 1: 89
Player 2: ahh
Player 3: 91
Player 4: 92
Player 1: clap
Player 2: 94
Player 3: 95
Player 4: clap
Player 1: 97
Player 2: 98
Player 3: clap
Player 4: rool
Total claps: 30
Player with most claps: Player 3
'IT.모바일 > 배움' 카테고리의 다른 글
클래스와 상속 super (0) | 2023.11.29 |
---|---|
Class, Abstract, Mixin, Interface 특징 개념 (0) | 2023.11.29 |
Null Safety, null 합류 연산자, late, final, const, 연산자, 조건문, 반복문, List, Map, 함수, 메서드, positional/named parameter | Flutter 문법 (0) | 2023.11.27 |
[패캠 태블로] 지도에 따른 시각화 | Ch.9 (0) | 2023.07.22 |
[패캠 태블로] 속성별 비교를 위한 시각화 KPI, Highlight Table, Bullet Table (0) | 2023.07.21 |
댓글