「題目程式碼」P1013~P1017(Java)

SamHX發表於2018-05-15

 

1013 C基礎-求偶數和

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt(),sum=0;
        while(T--!=0)
        {
            int a=sc.nextInt();
            if(a%2==0) sum+=a;
        }
        System.out.println(sum);
    }

}

1014 C基礎-等差數列

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        //2+3*(n-1)=3n-1
        //(3n+1)*n/2
        System.out.println((3*n+1)*n/2);
    }

}

1015 C基礎-同因查詢

import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        for(int i=10;i<=1000;++i)
        {
            if(i%2==0 && i%3==0 && i%7==0)
                System.out.println(i);
        }

    }

}

1016 Roliygu and Yilan

一道博弈題。注意到任意一個偶數邊長正方形都可以被$1\times 2$的方塊密鋪(為什麼考慮$1\times 2$?因為兩方操作一定構成一個這樣的方塊),那麼$n$為偶數,一定是先手必勝。$n$如果為奇數,那麼去掉第一塊剩下的能夠實現密鋪,最後一定是後手必勝。

//Adapted from HDU 1564
/* We can guess that R wins when n is even and Y wins when n 
 * is odd (simply make some examples, and you will get it). 
 * But why? A good explation comes from
 * https://blog.csdn.net/strokess/article/details/52136677.
 * Given an even n, the square shall be covered by 2*1 grids,
 * which means the upper-hand player is always capable of 
 * moving to a 2*1 grid. Therefore, he will always win. What
 * about an odd one? x^2-1, and it will be fully covered, so an
 * after-hand player will win. 
 */
import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext())
        {
            int n=cin.nextInt();
            if(n==0) break;
            if(n%2==0) System.out.println("Roliygu");
            else System.out.println("Yilan");
        }

    }

}

1017 平面切割(特別版)

遞迴思想處理。
我們考慮對n-1的情況再加一條閃電。想要新閃電帶來最多的分割,那麼這“三條線”必須各自穿過每一條邊。這樣一來,每條線都會帶來$3(n-1)$的額外分割;此外,這條閃電還把公共部分隔開了。因此,需要額外再+1。
這樣遞推公式就得到了。


import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main
{
    static long f(int x)
    {
        if(x==1) return 2;
        else return 9*(x-1)+1+f(x-1);
    }
    public static void main(String args[])
    {
        Scanner cin=new Scanner(System.in);
        int T=cin.nextInt();
        while(T--!=0)
        {
            int n=cin.nextInt();
            System.out.println(f(n));
        }

    }
}

相關文章