Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////Q3
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int get_matching_digit(int n1, int n2);
- int main()
- {
- srand(time(NULL));
- char repeat;
- int computer_number, player_number, matching_digit;
- do
- {
- computer_number = 100 + rand() % 900;
- printf("Enter your guess : ");
- scanf_s("%d", &player_number);
- matching_digit = get_matching_digit(computer_number, player_number);
- if (matching_digit != -1) {
- printf("You won!\nThe number is %d.\nYour guess is %d.\nAnd the matching digit is : %d.\n", computer_number, player_number, matching_digit);
- }
- else {
- printf("You lost.\n");
- }
- printf("Do you want to guess again ? [Y\\N] : ");
- scanf_s(" %c", &repeat);
- } while (repeat == 'Y');
- return 0;
- }
- int get_matching_digit(int n1, int n2)
- {
- int lu[11]{ 0 };
- while (n1 > 0)
- {
- lu[n1 % 10] = 1;
- n1 /= 10;
- }
- int d;
- while (n2 > 0)
- {
- d = n2 % 10;
- if (lu[d] == 1)
- return d;
- n2 /= 10;
- }
- return -1;
- }
- ///////////////////////////////////////Q4
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int get_hint(int number);
- void play(int number, int trail);
- int main()
- {
- srand(time(NULL));
- int computer_number = 100 + rand() % 900;
- printf("Hint the product of the number digits is %d.\n", get_hint(computer_number));
- play(computer_number, 1);
- //system("pause");
- return 0;
- }
- void play(int number, int trail)
- {
- if (trail > 1) {
- printf("Trail %d.\n", trail);
- }
- printf("Enter your guess : ");
- int player_guess;
- scanf("%d", &player_guess);
- if (player_guess == number) {
- printf("You won!\n");
- }
- else {
- play(number, trail + 1);
- }
- }
- int get_hint(int number) {
- int hint = 1;
- while (number > 0)
- {
- hint *= number % 10;
- number /= 10;
- }
- return hint;
- }
- /////////////////////////////////////////////////Q5
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int get_intial_hint(int number);
- int matching_digits_count(int number, int guess);
- void play(int number, int trail);
- int main()
- {
- srand(time(NULL));
- int computer_number = 1000 + rand() % 10000;
- printf("Hint, the sum of the number digits is %d.\n", get_intial_hint(computer_number));
- play(computer_number, 1);
- //system("pause");
- return 0;
- }
- void play(int number, int trail)
- {
- printf("Enter your guess : ");
- int player_guess;
- scanf("%d", &player_guess);
- if (player_guess == number) {
- printf("You won!\n");
- }
- else {
- if (trail == 10) {
- printf("You lost.\n");
- return;
- }
- trail++;
- printf("Trail %d.\n", trail);
- printf("Hint, the count of matching digits between your last guess and the number is %d.\n", matching_digits_count(number, player_guess));
- play(number, trail);
- }
- }
- int get_intial_hint(int number) {
- int hint = 0;
- while (number > 0)
- {
- hint += number % 10;
- number /= 10;
- }
- return hint;
- }
- int matching_digits_count(int number1, int number2) {
- int lu[11]{ 0 }, ans = 0;
- for (int i = 0; number1 > 0 && i < 11; i++)
- {
- lu[i] = number1 % 10;
- number1 /= 10;
- }
- for (int i = 0; number2 > 0 && i < 11; i++)
- {
- if (lu[i] == number2 % 10)
- ans++;
- number2 /= 10;
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement