P2181 对角线
这是一个数学问题,我整理了一下我现在能推的数据,像是n边形就有n个顶点
每个顶点所拥有的对角线除去本顶点和相邻顶点,每个顶点有n-3条对角线,
每条对角线有两个顶点,所以n边形的对角线有(n-3)*n/2条,但是没有向交点上想,
没写出来
推导过程为:任意三条对角线不会只有一个交点,
也就是过一个交点的对角线固定有两条,两条对角线对应四个顶点,
那么任意四个顶点能够确定一个交点,
现在就从规律寻找问题转变成了排列组合的问题,
当n>=4时,交点数是Cn|4,即4!/n!
对于计算时
import java.math.BigInteger;
import java.util.Scanner;
public class Diagonal {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m=1;
int dian = 4*3*2*1;
if (n>=4)
{
for (int i = 0; i < 4; i++) {
m*=n;
n-=1;
}
System.out.println(m/dian);
}else
System.out.println(0);
}
}
int似乎不行,使用long也仅仅能够从55提升到82.计算大数可以用Biginteger类
修改后
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger n = cin.nextBigInteger();
BigInteger Two = BigInteger.valueOf(2);
BigInteger Three = BigInteger.valueOf(3);
BigInteger TF = BigInteger.valueOf(24);
System.out.println(n.multiply(n.subtract(BigInteger.ONE)).multiply(n.subtract(Two).multiply(n.subtract(Three))).divide(TF));
}
}
AC!
P1085 [NOIP2004 普及组] 不高兴的津津
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int[] school = new int[7];
int[] home = new int[7];
int[] sum = new int[7];
for (int i = 0; i < 7; i++) {
school[i]=cin.nextInt();
home[i]=cin.nextInt();
sum[i]=school[i]+home[i];
}
int temp = 0;
for (int i = 0; i < sum.length; i++) {
if (sum[i]>temp)
temp=sum[i];
}
int day = 0;
for (int i = 0; i < sum.length; i++) {
if (sum[i]==temp)break;
day=i+2;
}
System.out.println(day);
}
}
P1055 [NOIP2008 普及组] ISBN 号码
其实过程很好想,用split会很麻烦,既然知道了号码的长度,只需模拟即可,中间使用了substring截取了字符串。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String ISBN = cin.next();
char[] num = ISBN.toCharArray();
int temp = 0, tran = 1;
for (int i = 0; i < num.length - 2; i++) {
if (num[i] != '-') {
temp += Character.getNumericValue(num[i]) * tran;
tran++;
}
}
int ID = temp % 11;
char str = ' ';
if (ID < 10) {
str = (char) (ID + '0');
} else {
str = 'X';
}
if (num[num.length - 1] == str) {
System.out.print("Right");
} else {
System.out.print(ISBN.substring(0, num.length - 1) + str);
}
}
}
AC!