#include <iostream> #include <math.h> #include <stdio.h> #include <stdlib.h> bool isFibonacci(int x) { int s1 = sqrt(5*x*x + 4); int s2 = sqrt(5*x*x - 4); if(s1*s1 == (5*x*x + 4) || s2*s2 == (5*x*x - 4)) return true; else return false; } bool isIloczyn(int x) { int *tab, ileEl; bool find = false; tab = (int*) malloc(2 * sizeof(int)); ileEl = 2; tab[0] = 0; tab[1] = 1; if(isFibonacci(x)) { find = true; } else { while(true) { if(x >= tab[ileEl-1]+tab[ileEl-2]) { tab = (int*) realloc(tab, (ileEl + 1) * sizeof(int)); tab[ileEl] = tab[ileEl-1]+tab[ileEl-2]; ileEl++; } else { break; } } for(int i=0; i<ileEl; i++) { for(int j=i; j<ileEl; j++) { if(tab[i] * tab[j] == x) { find = true; break; } } } } return find; } int main() { int ile; int x; int *tab; tab = (int*) malloc(1 * sizeof(int)); scanf("%d", &ile); if(ile >= 1 && ile <= 10) { for (int i = 1; i <= ile; i++) { scanf("%i", &x); isIloczyn(x)? printf("TAK\n") : printf("NIE\n"); } } system("PAUSE"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <math.h> #include <stdio.h> #include <stdlib.h> bool isFibonacci(int x) { int s1 = sqrt(5*x*x + 4); int s2 = sqrt(5*x*x - 4); if(s1*s1 == (5*x*x + 4) || s2*s2 == (5*x*x - 4)) return true; else return false; } bool isIloczyn(int x) { int *tab, ileEl; bool find = false; tab = (int*) malloc(2 * sizeof(int)); ileEl = 2; tab[0] = 0; tab[1] = 1; if(isFibonacci(x)) { find = true; } else { while(true) { if(x >= tab[ileEl-1]+tab[ileEl-2]) { tab = (int*) realloc(tab, (ileEl + 1) * sizeof(int)); tab[ileEl] = tab[ileEl-1]+tab[ileEl-2]; ileEl++; } else { break; } } for(int i=0; i<ileEl; i++) { for(int j=i; j<ileEl; j++) { if(tab[i] * tab[j] == x) { find = true; break; } } } } return find; } int main() { int ile; int x; int *tab; tab = (int*) malloc(1 * sizeof(int)); scanf("%d", &ile); if(ile >= 1 && ile <= 10) { for (int i = 1; i <= ile; i++) { scanf("%i", &x); isIloczyn(x)? printf("TAK\n") : printf("NIE\n"); } } system("PAUSE"); return 0; } |