附:友情链接之 Tsinsen 第一卷 题解 1000-1024(已完结)
1034 孪生素数对
import java.util.Scanner;
public class Main {
public static void f(int n1,int n2){
int first = 0;
int second = 0;
int count = 0;
for (int i = n1; i <= n2 - 2; i++) {
first = 1;
second = 1;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
first = 0;
break;
}
}
for (int j = 2; j <= Math.sqrt(i + 2); j++) {
if ((i + 2) % j == 0) {
second = 0;
break;
}
}
if (first == 1 && second == 1) {
System.out.println(i + " " + (i + 2));
count++;
}
}
if (count == 0) {
System.out.println("-1");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int n2 = sc.nextInt();
sc.close();
if (n1 == 1) {
f(2,n2);
} else{
f(n1,n2);
}
}
}
1035 素数之和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int N = sc.nextInt();
if(N==0){
break;
}
int[] a = new int[N + 1];
int i, j;
int sum = 0;
for (i = 2; i <= N; i++) {
a[i] = i;
}
for (i = 2; i <= N / 2; i++) {
if (a[i] != 0) {
for (j = i + i; j <= N; j += i)
a[j] = 0;
}
}
for (i = 2; i <= N; i++) {
if (a[i] != 0)
sum += a[i];
}
System.out.println(sum);
}
}
}
1037 下楼问题
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int []a = new int[30];
int n = sc.nextInt();
if(n<4 || n>20){
System.out.println(0);
}
else{
a[0] = 1;
a[1] = 1;
a[2] = 2;
for(int i=3;i<=n;i++){
a[i] = a[i-1] + a[i-2] + a[i-3];
}
System.out.println(a[n]);
}
}
}
1038 超级重复串
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
char []a = new char[101];
a = sc.next().toCharArray();
int result = 0;
for(int i=0;i<a.length-1;i++){
for(int j=i+1;j<a.length;j+=2){
int L = (j-i+1)/2;
int flag=0;
for(int k=0;k<L;k++){
if(a[i+k]!=a[i+k+L]){
flag=1;
break;
}
}
if(flag==0){
if(j-i+1>result){
result=j-i+1;
}
}
}
}
System.out.println(result);
}
}
1039 欢乐的跳跃者
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []a = new int[n+1];
int []b = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
for(int i=0;i<n-1;i++){
b[i] = Math.abs(a[i]-a[i+1]);
}
Arrays.sort(b);
int flag=0;
for(int i=0;i<n-1;i++){
if(b[i]!=i){
flag=1;
}
}
if(flag==0){
System.out.println("I'm happy!!");
}
else{
System.out.println("I'm unhappy!!");
}
}
}
1040 Cantor表
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int temp = n;
int i;
for (i = 1; i < temp; i++) {
temp = temp - i;
}
if (i % 2 == 0) {
System.out.printf("%d/%d", temp, i + 1 - temp); //偶数排,右上→左下
} else {
System.out.printf("%d/%d", i + 1 - temp, temp); //奇数排,左下→右上
}
}
}