给定周长下的三角形数量 Java Edition

类别:Java 点击:0 评论:0 推荐:

基本思路为,确定最长边的范围,对每一种情况求和,最后加以修正


import java.util.Scanner;
import java.io.FileInputStream;

/**
 * Created by IntelliJ IDEA.
 * User: Yu Fan
 * Date: 2005-4-23
 * Time: 14:01:29
 * To change this template use File | Settings | File Templates.
 */
public class B {
    static Scanner in;
    static
    {
        try
        {
        System.setIn(new FileInputStream("B.txt"));
        in = new Scanner(System.in);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] arg) {
        while(in.hasNext())
        {
            long q=in.nextLong();
            long ans=in.nextLong();
            long my=0;
            long i;
            if(q%3==0)
                i=q/3;
            else
                i=q/3+1;
            long p;
            if(q%2==0)
                p=q/2;
            else
                p=q/2+1;
            my=(3*i+3*p-2*q-1)*(p-i)/4;
            if((p-i)%4==0)
            {
                my+=(p-i)/4;
            }
            else if((p-i)%4==1)
            {
                if((3*i-q+1)%2==0)
                {
                    my+=(p-i)/4;
                }
                else
                {
                    my+=(p-i)/4+1;
                }
            }
            else if((p-i)%4>=2)
            {
                my+=(p-i)/4+1;
            }
            /*  以上为下面循坏的级数求和
            for(i=i;i<p;i++)
            {
                long k;
                k=3*i-q+1;
                if(k%2==1)
                    k=k/2+1;
                else
                    k=k/2;
                my+=k;
            }
            */
            if(my!=ans)
            {
                System.out.println("Bad luck!");
            }
            else
            {
                System.out.println("Congratulations!");
            }
        }
    }
}

本文地址:http://com.8s8s.com/it/it10111.htm