오름캠프에서 KDT 국비지원으로 Flutter 강의를 듣게 되었다. 2일차 부터 너무 어려운 과제를 받게되어... 블로그에 정리 해본다..!
에.. 이걸 저한테 하라고여...? 알고리즘엔 약하지만 매우 흥미로운 주제였다... 팀원들과 힘을 합쳐서 진행시작 !
List
를 이용해서 players 를 만들어주었고, List<String> players = ['player1', 'player2', 'player3', 'player4']; // 플레이어 목록
int currentPlayerIndex = 0; // 현재 플레이어를 추적하기 위한 변수
// 0으로 초기화 되는 카운터
int clapCount = 0;
int roolCount = 0;
int ahhCount = 0;
//각 플레이어의 박수 횟수를 추적하기 위한 변수
List<int> clapCountsByPlayer = [0, 0, 0, 0];
for
문과 if
문을 이용하였다. 두 번째에서는 총 갯수를 구해주었고,// 1 ~ 100까지 반복하며 'clap','rool','ahh' 총 개수 출력
for (int i = 1; i <= 100; i++) {
String currentPlayer = players[currentPlayerIndex];
if (i % 30 == 0) {
print('$currentPlayer: ahh');
ahhCount++;
//30번째 반복마다 'ahh'를 출력
} else if (i % 3 == 0) {
print('$currentPlayer: clap');
clapCount++;
clapCountsByPlayer[currentPlayerIndex]++;
// 3의 배수이지만 30의 배수가 아닌 모든 반복에 대해 현재 플레이어에 대한 'clap'를 출력하고 증가시키며 각 플레이어의 'clap' 횟수도 증가시킴
} else if (i % 10 == 0) {
print('$currentPlayer: rool');
roolCount++;
//10의 배수이지만 30의 배수가 아닌 모든 반복에 대해 현재 플레이어에 대해 'rool'을 출력
} else {
print('$currentPlayer: $i');
// 위 모든 조건이 아닐 경우 현재 플레이어이름과 loop index가 출력
}
currentPlayerIndex = (currentPlayerIndex + 1) % players.length;
// 'clap', 'rool', 'ahh'의 총 횟수를 출력
}
print('\nclap의 총 갯수: $clapCount');
print('rool의 총 갯수: $roolCount');
print('ahh의 총 갯수: $ahhCount');
int maxClapCount = 0;
// 가장 많은 'clap' 을 한 플레이어를 결정하고 각 플레이어의 'clap'횟수를 출력
String playerWithMaxClap = '';
for (int i = 0; i < players.length; i++) {
int count = clapCountsByPlayer[i];
print('${players[i]}: $count');
// 각 플레이어의 'clap' 횟수 목록을 반복하여 출력
if(count > maxClapCount){
maxClapCount=count;
playerWithMaxClap = players[i];
}
}
print('\n가장 많은 clap을 출력한 플레이어: $playerWithMaxClap');
// 플레이어와 가장 많은 'clap'을 한 플레이어 출력
이번 과제에서는 팀원분들과 강사님의 도움을 많이 받았지만 첫 술에 배부를 순 없으니 점점 스스로 만들어보고 해결해가면서 성장해야겠다고 많이 느낀 과제였다!