Advertisement
Guest User

Untitled

a guest
May 25th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. int MMC_2(int n1, int n2) {
  5.    
  6.     int resto, x = n1, y = n2;
  7.  
  8.     do {
  9.         resto = x % y;
  10.  
  11.         x = y;
  12.         y = resto;
  13.  
  14.     } while (resto != 0);
  15.  
  16.     return ( n1 * n2) / x;
  17. }
  18.  
  19. int MMC_Geral(int *TQ, int N){
  20.     int mmc = TQ[0];
  21.    
  22.     if (N!=1){
  23.         int i = 0; 
  24.         for (i = 1; i < N; i++){
  25.             mmc = MMC_2(mmc, TQ[i]);
  26.         }
  27.     }
  28.    
  29.     return mmc;
  30.    
  31. }
  32.  
  33. int acharX(int mmc, int T, int N, int *TQ){
  34.     int i = 0, x = 0;
  35.    
  36.     for (i = 2; i <= T; i++){
  37.             int Y = MMC_2(mmc, i);
  38.             bool isInvalido = false;
  39.             if (Y == T){
  40.                 int j = 0;
  41.                 for (j = 0; j < N; j++){
  42.                     if (i==TQ[j]){
  43.                         isInvalido = true;
  44.                         break;
  45.                     }
  46.                 }
  47.                 if (!isInvalido){
  48.                     x = i;
  49.                     break; 
  50.                 }  
  51.             }
  52.     }
  53.    
  54.     return x;
  55.        
  56. }
  57.  
  58. int main(void){
  59.     int N = 0, T = 0; //número de bolas e tempo das N+1 bolas
  60.     scanf("%d %d", &N, &T);
  61.    
  62.     while((N!=0) && (T!=0)){
  63.         int *TQ = (int*) malloc(N*sizeof(int)); //tempo de quique de cada uma das bolas
  64.        
  65.         int i = 0;
  66.         for (i = 0; i < N; i++){
  67.             scanf("%d", &TQ[i]);
  68.         }
  69.        
  70.         int mmc = MMC_Geral(TQ, N);
  71.         int x = acharX(mmc, T, N, TQ);
  72.        
  73.         if (x==0){
  74.             printf("Impossivel\n");
  75.         }
  76.         else{
  77.             printf("%d\n", x);
  78.         }
  79.        
  80.         scanf("%d %d", &N, &T);
  81.     }
  82.    
  83.     return 0;
  84.    
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement