Problem Description
CA loves to play with sticks.
One day he receives four pieces of sticks, he wants to know these sticks can spell a quadrilateral.
(What is quadrilateral? Click here: https://en.wikipedia.org/wiki/Quadrilateral)
Input
First line contains T denoting the number of testcases.
T testcases follow. Each testcase contains four integers a,b,c,d in a line, denoting the length of sticks.
1≤T≤1000, 0≤a,b,c,d≤263−1
Output
For each testcase, if these sticks can spell a quadrilateral, output “Yes”; otherwise, output “No” (without the quotation marks).
Sample Input
2
1 1 1 1
1 1 9 2
Sample Output
Yes
No
这道模拟很好的诠释了“最笨的方法是最好的方法”。网上搜题解看到的方法,不得不服。另外有一个坑点,就是如果走过了2,之后再走13是可以的。
贴代码。关键是一个判断函数很巧妙。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<climits>
#include<cmath>
#include<algorithm>
#include<ctype.h>
using namespace std;
int visit[20],a[20];
bool panduan(int x,int y)
{
if(x>y)
swap(x,y);
if((x==1)&&(y==3))
return(visit[2]>0);
if((x==4)&&(y==6))
return(visit[5]>0);
if((x==7)&&(y==9))
return(visit[8]>0);
if((x==1)&&(y==7))
return(visit[4]>0);
if((x==2)&&(y==8))
return(visit[5]>0);
if((x==3)&&(y==9))
return(visit[6]>0);
if((x==1)&&(y==9))
return(visit[5]>0);
if((x==3)&&(y==7))
return(visit[5]>0);
return 1;
}
int main(void)
{
int status,t,n;
cin>>t;
while(t--)
{
memset(visit,0,sizeof(visit));
scanf("%d",&n);
status=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if((a[i]>=1)&&(a[i]<=9))
{
visit[a[i]]++;
if((i>1)&&(a[i-1]>=1)&&(a[i-1]<=9))
if(panduan(a[i-1],a[i])==0)
status=0;
}
else
status=0;
}
//cout<<status<<endl;
for(int i=1;i<=9;i++)
if(visit[i]>1)
status=0;
if(n<4)
status=0;
//cout<<status<<endl;
if(status)
printf("valid\n");
else
printf("invalid\n");
}
return 0;
}