hdu 5143 NPY and arithmetic progression(枚举)

NPY is learning arithmetic progression in his math class. In mathematics, an arithmetic progression (AP) is a sequence of numbers such that the difference between the consecutive terms is constant.(from wikipedia)
He thinks it's easy to understand,and he found a challenging problem from his talented math teacher:
You're given four integers, $a_1, a_2, a_3, a_4$, which are the numbers of 1,2,3,4 you have.Can you divide these numbers into some Arithmetic Progressions,whose lengths are equal to or greater than 3?(i.e.The number of AP can be one)
Attention: You must use every number exactly once.
Can you solve this problem?
Input
The first line contains a integer T — the number of test cases (1≤T≤1000001≤T≤100000).
The next T lines,each contains 4 integers a1,a2,a3,a4(0≤a1,a2,a3,a4≤109)a1,a2,a3,a4(0≤a1,a2,a3,a4≤109).
Output
For each test case,print "Yes"(without quotes) if the numbers can be divided properly,otherwise print "No"(without quotes).
Sample Input

3
1 2 2 1
1 0 0 0
3 0 0 0

Sample Output

Yes
No
Yes

/*
解题思路:等差数列共三种情况:123 234 1234 用光所有数字,组成0到多个长度至少为3的等差数列 组不成等差数列返回No 组成返回Yes
            1. a个1 b个2 c个3 d个4 组成公差为1的等差数列后 剩余的数字大于3的话可以自身组成公差为0的等差数列(例如组成2个123 一个333 那么输入的abcd依次为2250)
            2. a个1 b个2 c个3 d个4 刚好用完,组成多个公差为1的等差数列
*/

#include<stdio.h>
#include<string.h>
#define MAX 100000
#define MAX2 1000000000
int main()
{
    int i,j,k,T,flag;
    int a,b,c,d;
    scanf("%d",&T);
    if (T<=MAX)
    {
    while(T--){
        scanf("%d%d%d%d",&a,&b,&c,&d);
        flag=0;
        if(a<=MAX2||b<=MAX2||c<=MAX2||d<=MAX2){
            for (i = 0; i <= 2; i++)    //情况1:123
                for (j = 0; j <= 2; j++)//情况2:234               
                    for (k = 0; k <= 2; k++)//情况3:1234                  
                        {if (((a-i-k==0)||(a-i-k>=3))&&((b-i-j-k==0)||(b-i-j-k>=3))&&((c-i-j-k==0)||(c-i-j-k>=3))&&((d-j-k==0)||(d-j-k>=3)))
                            {flag=1;break;}
                    }

                    
                
            
        if (flag)
            printf("Yes\n");        
        else
            printf("No\n");
    }
    }   
    }


    return 0;
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 12,230评论 0赞 23
  • 中国是制造业大国,但随着技术的革新,行业竞争已经由传统的制造转向服务,从去年起,中国的服务业在全国GDP中的比重首...
    茶山阅读 484评论 0赞 3
  • 我绕过走廊的拐角,推开了虚掩的门。房间里,那个男人还在和他周围的听众们讲述着那个故事。 “人类一生都是在寻找答案...
    天气先生smiley阅读 318评论 0赞 0

友情链接更多精彩内容