频道栏目
首页 > 程序开发 > 软件开发 > C++ > 正文
集训第一次周赛题目及题解
2013-07-15 13:19:26           
收藏   我要投稿

A. Help Vasilisa the Wise 2
Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 42   Accepted Submission(s) : 12
Problem Description
Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box's secret is, since she cannot open it again. She hopes that you will help her one more time with that.

The box's lock looks as follows: it contains 4 identical deepenings for gems as a 2×2 square, and some integer numbers are written at the lock's edge near the deepenings. The example of a lock is given on the picture below.

 


The box is accompanied with 9 gems. Their shapes match the deepenings' shapes and each gem contains one number from 1 to 9 (each number is written on exactly one gem). The box will only open after it is decorated with gems correctly: that is, each deepening in the lock should be filled with exactly one gem. Also, the sums of numbers in the square's rows, columns and two diagonals of the square should match the numbers written at the lock's edge. For example, the above lock will open if we fill the deepenings with gems with numbers as is shown on the picture below.

 


Now Vasilisa the Wise wants to define, given the numbers on the box's lock, which gems she should put in the deepenings to open the box. Help Vasilisa to solve this challenging task.

 

Input
The input contains numbers written on the edges of the lock of the box. The first line contains space-separated integers r1 and r2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers c1 and c2 that define the required sums of numbers in the columns of the square. The third line contains space-separated integers d1 and d2 that define the required sums of numbers on the main and on the side diagonals of the square (1≤r1,r2,c1,c2,d1,d2≤20). Correspondence between the above 6 variables and places where they are written is shown on the picture below. For more clarifications please look at the second sample test that demonstrates the example given in the problem statement.

 


 

Output
Print the scheme of decorating the box with stones: two lines containing two space-separated integers from 1 to 9. The numbers should be pairwise different. If there is no solution for the given lock, then print the single number "-1" (without the quotes).

If there are several solutions, output any.

 

Sample Input
3 7
4 6
5 5
11 10
13 8
5 16
 

Sample Output
1 2
3 4
4 7
9 1
 
题目意思从图上可以很容易的看懂,注意的是计算的时候从一个方面计算的话可能会漏掉数据,送多个数据进行计算,其次就是考虑计算结果为0的情况。。。。wa了几次。代码:

 
#include <stdio.h>   
int main()  
{  
    int r1,r2,c1,c2,d1,d2;  
    double a,b,c,d;  
    while(scanf("%d%d%d%d%d%d",&r1,&r2,&c1,&c2,&d1,&d2)==6)  
    {  
        double aa,bb,cc,dd,aaa,bbb,ccc,ddd;  
        if(r1-d1>0)  
        {  
            b=(r1-d1+c2)/2;  
            a=r1-b;  
            c=c1-a;  
            d=c2-b;  
        }  
        else  
        {  
            d=(d1-r1+c2)/2;  
            b=c2-d;  
            a=r1-b;  
            c=r2-d;  
        }  
        if(d2-c1>0)  
        {  
            bb=(d2-c1+r1)/2;  
            aa=r1-bb;  
            dd=c2-bb;  
            cc=r2-dd;  
        }  
        else  
        {  
            cc=(c1-d2+r1)/2;  
            dd=r2-cc;  
            aa=c1-cc;  
            bb=c2-dd;  
        }  
        if(r2-c2>0){  
            ccc=(r2-c2+d2)/2;  
            ddd=r2-ccc;  
            aaa=c1-ccc;  
            bbb=c2-ddd;  
        }  
        else  
        {  
            bbb=(c2-r2+d2)/2;  
            aaa=r1-bbb;  
            ddd=c2-bbb;  
            ccc=r2-ddd;  
        }  
        if(a==aa&&c==ccc&&d==ddd&&bb==bbb&&a>0&&b>0&&c>0&&d>0)  
        {  
                printf("%.0lf %.0lf\n",a,b);  
                printf("%.0lf %.0lf\n",c,d);  
        }  
        else  
            printf("-1\n");  
  
    }  
    return 0;  
}  

#include <stdio.h>
int main()
{
    int r1,r2,c1,c2,d1,d2;
    double a,b,c,d;
    while(scanf("%d%d%d%d%d%d",&r1,&r2,&c1,&c2,&d1,&d2)==6)
    {
        double aa,bb,cc,dd,aaa,bbb,ccc,ddd;
        if(r1-d1>0)
        {
            b=(r1-d1+c2)/2;
            a=r1-b;
            c=c1-a;
            d=c2-b;
        }
        else
        {
            d=(d1-r1+c2)/2;
            b=c2-d;
            a=r1-b;
            c=r2-d;
        }
        if(d2-c1>0)
        {
            bb=(d2-c1+r1)/2;
            aa=r1-bb;
            dd=c2-bb;
            cc=r2-dd;
        }
        else
        {
            cc=(c1-d2+r1)/2;
            dd=r2-cc;
            aa=c1-cc;
            bb=c2-dd;
        }
        if(r2-c2>0){
            ccc=(r2-c2+d2)/2;
            ddd=r2-ccc;
            aaa=c1-ccc;
            bbb=c2-ddd;
        }
        else
        {
            bbb=(c2-r2+d2)/2;
            aaa=r1-bbb;
            ddd=c2-bbb;
            ccc=r2-ddd;
        }
        if(a==aa&&c==ccc&&d==ddd&&bb==bbb&&a>0&&b>0&&c>0&&d>0)
        {
                printf("%.0lf %.0lf\n",a,b);
                printf("%.0lf %.0lf\n",c,d);
        }
        else
            printf("-1\n");

    }
    return 0;
}


 

点击复制链接 与好友分享!回本站首页
上一篇:hdu 1861-游船出租
下一篇:编程之美 裴波那楔数列
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站