Advertisement
Guest User

PSUT

a guest
Apr 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. ////////////Q3
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. int get_matching_digit(int n1, int n2);
  6. int main()
  7. {
  8.     srand(time(NULL));
  9.     char repeat;
  10.     int computer_number, player_number, matching_digit;
  11.     do
  12.     {
  13.         computer_number = 100 + rand() % 900;
  14.         printf("Enter your guess : ");
  15.         scanf_s("%d", &player_number);
  16.         matching_digit = get_matching_digit(computer_number, player_number);
  17.         if (matching_digit != -1) {
  18.             printf("You won!\nThe number is %d.\nYour guess is %d.\nAnd the matching digit is : %d.\n", computer_number, player_number, matching_digit);
  19.         }
  20.         else {
  21.             printf("You lost.\n");
  22.         }
  23.         printf("Do you want to guess again ? [Y\\N] : ");
  24.         scanf_s(" %c", &repeat);
  25.     } while (repeat == 'Y');
  26.     return 0;
  27. }
  28.  
  29. int get_matching_digit(int n1, int n2)
  30. {
  31.     int lu[11]{ 0 };
  32.     while (n1 > 0)
  33.     {
  34.         lu[n1 % 10] = 1;
  35.         n1 /= 10;
  36.     }
  37.     int d;
  38.     while (n2 > 0)
  39.     {
  40.         d = n2 % 10;
  41.         if (lu[d] == 1)
  42.             return d;
  43.         n2 /= 10;
  44.     }
  45.     return -1;
  46. }
  47.  
  48. ///////////////////////////////////////Q4
  49. #define _CRT_SECURE_NO_WARNINGS
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <time.h>
  53. int get_hint(int number);
  54. void play(int number, int trail);
  55. int main()
  56. {
  57.     srand(time(NULL));
  58.     int computer_number = 100 + rand() % 900;
  59.     printf("Hint the product of the number digits is %d.\n", get_hint(computer_number));
  60.     play(computer_number, 1);
  61.     //system("pause");
  62.     return 0;
  63. }
  64. void play(int number, int trail)
  65. {
  66.     if (trail > 1) {
  67.         printf("Trail %d.\n", trail);
  68.     }
  69.     printf("Enter your guess : ");
  70.     int player_guess;
  71.     scanf("%d", &player_guess);
  72.     if (player_guess == number) {
  73.         printf("You won!\n");
  74.     }
  75.     else {
  76.         play(number, trail + 1);
  77.     }
  78. }
  79. int get_hint(int number) {
  80.     int hint = 1;
  81.     while (number > 0)
  82.     {
  83.         hint *= number % 10;
  84.         number /= 10;
  85.     }
  86.     return hint;
  87. }
  88.  
  89.  
  90. /////////////////////////////////////////////////Q5
  91. #define _CRT_SECURE_NO_WARNINGS
  92. #include <stdio.h>
  93. #include <stdlib.h>
  94. #include <time.h>
  95. int get_intial_hint(int number);
  96. int matching_digits_count(int number, int guess);
  97. void play(int number, int trail);
  98. int main()
  99. {
  100.     srand(time(NULL));
  101.     int computer_number = 1000 + rand() % 10000;
  102.     printf("Hint, the sum of the number digits is %d.\n", get_intial_hint(computer_number));
  103.     play(computer_number, 1);
  104.     //system("pause");
  105.     return 0;
  106. }
  107. void play(int number, int trail)
  108. {
  109.     printf("Enter your guess : ");
  110.     int player_guess;
  111.     scanf("%d", &player_guess);
  112.     if (player_guess == number) {
  113.         printf("You won!\n");
  114.     }
  115.     else {
  116.         if (trail == 10) {
  117.             printf("You lost.\n");
  118.             return;
  119.         }
  120.         trail++;
  121.         printf("Trail %d.\n", trail);
  122.         printf("Hint, the count of matching digits between your last guess and the number is %d.\n", matching_digits_count(number, player_guess));
  123.         play(number, trail);
  124.     }
  125. }
  126. int get_intial_hint(int number) {
  127.     int hint = 0;
  128.     while (number > 0)
  129.     {
  130.         hint += number % 10;
  131.         number /= 10;
  132.     }
  133.     return hint;
  134. }
  135. int matching_digits_count(int number1, int number2) {
  136.     int lu[11]{ 0 }, ans = 0;
  137.  
  138.     for (int i = 0; number1 > 0 && i < 11; i++)
  139.     {
  140.         lu[i] = number1 % 10;
  141.         number1 /= 10;
  142.     }
  143.     for (int i = 0; number2 > 0 && i < 11; i++)
  144.     {
  145.         if (lu[i] == number2 % 10)
  146.             ans++;
  147.         number2 /= 10;
  148.     }
  149.     return ans;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement