Combination Lock

http://train.usaco.org/usacoprob2?a=LqcgyaE9N7d&S=combo

题目大意:

N个字母组成的3位密码锁,有2种标准密码可以打开锁

锁具有容错性,允许每个位置的数字在标准密码对应的数字+/-2的范围内打开锁

输入N和2中标准密码,输出有多少种密码可以打开锁


分析:

N<=5时:N³种

N>5时:250(5³)— 重复的接近2中密码的组合(即既可视为接近第一种密码,又可视为接近第二种密码)


#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;
int a[4][15];
int p[4];
int main()
{
	ifstream fin("combo.in");
	ofstream fout("combo.out");
	int n, t, k;
	while(fin >> n)
	{
		p[1] = p[2] =p[3] = 0;
		for(int i = 0; i < 2; i++)			//2次输入 
		{
			for(int j = 1; j <= 3; j++)		//每次输入3个数字 
			{
				fin >> t;
				int x;
				for(int m = 0; m < 5; m++) 
				{
					x = (t + n - 2 + m)%n == 0 ? n : (t + 2*n - 2 + m)%n;
					for(k = 0; k < p[j]; k++)
						if(x == a[j][k]) break;
					if(k == p[j]) a[j][p[j]++] = x;
				}
			}
		}
		if(n <= 5) fout << n*n*n << endl;
		else fout << 250 - (10 - p[1])*(10 - p[2])*(10 - p[3]) << endl;	
	}
	fout.close();
	return 0;
}


你可能感兴趣的:(C++,水题)