0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

毎日ABC day19【c++】ABC327 A-C問題解説【ACコード付き】

Posted at

AtCoder Beginner Contest 327

A - ab

int main()
{
	int N;
	cin >> N;
	string S;
	cin >> S;
	for (int i = 1; i < N; i++)
	{
		if (S[i] == 'a' && S[i - 1] == 'b')
		{
			cout << "Yes" << endl;
			return 0;
		}
		if (S[i] == 'b' && S[i - 1] == 'a')
		{
			cout << "Yes" << endl;
			return 0;
		}
	}
	cout << "No" << endl;
	return 0;
}

B - A^A

与えられる$10^{18}$までの整数 $B$ と一致するような$A^A$ が 存在するか調べろ、という問題。

$10^{18}$までという制約なので、$A$ の範囲が16までであることに目をつけて、計算量を抑える。

int main()
{
	long long  B;
	cin >> B;
	for (int i = 1; i <= 17; i++)
	{
		long long tmp = 1;
		for (int j = 1; j <= i; j++)
		{
			tmp *= i;
		}
		if (tmp == B)
		{
			cout << i << endl;
			return 0;
		}
	}
	cout << -1 << endl;
	return 0;
}

C - Number Place

ナンプレ。0から9までの数字が、①縦でも②横でも③3×3のマスでも重複しないような盤面かどうかを判定せよ、という問題。

①②③それぞれで0から9の数字に対応するbool型を保持し、重複が来たら即座に"No"を出力するというプログラムを書いた。

int main()
{
	int A[10][10];
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++) cin >> A[i][j];
	}
	// 重複チェック
	bool check_row[10][10];
	bool check_column[10][10];
	bool check_box[10][10];
	for (int i = 0; i <= 9; i++)
	{
		for (int j = 0; j <= 9; j++)
		{
			check_column[i][j] = false;
			check_row[i][j] = false;
			check_box[i][j] = false;
		}
	}
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			int num = A[i][j];
			int k = (i / 3) * 3 + (j / 3);
			if (check_column[i][num] || check_row[j][num] || check_box[k][num])
			{
				cout << "No" << endl;
				return 0;
			}
			check_column[i][num]= true;
			check_row[j][num] = true;
			check_box[k][num] = true;
		}
	}
	cout << "Yes" << endl;
	return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?