Desafio 1 – Escapada Ninja
Enunciado
Resolução
#include <stdio.h> #include <string.h> int main(){ char instrucoes[20]; int N; scanf("%d", &N); while(N--){ scanf("%s", instrucoes); int tam = strlen(instrucoes); for(int i = 0; i<tam; i++){ switch (instrucoes[i]) { case 'D': printf("Rolada tatica ninja.\n"); break; case 'E': printf("Esquerdei, e agora?\n"); break; case 'C': printf("Eita pipoco. Zumm!\n"); break; case 'B': printf("\n"); break; case 'F': printf("Pronto e agora, e agora?\n"); break; case 'A': printf("Nem ferrando. Vou de agentes mesmo.\n"); break; } } } }
Desafio 2 – Recebendo Mensagens
Enunciado
Resolução
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> int main(){ setlocale (LC_ALL, ""); char alfabeto[6][5] = { //matriz de char igual a do enunciado {'A','B','C','D','E'}, {'F','G','H','I','J'}, {'K','L','M','N','O'}, {'P','Q','R','S','T'}, {'U','V','W','X','Y'}, {'Z',',','.','?','!'}}; int n; scanf("%i\n", &n); fflush(stdin); int joke; for(joke=0; joke<n; joke++){ char frase[101]; char descod[100] = {}; scanf("%[^\n]\n", &frase); fflush(stdin); int i, j; j=0; for(i=0; i<strlen(frase)-1; i+=2){ if(frase[i] == ' '){ //se tiver espaço na string apenas repete ele descod[j] = ' '; i++; //pula o index, pois era um espaço j++; } descod[j] = alfabeto[(frase[i]-'0')][(frase[i+1]-'0')]; //joga o char no array descodificado j++; } printf("%s\n", descod); } return 0; }
Desafio 3 – Tic-Tac de cereja ou menta
Enunciado
Resolução
#include <stdio.h> #include <malloc.h> char* verificaGanhou(char** tabuleiro, char jogador){ //linhas if(tabuleiro[0][0]==jogador && tabuleiro[0][1]==jogador && tabuleiro[0][2]==jogador) return "11-12-13"; else if(tabuleiro[1][0]==jogador && tabuleiro[1][1]==jogador && tabuleiro[1][2]==jogador) return "21-22-23"; else if(tabuleiro[2][0]==jogador && tabuleiro[2][1]==jogador && tabuleiro[2][2]==jogador) return "31-32-33"; //colunas else if(tabuleiro[0][0]==jogador && tabuleiro[1][0]==jogador && tabuleiro[2][0]==jogador) return "11-21-31"; else if(tabuleiro[0][1]==jogador && tabuleiro[1][1]==jogador && tabuleiro[2][1]==jogador) return "12-22-32"; else if(tabuleiro[0][2]==jogador && tabuleiro[1][2]==jogador && tabuleiro[2][2]==jogador) return "13-23-33"; //diagonais else if(tabuleiro[0][0]==jogador && tabuleiro[1][1]==jogador && tabuleiro[2][2]==jogador) return "11-22-33"; else if(tabuleiro[0][2]==jogador && tabuleiro[1][1]==jogador && tabuleiro[2][0]==jogador) return "13-22-31"; else return ""; } void preencheTabuleiro(char** tabuleiro, char* mudancas){ int lin, col; for(int i = 0; i < 9; i+=3){ lin = mudancas[i]-'1'; col = mudancas[i+1]-'1'; tabuleiro[lin][col] = 'X'; } } void imprimeTabuleiro(char** tabuleiro){ for(int i = 0; i<3; i++){ printf("%c %c %c\n", tabuleiro[i][0], tabuleiro[i][1], tabuleiro[i][2]); } } int main(){ char ** tabuleiro = (char**) malloc(sizeof(char*)*3); tabuleiro[0] = (char*) malloc(sizeof(char)*3); tabuleiro[1] = (char*) malloc(sizeof(char)*3); tabuleiro[2] = (char*) malloc(sizeof(char)*3); int N; scanf("%d", &N); for(int i = 0; i<N; i++){ for(int j = 0; j<3; j++){ scanf(" %c %c %c", &tabuleiro[j][0], &tabuleiro[j][1], &tabuleiro[j][2]); } char* resp = verificaGanhou(tabuleiro, 'O'); if(resp!=""){ printf("Tic-Tac de cereja eh bom tambem\n"); continue; } resp = verificaGanhou(tabuleiro, 'V'); if(resp!=""){ printf("Hora de tirar o bafo\n%s\n", resp); preencheTabuleiro(tabuleiro, resp); imprimeTabuleiro(tabuleiro); continue; } printf("Bora outra valendo os dois Tic-Tac ?\n"); } return 0; }
Desafio 4 – Desviando de balas
Enunciado
Resolução
#include <stdio.h> int main(){ int e; //numero de execucoes scanf("%i", &e); int n; //numero de balas em cada entrada scanf("%i", &n); for(int k = 0; k < e; k++){ int ent[n]; //balas da entrada for(int i = 0; i < n; i++){ scanf("%i", &ent[i]); } int arr[n]; //arranjo copia para poder comparar a posição original depois de ordenar for(int i = 0; i < n; i++){ arr[i] = ent[i]; } for(int i = 0; i < n; i++){ //bubble sort para ordenar o arranjo em ordem crescente for(int j = 0; j < n-1; j++){ if(arr[j] > arr[j+1]){ int aux = arr[j]; arr[j] = arr[j+1]; arr[j+1] = aux; } } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(arr[i] == ent[j]){ //comparo os elemrntos em ordem crescente com o arranjo original para descobrir a prosição printf("%i-%i ", arr[i], j); } } } printf("\n"); } return 0; }