BullsAndCows Ver.I
C
#pragma warning (disable:4996) #include<stdio.h> #include<stdlib.h> #include<time.h> int guess[4]; int random(int a, int b) { return (rand() % (b - a + 1) + a); } int repeat() { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (guess[i] == guess[j] && i != j) { return 1; } } } return 0; } void init() { do { srand((unsigned)time(NULL)); guess[0] = random(1, 9); for (int i = 1; i < 4; ++i) { guess[i] = random(0, 9); } } while (repeat()); } int judge() { int m = 0, n = 0; int input[4] = { getchar() - 48,getchar() - 48,getchar() - 48,getchar() - 48 }; getchar(); for (int i = 0; i < 4; ++i) { if (input[i] == guess[i]) { ++m; } for (int j = 0; j < 4; ++j) { if (input[i] == guess[j] && j != i) { ++n; } } } if (m == 4) { return 1; } printf("%dA%dB\n", m, n); } int main() { init(); printf("guess 4bit\n"); for (int i = 0; i < 15; ++i) { if (i == 7) { printf("7,cont?y/n"); if (getchar() == 'n') { return 0; } getchar(); } if (judge() == 1) { printf("fuck"); } } printf("you died"); return 0; }
C++
#include<iostream> #include<cstdlib> #include<ctime> #include<string> #define random(a,b) (rand()%(b-a+1)+a) using namespace std; bool game() { srand((unsigned)time(NULL)); string guess; for (guess = to_string((random(1, 9)) * 1000 + (random(0, 9)) * 100 + (random(0, 9)) * 10 + (random(0, 9))); !(guess[0] != guess[1] != guess[2] != guess[3]); guess = to_string((random(1, 9)) * 1000 + (random(0, 9)) * 100 + (random(0, 9)) * 10 + (random(0, 9)))); cout << "猜一个4位数" << endl; string input; int m = 0, n = 0; for (int i = 0; i < 15; ++i) { cin >> input; if (guess == input) { cout << "fuck"; return true; } else { for (int j = 0; j < 4; ++j) { if (input[j] == guess[j]) { ++m; } for (int k = 0; k < 4; ++k) { if (input[j] == guess[k] && j != k) { ++n; } } } } cout << m << "A" << n << "B" << endl; if (i == 7) { cout << "7,con?" << endl; for (int choose;;) { cin >> choose; if (choose == 0) { return true; } else if (choose == 1) { break; } } } } cout << "dead"; return true; } int main() { game(); return 0; }