Advertisement
Patey

Untitled

May 27th, 2021
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int st[100], vf;
  5. int N, Q, V;
  6. int nr=0;
  7.  
  8. int patrat_perfect(int x)
  9. {
  10.     int d;
  11.     for (d = 2; d <= x / 2; d++)
  12.     {
  13.         if (d*d == x)
  14.             return 1;
  15.     }
  16.     return 0;
  17. }
  18.  
  19. void Init(int k)
  20. {
  21.     st[k] = 0;
  22. }
  23.  
  24. int Succesor(int k) {
  25.     if (patrat_perfect(st[k]) == 1)
  26.         nr--;
  27.     if (st[k] < V)
  28.     {
  29.         st[k]++;
  30.         {
  31.             if (patrat_perfect(st[k]) == 1)
  32.                 nr++;
  33.         }
  34.         return 1;
  35.     }
  36.     else
  37.     {
  38.         return 0;
  39.     }
  40. }
  41.  
  42. int Solution(int k)
  43. {
  44.     if (k == N)
  45.         return 1;
  46.     else
  47.         return 0;
  48. }
  49.  
  50. void Print() {
  51.     FILE *fis_out = fopen("out.txt", "wt");
  52.     int i;
  53.     for (i = 1; i <= N; i++)
  54.     {
  55.         fprintf(fis_out,"%d ", st[i]);
  56.     }
  57.     printf("\n");
  58. }
  59.  
  60. void Back()
  61. {
  62.     int iS, iV, i, j, k, suma, poz;
  63.  
  64.     vf = 1;
  65.     Init(vf);
  66.     while (vf > 0)
  67.     {
  68.         iS = 0;
  69.         iV = 0;
  70.         do {
  71.             iS = Succesor(vf);
  72.             if (iS)
  73.             {
  74.                 iV = 1;
  75.             }
  76.         } while (iS && !iV);
  77.         if (iS)
  78.         {
  79.             if (Solution(vf))
  80.             {
  81.                 if (nr == Q)
  82.                 {
  83.                     Print(vf);
  84.                 }
  85.             }
  86.             else
  87.             {
  88.                 vf++;
  89.                 Init(vf);
  90.             }
  91.         }
  92.         else
  93.         {
  94.             vf--;
  95.         }
  96.     }
  97. }
  98. void main()
  99. {
  100.     FILE *fis = fopen("in.txt", "rt");
  101.     fscanf(fis,"%d", &N);
  102.     fscanf(fis,"%d", &Q);
  103.     fscanf(fis,"%d", &V);
  104.     Back();
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement