算法实践-简单篇2

本文介绍了几个基础的算法实践,包括蛇形矩阵、斐波那契兔群问题、字符统计、杨辉三角的变形、完全数计算、素数配对、递归放苹果、位运算计数1和日期转换等,涵盖了数据结构和逻辑运算的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 蛇形矩阵
 * 当输入5时,应该输出的三角形为:
 * 1 3 6 10 15
 * 2 5 9 14
 * 4 8 13
 * 7 12
 * 11
 */
import java.util.Scanner;
public class Test1{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int n = in.nextInt();    //读入正整数n

            int[][] result = new int[n][];    //建立数组(n行)
            int t = 1;    //记录依次赋予的数组值
            for(int i=0; i < n; i++){
                result[i] = new int[n-i];    //数组第i行有n-i个元素
                for(int j=0; j < i+1; j++){    //对第i个对角线赋值
                    result[i-j][j] = t;
                    t++;
                }
            }

            //输出数组值
            for(int[] a : result){
                for(int a1 : a)
                    System.out.print(a1 + " ");
                System.out.println();
            }
        }
    }
}
/**
 * 统计每个月兔子的总数
 * 题目描述 有一只兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第三个月后每个月又生一只兔子,假如兔子都不死,问第n个月的兔子总数为多少?
 */
import java.util.*;
public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            System.out.println(solution(scanner.nextInt()));
        }
    }
    private static int solution(int month) {
        // 第一个月初始化
        // 一月龄兔子总数
        int oneMonth = 1;
        // 二月龄兔子总数
        int twoMonth = 0;
        // 三月龄及以上兔子总数
        int threeMonth = 0;
        // 下个月将繁殖的兔子数量
        int addVal = 0;
        // 第二个月开始递推, i表示第i个月
        for(int i = 2; i <= month; i++) {
            // 三月龄及以上兔子总数 = 二月龄兔子总数 + 原本三月龄及以上兔子总数
            threeMonth += twoMonth;
            // 二月龄兔子总数 = 上个月的一月龄兔子总数
            twoMonth = oneMonth;
            // 一月龄(即这个月出生)兔子总数 = 上个月将繁殖的兔子数量
            oneMonth = addVal;
            // 下个月将出生的兔子 = 下个月成为三月龄及以上的兔子数量
            addVal = twoMonth + threeMonth;
        }
        return (oneMonth + twoMonth + threeMonth);
    }
}
/**
 * 统计字符
 * 输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
 */
import java.util.*;
public class Test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String s = scanner.nextLine();
            char[] arr = s.toCharArray();
            int a = 0;
            int b=0;
            int c=0;
            int d=0;
            for(char ch:arr){
                if(('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z')){
                    a++;
                }
                else if(ch==' '){
                    b++;

                }else if('0'<=ch&&ch<='9'){
                    c++;
                }else {
                    d++;
                }
            }
            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
            System.out.println(d);
        }

    }

}
/**
 * 杨辉三角的变形
 * 以上三角形的数阵,第一行只有一个数1,以下每行的每个数,是恰好是它上面的数、左上角数和右上角的数,3个数之和(如果不存在某个数,认为该数就是0)。
 * 求第n行第一个偶数出现的位置。如果没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3。
 * 只有n为1,2时,没有出现偶数,剩下的按照2 3 2 4的规律每四行循环一次。
 */
import java.util.Scanner;
public class Test4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int num = in.nextInt();
            if(num == 1 || num == 2){
                System.out.println(-1);
                continue;
            }
            else if(num % 4 == 1 || num % 4 == 3){
                System.out.println(2);
                continue;
            }
            else if(num % 4 == 0){
                System.out.println(3);
                continue;
            }
            else if(num % 4 == 2){
                System.out.println(4);
                continue;
            }
        }
    }
}
/**
 * 完全数计算
 * 它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身。
 * 例如:28,它有约数1、2、4、7、14、28,除去它本身28外,其余5个数相加,1+2+4+7+14=28。
 * 输入n,请输出n以内(含n)完全数的个数。
 */
