「题目代码」P1013~P1017(Java)
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));
}
}
}
如非注明,原创内容遵循GFDLv1.3发布;其中的代码遵循GPLv3发布。