Tsinsen 题解 1034-1043(已完结)

附:友情链接之 Tsinsen 第一卷 题解 1000-1024(已完结)          

                        Tsinsen 第一卷 题解 1025-1033(已完结)  


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);
        }
    }
}


1036 分解质因数 —见 详解


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); //奇数排,左下→右上
        }
    }
}


1041 矩阵乘法 —见 详解


1042 矩阵面积交 —见 详解


1043 完美的代价 —见 详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值