1.数列求值
解法一
在每次求和之后都取10000的模就不会超范围
public class Main {
public static void main(String[] args) {
int a = 1, b = 1, c = 1, d = 0;
for(int i = 4; i <= 20190324; i++){
d = (a + b + c) % 10000 ;
a = b;
b = c;
c = d;
}
System.out.println(d);
}
}
解法二
滚动数组
public class Main {
public static void main(String[]args){
int[]a=new int[20190324];
a[0]=1;a[1]=1;a[2]=1;a[3]=3;
for(int i=4;i<20190324;i++){
a[i]=(a[i-1]+a[i-2]+a[i-3])%10000;
}
System.out.println(a[20190323]);
}
}
2.质数
public class Main {
public static void main(String[] args) {
int x = 2;
int cnt = 1;
while (cnt < 2019) {
x++;
if (isPrime(x)) cnt++;
}
System.out.println(x);
}
static boolean isPrime(int x) {
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) return false;
}
return true;
}
}
3.饮料换购
import java.util.Scanner;
public class MainMain {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int cnt = n;
while(n>=3){
int a = n/3;//每三个瓶盖换一个饮料
n %=3;
cnt += a;//换来的饮料加进去
n += a;//换来的饮料喝完的瓶盖接着换
}
System.out.println(cnt);
}
}