U E D R , A S I H C RSS

숫자야구/aekae

숫자 야구

~cpp 
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	srand(time(0));
	int number=rand() % 1000;
	cout << number << endl;
	int arr_number[3]={number % 10, (number / 10) % 10, number / 100};

	while (arr_number[0] == arr_number[1] || arr_number[1] == arr_number[2] || arr_number[2] == arr_number[0])
	{
		number=rand() % 1000;
		arr_number[0]=number % 10;
		arr_number[1]=(number / 10) % 10;
		arr_number[2]=number / 100;
	}

	cout << number << endl;
	
	cout << "3자리 숫자 입력: ";
	int input;
	int i,j;
	while(cin >> input)
	{
		int arr_input[3] = {input % 10, (input / 10) % 10, input / 100};
		int strike = 0;
		int ball = 0;
		for (i=0; i<3; i++)
		{
			if (arr_input[i] == arr_number[i])
				strike++;
			for (j=0; j<3; j++)
			{
				if (i != j)
				{
					if (arr_input[i] == arr_number[j])
						ball++;
				}
			}
		}
		if (input == number)
			break;
		cout << strike << "S " << ball << "B " << endl;
		cout << "3자리 숫자 입력: ";
	}

	cout << "OUT" << endl;
	return 0;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:16
Processing time 0.0144 sec