import java.util.Scanner;
public class Test5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int n = scanner.nextInt();
            int res = 0;
            for (int i = 1; i <= n; i++) {
                int[] arr=new int[i];
                int num = 0;
                for(int j=1;j<i;j++){
                    if(i%j==0){
                        arr[j]=j;
                    }else{
                        arr[j]=0;
                    }
                }
                for(int a:arr){
                    num+=a;
                }
                if(num==i){
                    res++;
                }
            }
            System.out.println(res);
        }
    }
}
/**
 * 查找组成一个偶数最接近的两个素数
 * 任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,本题目要求输出组成指定偶数的两个素数差值最小的素数对。4≤n≤1000
 * 输入 :20
 * 输出:
 * 7
 * 13
 */
import java.util.*;

public class Test6 {

    public Test6() {
    }
    private boolean isPrime(int num) {
        for (int i = 2; i <= num/i; i++) {
            if (num % i == 0) return false;
        }
        return true;
    }
    public int count(int n) {
        int i = n/2, j = n - i;
        while (!isPrime(i) || !isPrime(j)) {
            i++;
            j--;
        }
        return j;
    }
    public static void main(String[] args) {
        Test6 solution = new Test6();
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = Integer.parseInt(in.next());
            int res = solution.count(n);
            System.out.println(res);
            System.out.println(n - res);
        }
    }
}
/**
 * 放苹果
 * m个苹果放在N个盘子里(递归)
 * 采用递归的思想将此事件无限细分,每个事件可以分为f(m,n)=f(m-n,n)+f(m,n-1);
 * f(m-n,n)是当苹果数大于等于盘子数的情况,f(m,n-1)是当苹果数小于盘子数的情况。
 * 当此事件分到苹果数为0或苹果数为1或盘子数为1的时候返回1,当苹果数小于0或盘子数小于等于0返回0
 */
import java.util.Scanner;
public class Test7{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt())
        {
            System.out.println(count(sc.nextInt(),sc.nextInt()));
        }
        sc.close();
    }
    public static int count(int m,int n)
    {
        if(m<0||n<=0)
            return 0;
        //细分到苹果数为一或盘子数为一的情况返回一
        if(m==1||n==1||m==0)
            return 1;
        //将此事件无线细分
        return count(m,n-1)+count(m-n,n);
    }
}
/**
 * 查找二进制中1的个数
 * 位运算
 */

import java.util.*;

public class Test8 {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()){
            int in = input.nextInt();
            int count=0;
            while(in!=0){
                if(in%2==1) count++;
                in=in>>1;
            }
            System.out.println(count);
        }
    }
}
/**
 * 计算日期到天数转换
 */

import java.util.Scanner;
public class Test9{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            int[] month_day = {31,28,31,30,31,30,31,31,30,31,30,31};
            int sum = 0;
            for(int i = 0; i < month - 1; i++){
                sum += month_day[i];
            }
            sum += day;
            if(month > 2 && is_leap(year)){
                sum += 1;
            }
            System.out.println(sum);
        }
    }
    public static boolean is_leap(int n){
        if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0){
            return true;
        }else{
            return false;
        }
    }
}
/**
 * 整型数组合并
 * treeSet有序不重复
 */

import java.util.*;
public  class  Test10 {
    public static  void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
            Set<Long> set = new TreeSet<>();
            //接收第一个整形数组大小
            int size1 = sc.nextInt();
            for (int i = 0; i < size1; i++) {
                set.add(sc.nextLong()); //将该组的整数按数组大小循环添加进 set
            }
            //接收第一个整形数组大小
            int size2 = sc.nextInt();
            for (int i = 0; i < size2; i++) {
                set.add(sc.nextLong());
            }
            //遍历输出
            for (long n : set) {
                System.out.print(n);
            }
          /*
          注意:测试案例会以 两个整形数组 为一组测试用例, 并可能输入多组
          要记得组与组的结果之间换行
          */
            System.out.println();
        }

    }
}
/**
 * 最长回文子串
 */
import java.util.*;
public class Test11 {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        int max = 0;
        /**
         *双指针遍历找到最长子串
         */
        for (int i = 0; i < s.length(); i++) {
            for (int j = s.length(); j > i; j--) {
                String toBeJuged = s.substring(i, j);
                if (isPalindromeString(toBeJuged)) {
                    max = Math.max(max, j - i);
                }
            }
        }
        System.out.print(max);
    }

    /**
     *判断一个字符串是否是回文字符串的方法
     */
    static boolean isPalindromeString(String s) {
        return s.equals(new StringBuilder(s).reverse().toString());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值