B. Cubes for Masha
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Absent-minded Masha got set of n cubes for her birthday.
At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x.
To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number.
The number can’t contain leading zeros. It’s not required to use all cubes to build a number.
Pay attention: Masha can’t make digit 6 from digit 9 and vice-versa using cube rotations.
Input
In first line integer n is given (1 ≤ n ≤ 3) — the number of cubes, Masha got for her birthday.
Each of next n lines contains 6 integers aij (0 ≤ aij ≤ 9) — number on j-th face of i-th cube.
Output
Print single integer — maximum number x such Masha can make any integers from 1 to x using her cubes or 0 if Masha can’t make even 1.
Examples
input
3
0 1 2 3 4 5
6 7 8 9 0 1
2 3 4 5 6 7
output
87
input
3
0 1 3 5 6 8
1 2 4 5 7 8
2 3 4 6 7 9
output
98
Note
In the first test case, Masha can build all numbers from 1 to 87, but she can’t make 88 because there are no two cubes with digit 8.
题意:
给出n行数,每行6个数,从每行中只能抽取一个数问能组成最大的数是多少,输出这个数m,且要保证1~~m-1都能组成;
思路:
数据较少,暴力,仔细想想可以发现最大能组成的数不超过99,由于n最大为3,若要组成99,至少需要2个1,2个2,2个3……2个9,一个0,这些数加起来超过了18,所以最大为99;
代码;
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
int n;
int vis[100];
int a[4][7];
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
for(int j=0;j<6;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<6;j++)
{
vis[a[i][j]]=1;
for(int k=0;k<n;k++)
{
if(i==k) continue;
for(int l=0;l<6;l++)
{
vis[a[i][j]*10+a[k][l]]=1;
}
}
}
}
for(int i=1;i<=100;i++)
{
if(!vis[i])
{
printf("%d\n",i-1);
break;
}
}
}
}
从一个任意大小的二维数组中取两个数进行操作 [//];
for(int i=0;i<n;i++)//
{
for(int j=0;j<6;j++)//
{
vis[a[i][j]]=1;
for(int k=0;k<n;k++)//
{
if(i==k) continue;//
for(int l=0;l<6;l++)//
{
vis[a[i][j]*10+a[k][l]]=1;
}
}
}
}