#include <stdio.h> #include <wchar.h> #include <locale.h> #include <stdbool.h> #include <string.h> int extrairnaipe(int carta, int cartas[4][14]) { int naipe; if (carta >= cartas[0][0] && carta <= cartas[0][13]) naipe = 0; // espadas else if (carta >= cartas[1][0] && carta <= cartas[1][13]) naipe = 1; // copas else if (carta >= cartas[2][0] && carta <= cartas[2][13]) naipe = 2; // ouros else if (carta >= cartas[3][0] && carta <= cartas[3][13]) naipe = 3; // paus else naipe = 4; // Carta inválida return naipe; } int extrairvalor(int carta, int cartas[4][14]) { int naipe = extrairnaipe(carta, cartas); int valor=0; if (naipe != 4) { for (int i = 0; i < 14; ++i) { if (cartas[naipe][i] == carta) { valor = i; break; } } } else valor = -1; return valor; } void preencherbool(wchar_t baralho[], size_t tamanho, int boolcartas[][14], int cartas[4][14]) { for (size_t i = 0; i < tamanho; i++) { wchar_t carta = baralho[i]; int naipe = extrairnaipe(carta,cartas); int valor = extrairvalor(carta,cartas); boolcartas[naipe][valor] = 1; } } int cartamaisbaixa(int boolcartas[][14]) { int cartaMaisBaixaValor = -1; for (int valor = 0; valor < 14; valor++) { for (int naipe = 0; naipe < 4; naipe++) { if (boolcartas[naipe][valor] == 1) { cartaMaisBaixaValor = valor; return cartaMaisBaixaValor; } } } return cartaMaisBaixaValor; } int verificarconjunto(size_t tamanho, int boolcartas[][14], int cartas[4][14]) { int valor = extrairvalor(boolcartas[0][0], cartas); for (int naipe = 0; naipe < 4; naipe++) { if (boolcartas[naipe][valor] == 1) { size_t count = 1; for (size_t i = 1; i < tamanho; i++) { if (extrairvalor(boolcartas[i][0], cartas) == valor) count++; } if (count == tamanho) return 1; //é conjunto } } return 0; // Não é um conjunto } int verificarsequencia(size_t tamanho, int boolcartas[][14]) { if (tamanho < 3) { return 0; // Não é uma sequência } int cartaMaisBaixaValor = cartamaisbaixa(boolcartas); if (cartaMaisBaixaValor == -1) { return 0; // Não há cartas } size_t sequenciaCount = 1; for (int valor = cartaMaisBaixaValor + 1; valor < 14; valor++) { if (boolcartas[0][valor] == 1 || boolcartas[1][valor] == 1 || boolcartas[2][valor] == 1 || boolcartas[3][valor] == 1) sequenciaCount++; else break; } if (sequenciaCount == tamanho) return 1; // É uma sequência else return 0; // Não é uma sequência } int verificarduplasequencia(size_t tamanho, int boolcartas[][14]) { if (tamanho < 6 || tamanho % 2 != 0) { return 0; // Não é uma dupla sequência } int cartaMaisBaixaValor = cartamaisbaixa(boolcartas); if (cartaMaisBaixaValor == -1) { return 0; // Não há cartas } int contaPares = tamanho / 2; int numeroPares = 0; for (int valor= cartaMaisBaixaValor;valor<14;valor++) { for (int naipe=0; naipe<4;naipe++) { if(boolcartas[naipe][valor]==1) { for (int naipe2=naipe+1; naipe2<4;naipe2++) { if(boolcartas[naipe2][valor]==1) numeroPares++; } } } } if (numeroPares==contaPares) return 1; return 0; } int verificarcombinacoes(size_t tamanho1, int boolcartas1[][14], size_t tamanho2, int boolcartas2[][14], int cartas[4][14]){ if(tamanho1==tamanho2){ if (verificarconjunto(tamanho1,boolcartas1,cartas)==verificarconjunto(tamanho2,boolcartas2,cartas)|| verificarsequencia(tamanho1,boolcartas1)==verificarsequencia(tamanho2,boolcartas2)|| verificarduplasequencia(tamanho1,boolcartas1)== verificarduplasequencia(tamanho2,boolcartas2)){ return 1; } } return 0; } void ordenarcartas(wchar_t baralho[], size_t tamanho,int cartas[4][14]) { for (size_t i = 0; i < tamanho - 1; i++) { for (size_t j = 0; j < tamanho - i - 1; j++) { wchar_t carta1 = baralho[j]; wchar_t carta2 = baralho[j + 1]; int valor1 = extrairvalor(carta1,cartas); int valor2 = extrairvalor(carta2,cartas); int naipe1 = extrairnaipe(carta1,cartas); int naipe2 = extrairnaipe(carta2,cartas); if (valor1 > valor2) { wchar_t temp = baralho[j]; baralho[j] = baralho[j + 1]; baralho[j + 1] = temp; } else if (valor1 == valor2 && naipe1 > naipe2) { wchar_t temp = baralho[j]; baralho[j] = baralho[j + 1]; baralho[j + 1] = temp; } } } } void combinacaomaior(wchar_t combinacoes[][30], size_t tamanhos[], int numLinhas, int cartas[4][14]) { int valores[numLinhas]; for (int i = 0; i < numLinhas; i++) { int cartaMaisAltaValor = -1; for (size_t j = 0; j < tamanhos[i]; j++) { int valor = extrairvalor(combinacoes[i][j], cartas); if (valor > cartaMaisAltaValor) cartaMaisAltaValor = valor; } valores[i] = cartaMaisAltaValor; } for (int i = 0; i < numLinhas - 1; i++) { for (int j = 0; j < numLinhas - i - 1; j++) { if (valores[j] > valores[j + 1]) { int tempValor = valores[j]; valores[j] = valores[j + 1]; valores[j + 1] = tempValor; wchar_t tempComb[30]; memcpy(tempComb, combinacoes[j], sizeof(wchar_t) * 30); memcpy(combinacoes[j], combinacoes[j + 1], sizeof(wchar_t) * 30); memcpy(combinacoes[j + 1], tempComb, sizeof(wchar_t) * 30); size_t tempSize = tamanhos[j]; tamanhos[j] = tamanhos[j + 1]; tamanhos[j + 1] = tempSize; } } } } void inverterLinhas(wchar_t combinacoes[][30], size_t tamanhos[], int numLinhas) { for (int i = 0; i < numLinhas / 2; i++) { wchar_t tempComb[30]; memcpy(tempComb, combinacoes[i], sizeof(wchar_t) * 30); memcpy(combinacoes[i], combinacoes[numLinhas - i - 1], sizeof(wchar_t) * 30); memcpy(combinacoes[numLinhas - i - 1], tempComb, sizeof(wchar_t) * 30); size_t tempSize = tamanhos[i]; tamanhos[i] = tamanhos[numLinhas - i - 1]; tamanhos[numLinhas - i - 1] = tempSize; } } void processarteste(int num_teste, int cartas[4][14]) { wprintf(L"Teste %d\n", num_teste); int k; wscanf(L"%d", &k); wchar_t baralho[k][30]; size_t tamanhos[k]; int boolcartas[k][4][14]; for (int j = 0; j < k; j++) { wscanf(L"%ls", baralho[j]); tamanhos[j] = wcslen(baralho[j]); preencherbool(baralho[j], tamanhos[j], boolcartas[j],cartas); } if (!verificarcombinacoes(tamanhos[0], boolcartas[0], tamanhos[1], boolcartas[1])) { wprintf(L"Combinações não iguais!\n"); return; } for (int j = 0; j < k; j++) { ordenarcartas(baralho[j], tamanhos[j]); } combinacaomaior(baralho, tamanhos, k); inverterLinhas(baralho, tamanhos, k); for (int j = k - 1; j >= 0; j--) { for (size_t l = 0; l < tamanhos[j]; ++l) { wprintf(L"%lc ", baralho[j][l]); } wprintf(L"\n"); } } int main() { setlocale(LC_ALL, "C.UTF-8"); wchar_t cartas[4][14] = { {0x1F0A1, 0x1F0A2, 0x1F0A3, 0x1F0A4, 0x1F0A5, 0x1F0A6, 0x1F0A7, 0x1F0A8, 0x1F0A9, 0x1F0AA, 0x1F0AB, 0x1F0AC, 0x1F0AD, 0x1F0AE}, // Espadas {0x1F0B1, 0x1F0B2, 0x1F0B3, 0x1F0B4, 0x1F0B5, 0x1F0B6, 0x1F0B7, 0x1F0B8, 0x1F0B9, 0x1F0BA, 0x1F0BB, 0x1F0BC, 0x1F0BD, 0x1F0BE}, // Copas {0x1F0C1, 0x1F0C2, 0x1F0C3, 0x1F0C4, 0x1F0C5, 0x1F0C6, 0x1F0C7, 0x1F0C8, 0x1F0C9, 0x1F0CA, 0x1F0CB, 0x1F0CC, 0x1F0CD, 0x1F0CE}, // Ouros {0x1F0D1, 0x1F0D2, 0x1F0D3, 0x1F0D4, 0x1F0D5, 0x1F0D6, 0x1F0D7, 0x1F0D8, 0x1F0D9, 0x1F0DA, 0x1F0DB, 0x1F0DC, 0x1F0DD, 0x1F0DE} // Paus }; int T; wscanf(L"%d", &T); for (int i = 0; i < T; i++) { processarteste(i + 1, cartas); } return 0; }
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else
is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}