Advertisement
FreakSkipper

B de primos #fail_edition

Sep 26th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. /*Questão B da semana 05 ("B de primos"). */
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. int ehPrimo (int x) {   /*Confere se o valor fornecido é primo (1) ou não é primo (0).*/
  7.     int aux;
  8.     if (x==2) return 1;
  9.  
  10.     else {    
  11.         if (x>2) {
  12.             aux=x%2;
  13.             if (aux==0) {
  14.                 return 0;
  15.             }  
  16.             else {
  17.                 return 1;
  18.             }
  19.         }
  20.         else return 0;
  21.     }
  22. }
  23.  
  24. int divisoresPrimos (int x) {    /*Atribui a variavel "qtd" a quantidade de divisores de "x" com o auxilio de "ehPrimo".*/
  25.     int i, qtd;
  26.     qtd=0;
  27.  
  28.     for (i=1; i<=x; i++) {
  29.         if (ehPrimo(i)==1) {
  30.             if (x%i==0) qtd+=1;
  31.         }
  32.     }
  33.     return qtd;
  34. }
  35.  
  36. int f (int x, int a, int b) {     /*Função responsavel por calcular o valor f(x) especificado pela "main" e com o auxilio das outras funções.*/
  37.     int funcao;
  38.    
  39.     funcao= (a*pow(divisoresPrimos(x), 3) - (b*x));
  40.  
  41.     return funcao;
  42. }
  43.  
  44. int main () {         /*Recebe os valores, nomeia um valor para "x" de "l" até "r", e passa o mesmo junto de "a, b" para a função "f".*/
  45.     int a, b, l, r, i, x, funcao;
  46.  
  47.     scanf("%d %d %d %d", &a, &b, &l, &r);
  48.  
  49.     for (i=l; i<=r; i++) {
  50.         x=i;
  51.        
  52.         if (x==l) {
  53.             funcao= f(x, a, b);
  54.         }
  55.    
  56.         else if (funcao<f(x, a, b)) {
  57.             funcao= f(x, a, b);
  58.         }
  59.     }
  60.    
  61.     printf("%d\n", funcao);         /*Printa o maior valor possível para f(x).*/
  62.  
  63.     return 0;
  64. }
  65.  
  66.  
  67. /* Pelo menos essa era a ideia né, já que eu tô tomando WA de 2% =/ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement