Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package softuni;
- import java.util.Scanner;
- public class SoftUni {
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter your number: ");
- int guessNumber = Integer.parseInt(sc.nextLine());
- System.out.print("Enter the number of bulls: ");
- int targetBulls = Integer.parseInt(sc.nextLine());
- System.out.print("Enter the number of cows: ");
- int targetCows = Integer.parseInt(sc.nextLine());
- boolean solutionFound = false;
- int guessDigit1 = (guessNumber / 1000) % 10;
- int guessDigit2 = (guessNumber / 100) % 10;
- int guessDigit3 = (guessNumber / 10) % 10;
- int guessDigit4 = (guessNumber / 1) % 10;
- for(int digit1 = 1;digit1 <= 9;digit1++){
- for(int digit2 = 1;digit2 <= 9;digit2++){
- for(int digit3 = 1;digit3 <= 9;digit3++){
- for(int digit4 = 1;digit4 <= 9;digit4++){
- int currentBulls = 0;
- int currentCows = 0;
- int digitToCheck = digit1;
- int digitToCheck2 = digit2;
- int digitToCheck3 = digit3;
- int digitToCheck4 = digit4;
- int guess1 = guessDigit1;
- int guess2 = guessDigit2;
- int guess3 = guessDigit3;
- int guess4 = guessDigit4;
- if(digitToCheck == guess1){
- currentBulls++;
- digitToCheck = -2;
- guess1 = -1;
- }
- if(digitToCheck2 == guess2){
- currentBulls++;
- digitToCheck2 = -2;
- guess2 = -1;
- }
- if(digitToCheck3 == guess3){
- currentBulls++;
- digitToCheck3 = -2;
- guess3 = -1;
- }
- if(digitToCheck4 == guess4){
- currentBulls++;
- digitToCheck4 = -2;
- guess4 = -1;
- }
- if(digitToCheck == guess2){
- currentCows++;
- guess2 = -1;
- }
- else if(digitToCheck == guess3){
- currentCows++;
- guess3 = -1;
- }
- else if(digitToCheck == guess4){
- currentCows++;
- guess4 = -1;
- }
- if(digitToCheck2 == guess1){
- currentCows++;
- guess1 = -1;
- }
- else if(digitToCheck2 == guess3){
- currentCows++;
- guess3 = -1;
- }
- else if(digitToCheck2 == guess4){
- currentCows++;
- guess4 = -1;
- }
- if(digitToCheck3 == guess1){
- currentCows++;
- guess1 = -1;
- }
- else if(digitToCheck3 == guess2){
- currentCows++;
- guess2 = -1;
- }
- else if(digitToCheck3 == guess4){
- currentCows++;
- guess4 = -1;
- }
- if(digitToCheck4 == guess1){
- currentCows++;
- guess1 = -1;
- }
- else if(digitToCheck4 == guess2){
- currentCows++;
- guess2 = -1;
- }
- else if(digitToCheck4 == guess3){
- currentCows++;
- guess3 = -1;
- }
- if(currentBulls == targetBulls && currentCows == targetCows){
- System.out.print("" + digit1 + digit2 + digit3 + digit4);
- solutionFound = true;
- if(solutionFound){
- System.out.print(" ");
- }
- }
- }
- }
- }
- }
- if(solutionFound == false){
- System.out.println("No");
- }
- }
- }
Add Comment
Please, Sign In to add comment