Last active
December 18, 2015 21:25
-
-
Save felipsmartins/943a2818e6496066c7db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Faça um programa em Linguagem C que leia uma matriz de | |
ordem 100 (ou seja 100x100) composta por números inteiros positivos | |
e escreva qual o maior número de fibonacci da coluna coluna, onde coluna é o | |
numero inteiro informado pelo usuário. | |
*/ | |
#include <stdio.h> | |
int isFibonacci(n) { | |
int a = 0; int b = 1; int c = 0; | |
if (a == n || b == n) { return 1; } | |
while (c <= n) { | |
c = a + b; | |
a = b; | |
b = c; | |
if (c == n) { return 1; /*true*/ } | |
} | |
return 0; /*false*/ | |
} | |
void main() { | |
int matrizTamanho = 4; //sim, é 100x100, mas seria tedioso testar... | |
int matriz[matrizTamanho][matrizTamanho]; //4x4 | |
int linha, coluna; //contadores | |
for (linha = 0; linha < matrizTamanho; linha++) { | |
for (coluna = 0; coluna < matrizTamanho; coluna++) { | |
printf("Coordenada %d, %d:\n", linha, coluna); | |
scanf("%d", &matriz[linha][coluna]); | |
} | |
//talvez aqui um system("cls") ou system("clear") (linux), seja legal. | |
} | |
int colunaK; | |
int FIBO_ENCONTRADO = 0; // boolean flag | |
int testNum; | |
puts("Número da coluna a pesquisar o maior número Fibonacci: "); | |
scanf("%d", &colunaK); | |
int maiorFibo = -1; // considera -1 como maior, inicialmente... | |
for (linha = 0; linha < matrizTamanho; linha++) { | |
testNum = matriz[linha][colunaK]; | |
if (isFibonacci(testNum)) { | |
FIBO_ENCONTRADO = 1; // encontrou | |
maiorFibo = (testNum > maiorFibo) ? testNum : maiorFibo; | |
} | |
} | |
if (FIBO_ENCONTRADO == 1) { | |
printf("Maior Fibonacci da coluna K: %d\n", maiorFibo); | |
} else { | |
puts("Não há Fibonacci na coluna K"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment