栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

蓝桥杯练习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

蓝桥杯练习题

    class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            HashSet set = new HashSet<>();
           for(int i=0;ik) {
        		   set.remove(nums[i-k]);
        	   }
           }
        		 return false;  
    	    }
        }
    

    2
    一个字符串的非空子串是指字符串中长度至少为1 的连续的一段字符组成
    的串。例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab, aaab,一共7 个。
    注意在计算时,只算本质不同的串的个数。
    请问,字符串0100110001010001 有多少个不同的非空子串?

    public class Main {
        public static void main(String[] args) {
            String a = "0100110001010001";
            HashSet set = new HashSet<>();
            for (int i = 0; i  
    

    3
    给定数列1, 1, 1, 3, 5, 9, 17, …,从第4 项开始,每项都是前3 项的和。求
    第20190324 项的最后4 位数字。

    public class Main {
        public static void main(String[] args) {
           int a=1,b=1,c=1,num=0;
            for (int i = 4; i <=20190324; i++) {
                    num=a+b+c;
                    num=num%10000;
                    a=b%10000;
                    b=c%10000;
                    c=num;
            }
            System.out.println(num);
        }
    }
    
    

    4
    把2019 分解成3 个各不相同的正整数之和,并且要求每个正整数都不包
    含数字2 和4,一共有多少种不同的分解方法?
    注意交换3 个整数的顺序被视为同

    在public class xuexi {
    	 public static void main(String[] args) {
    	        int sum=0;
    	        for (int i = 1; i <2019 ; i++) {
    	            for (int j = i+1; j <2019 ; j++) {
    	                for (int k = j+1; k <2019 ; k++) {
    	                    if(i+j+k==2019) {
    	                	if(f(i)&&f(j)&&f(k)) {
    	                    sum++;	   
    	                       }
    	                    }
    	                }
    	            }
    	        }
    	        System.out.println(sum);
    	    }
    	    public static boolean f(int a)
    	    {
    	        while (a!=0)
    	        {
    	            int c=a%10;
    	            a/=10;
    	            if(c==4||c==2)
    	            {
    	                return false;
    	            }
    	        }
    	        return true;
    	    }
    	}
    

    5
    小明对数位中含有 2、0、1、9 的数字很感兴趣(不包括前导 0),在 1 到 40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。 请问,在 1 到 n 中,所有这样的数的和是多少?

    public class xuexi {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n =sc.nextInt();
    		int sum=0;
    		for(int i=1;i<=n;i++) {
    			int t=i;
    			while(t!=0) {
    			if(t%10==2||t%10==0||t%10==1||t%10==9) {
    				sum+=i;
    				break;
    			}
    			t/=10;
    		}
    		}
    		System.out.println(sum);
    	}
    	}
    
    

    6
    将编号为1~10的10本书排放在书架上,要求编号相邻的书不能放在相邻的位置。
    请计算一共有多少种不同的排列方案。

    public class BookSort {
    
        public static void main(String[] args) {
            int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            fullSort(arr, 0, arr.length - 1);
            System.out.println(res);
        }
    
        static int res = 0;
    
        static void fullSort(int[] arr, int start, int end) {
            // 递归终止条件
            if (start == end) {
                // 求出了全排列的一种情况,然后检查是否满足条件
                if (check(arr))
                    res++;
                return;
            }
            for (int i = start; i <= end; i++) {
                swap(arr, start, i);
                fullSort(arr, start + 1, end);
                swap(arr, start, i);
            }
        }
    
        private static void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    
        static boolean check(int[] arr) {
            for (int i = 1; i < arr.length; i++) {
                if (Math.abs(arr[i] - arr[i - 1]) == 1)
                //求绝对值
                    return false;
            }
            return true;
        }
    
    }
    

    7 * 问题描述
      将LANQIAO中的字母重新排列,可以得到不同的单词,如LANQIAO、AAILNOQ等,注意这7个字母都要被用上,单词不一定有具体的英文意义。
      请问,总共能排列如多少个不同的单词。

     public static void main(String[] args) {
    		    char arr[]={'L','A','N','Q','I','A','O'};
    	        fullSort(arr, 0, arr.length - 1);
    	        System.out.println(set.size());
    	    }
    
    	    public static HashSet set=new HashSet();
    	    static void fullSort(char[] arr, int start, int end) {
    	    	
    	    	// 递归终止条件
    	        if (start == end) {
    	            // 求出了全排列的一种情况,然后检查是否满足条件
    	        	String str=new String(arr);
    	            set.add(str);
    	            return;
    	        }
    	        for (int i = start; i <= end; i++) {
    	            swap(arr, start, i);
    	            fullSort(arr, start + 1, end);
    	            swap(arr, start, i);
    	        }
    	    }
    
    	    private static void swap(char[] arr, int i, int j) {
    	        char tmp = arr[i];
    	        arr[i] = arr[j];
    	        arr[j] = tmp;
    	    }
    
    

    8

    public static void main(String[] args) {
        int[] arr = new int[10];
        for(int i=0;i<10;i++) {
        	arr[i]=2021;
        }
        	for(int j=1;j<5000;j++) {
        	if(del(j,arr)) {
        		System.out.println(j-1);
        		break;
        	}		
        }
        	
    	}
    	public static boolean del(int j,int[] arr) {
    		while(j!=0) {
    			arr[j%10]--;
    			if(arr[j%10]<0) {
    				return true;
    			}
    			j/=10;
    		}
    		return false;
    	}
    	}
    

    9
    给定一个单词,请使用凯撒密码将这个单词加密。
      凯撒密码是一种替换加密的技术,单词中的所有字母都在字母表上向后偏移3位后被替换成密文。即a变为d,b变为e,…,w变为z,x变为a,y变为b,z变为c。
      例如,lanqiao会变成odqtldr。
    输入格式
      输入一行,包含一个单词,单词中只包含小写英文字母。
    输出格式
      输出一行,表示加密后的密文。

    public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		String ans = sc.nextLine();
    		String sum ="";
    		char[] c=ans.toCharArray();
    		for(int i=0;i 
    

    10
    2020 年 7 月 1 日是中国共产党成立 99 周年纪念日。
    中国共产党成立于 1921 年 7 月 23 日。
    请问从 1921 年 7 月 23 日中午 12 时到 2020 年 7 月 1 日中午 12 时一共包
    含多少分钟?

     public static void main(String[] args) throws ParseException {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = format.parse("1921-7-23 12:00:00");
            Date end = format.parse("2020-7-1 12:00:00");
            System.out.print(((end.getTime() - start.getTime()) / 1000 / 60 / 60) * 60);
        }
    

    10

    import java.util.Scanner;
    public class F {
    	public static void main(String[] args) {
    		Scanner scanner=new Scanner(System.in);
    		int n=scanner.nextInt();
    		int max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
    		double sum=0;
    		for(int i=0;i 
    

    11 单词分析
    记录每个字符的出现次数即可

    public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		char []ch = sc.nextLine().toCharArray();
    		int[] arr = new int[26];
    		for(int i=0;ival) {
    			val=arr[i];
    			index=i;
    		}
    	}
    		System.out.println((char)('a'+index)+"n"+val);
    	}
    

    12
    对于一个字符串 S,我们定义 S 的分值 f(S) 为 S 中出现的不同的字符个 数。
    例如 f(”aba”) = 2,f(”abc”) = 3, f(”aaa”) = 1。
    现在给定一个字符串 S[0…n−1](长度为 n),
    请你计算对于所有 S 的非空 子串 S[i…j](0≤i≤ j < n),f(S[i…j]) 的和是多少。

    public static void main(String[] args) {
    		Scanner scanner=new Scanner(System.in);
    		String string=scanner.next();
    		char c[]=string.toCharArray();
    		long ans=0;
    		for(int i=0;i set=new HashSet();
    			for(int j=i;j 
    

    13
    如果一个整数 g 能同时整除整数 A 和 B,则称 g 是 A 和 B 的公约数。例如:43 是 86 和 2021 的公约数。
    请问在 1(含) 到 2021(含) 中,有多少个数与 2021 存在大于 1 的公约数。

    请注意 2021 和 2021 有大于 1 的公约数,因此在计算的时候要算一个。

    gcd 函数为Java中求公约数函数,从1~2021进行逐个查找,当其与自身的公约数大于1,即大于等于2时,说明其存在公约数,进行 count 计数

    public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		int count = 0;
    		for (int i = 1; i <= 2021; i++) {
    			if (gcd(i, 2021) > 1) {
    				count++;
    			}
    		}
    		System.out.println(count);
    	}
     
    	static int gcd(int a, int b) {
    		return b > 0 ? gcd(b, a % b) : a;
    	}
    
    

    14
    2021 是一个非常特殊的数,它可以表示成两个非负整数的平方差,2021 = 45 * 45 - 2 * 2。
    2025 也是同样特殊的数,它可以表示成 2025 = 45 * 45 - 0 * 0。请问,在 1 到 2021 中有多少个这样的数?
    请注意,有的数有多种表示方法,例如 9 = 3 * 3 - 0 * 0 = 5 * 5 - 4 * 4,在算答案时只算一次。

    通过嵌套循环进行迭代循环,达到全匹配的目的,这里涉及到一个数学问题,sum= aa - bb 也即 sum = (a+b)(a-b) ,当sum为正数时,b

    public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		int count = 0;
    		int[] arr = new int[2022];
    		for (int i = 1; i <= 1011; i++) {
    			for (int j = 0; j < i; j++) {
    				int sum = i * i - j * j;
    				if (sum <= 2021) {
    					arr[sum] = 1;
    				}
    			}
    		}
    		for (int j = 1; j <= 2021; j++) {
    			if (arr[j] == 1)
    				count++;
    		}
    		System.out.println(count);
    	}
    

    15
    下面的矩阵中包含 ABCDEF 六种字符,请问出现最多的字符出现了几次?
    put(K key,V value)
    key:是要保存到 Map 集合中的键名。
    value:是要保存到 Map 集合中对应键名的键值对象。
    map中的containsKey(key)方法是判断该key在map中是否有key存在。如果存在则返回true。如果不存在则返回false

    public static void main(String[] args) {
    		Map map = new HashMap<>();
    		for(int i = 0; i < chars.length(); i++) {
    			char ch = chars.charAt(i);
    			if(ch == 'A' || ch == 'B' || ch == 'C' || ch == 'D' || ch == 'E' || ch == 'F') {
    				if(!map.containsKey(ch)) {
    					map.put(ch, 1);
    				} else {
    					map.put(ch, map.get(ch) + 1);
    				}
    			}
    		}
    		System.out.println(map.get('A'));
    		System.out.println(map.get('B'));
    		System.out.println(map.get('C'));
    		System.out.println(map.get('D'));
    		System.out.println(map.get('E'));
    		System.out.println(map.get('F'));
    	}
    
    

    16
    一个 1 到 n 的排列被称为半递增序列,是指排列中的奇数位置上的值单调递增,偶数位置上的值也单调递增。
    例如:(1, 2, 4, 3, 5, 7, 6, 8, 9) 是一个半递增序列,因为它的奇数位置上的值是 1, 4, 5, 6, 9,单调递增,偶数位置上的值是 2, 3, 7, 8,也是单调递增。
    请问,1 到 n 的排列中有多少个半递增序列?
    输入格式
    输入一行包含一个正整数 n。
    输出格式
    输出一行包含一个整数,表示答案,答案可能很大,请输出答案除以 1000000007 (le9+7)的余数。

    public static long group(long a, long b) {
    		long ans = 1;
    		int rem = (int) 1e9 + 7;
    		for (long i = 1, j = a; i <= b; i++, j--) {
    			ans = ans * (j / i) % rem;
    		}
    		return ans;
    	}
     
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		long n = input.nextLong();
    		if (n % 2 == 0) {
    			System.out.println(group(n, n / 2));
    		} else
    			System.out.println(group(n, (n + 1) / 2));
    	}
    

    17
    一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值。
    每次只能走到下一层相邻的数上,例如从第3层的6向下走,只能走到第4层的2或9上。

    该三角形第n层有n个数字,例如:

    第一层有一个数字:5

    第二层有两个数字:8 4

    第三层有三个数字:3 6 9

    第四层有四个数字:7 2 9 5

    最优方案是:5 + 8 + 6 + 9 = 28

    public static void main(String[] args) {
    		         Scanner scan = new Scanner(System.in);
    		 
    		         int n = scan.nextInt();
    		         long max = 0;
    		         int[][] dp = new int[n][n];
    		         dp[0][0] = scan.nextInt();
    		
    		        for(int i=1;i 
    
    public static void main(String[] args) {
    		        Scanner sc = new Scanner(System.in);
    		        int n = sc.nextInt();
    		        long max = 0;
    
    		        int[][] r = new int[n+1][n+1];
    		        for(int i=1; i<=n; ++i) {
    		            for (int j = 1; j <= i; ++j) {
    		                r[i][j] = sc.nextInt();
    		                r[i][j] += Math.max(r[i - 1][j - 1], r[i - 1][j]);
    		                max = Math.max(r[i][j],max);
    		            }
    		        }
    		        System.out.println(max); //分奇数和偶数
    		    }
    

    18、
    上图给出了一个数字三角形。从三角形的顶部到底部有很多条不同的路径。
    对于每条路径,把路径上面的数加起来可以得到一个和,你的任务就是找到最大的和。
    路径上的每一步只能从一个数走到下一层和它最近的左边的那个数或者右边的那个数。此外,向左下走的次数与向右下走的次数相差不能超过 1。
    时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分
    输入格式
    输入的第一行包含一个整数 N (1 < N ≤ 100),表示三角形的行数。下面的N 行给出数字三角形。数字三角形上的数都是 0 至 100 之间的整数
    输出格式
    输出一个整数

    输入样例
    5
    7
    3 8
    8 1 0
    2 7 4 4
    4 5 2 6 5

    输出样例
    27
    动态规划+贪心版
    这个思路相对于不太容易理解一些,我来慢慢梳理。每次赋值的时候就更新dp表,r[i][j] += Math.max(r[i - 1][j - 1], r[i - 1][j]);的意思就是选择左上角或者右上角较大的累加,先不论左右选择差值的问题,因为根据题目中说的可以到最后再进行判断。最后的判断有两种情况:如果n为奇数,那么只有一种选择的可能,就是选择最后一行的的中间的数,比如下图,n=3,只能选择1那个位置的数;而如果n为偶数,就有两种可能了,只能选择7或者4的位置,选择累加和较大的那个。

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int[][] r = new int[n+1][n+1];
            for(int i=1; i<=n; ++i) {
                for (int j = 1; j <= i; ++j) {
                    r[i][j] = sc.nextInt();
                    r[i][j] += Math.max(r[i - 1][j - 1], r[i - 1][j]);
                }
            }
            System.out.println(n%2==1?r[n][n/2+1]:Math.max(r[n][n/2], r[n][n/2+1])); //分奇数和偶数
        }
    
    

    19
    一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值。例如:3 * 3的方格。

    1 3 3
    2 1 3
    2 2 1

    能够获得的最大价值为:11。

    public static void main(String[] args) {
    		        Scanner sc = new Scanner(System.in);
    		        int n = sc.nextInt();
    		        long max = 0;
    
    		        int[][] r = new int[n+1][n+1];
    		        for(int i=1; i<=n; i++) {
    		           for(int j=1;j<=n;j++) {
    		        	   r[i][j]=sc.nextInt();
    		        	   r[i][j]+=Math.max(r[i-1][j], r[i][j-1]);
    		           }
    		        }
    		        System.out.println(r[n][n]); 
    		    }
    

    20
    最长递归子序列
    [1,2,6,5,3,4] 6
    返回 4

    public static void main(String[] args) {
    			//5 1 6 8 2 4 5 10
    	        Scanner scan = new Scanner(System.in);
    	        int n = scan.nextInt();
    	        int[] num = new int[n];
    	        for(int i=0;i 
    

    21
    下面是r进制转10进制

    r进制转10进制 方法
    radix进制的字符串s转10进制 Integer.parseInt((String) s,(int) radix);

    22
    在nn方阵里填入1,2,3,…,nn,要求填成蛇形。例如n=4时方阵为:

    10 11 12 1

    9 16 13 2

    8 15 14 3

    7 6 5 4

    上面的方阵中,多余的空格只是为了便于观察规律,不必严格输出,n<=8。
    

    输入:一个整数n,n≤8。

    输出: n行,每行n个整数,用空格分隔。

    public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int [][]arr = new int[n][n];
    		int x=0;
    	    int y=0;
    		int c=0;
    		int z=1;//给数组赋值
    		while(true) {
    			if(z>n*n) {			
    				break;
    			}
    		for(x=c,y=c ;y<=n-1-c;y++) {
    		  arr [x][y]=z;
    		z++;
    		}	
    
    		for(x=c+1,y=n-1-c;x<=n-1-c;x++) {
    		   arr[x][y]=z;
    		z++;
    		}
    
    		for(x=n-c-1, y=n-2-c;y>c-1;y--) {
    			arr[x][y]=z;
    		z++;
    		}
    
    		for(x=n-c-2,y=c;x>c;x--) {
    			arr[x][y]=z;
    		z++;
    		}
    
    		c++;
    		    }
    		for(int i=0;i 
    

    22
    这种方法的具体描述如下:假设 maxnmaxn 是单词中出现次数最多的字母的出现次数,minnminn 是单词中出现次数最少的字母的出现次数,如果 maxn-minnmaxn−minn 是一个质数,那么笨小猴就认为这是个 Lucky Word,这样的单词很可能就是正确的答案。
    输出两行,第一行是一个字符串,假设输入的的单词是Lucky Word,那么输出Lucky Word,否则输出No Answer;

    第二行是一个整数,如果输入单词是 Lucky Word,输出 maxn-minnmaxn−minn 的值,否则输出 0。

     public static void main(String[] args) {
            
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine(); // 从键盘输入一个字符串
            char[] ch = str.toCharArray(); // 把字符串转换为字符数组
            int[] a = new int[100]; // 因为最多有100个字母,用于存储str中每个位置字母的个数
            int maxn = 0, minn = 100; // 因为当100字母都是相同的则最小就为100
            for (int i = 0; i < ch.length; i ++){//找出每个位置字母在整个字符串中相同字母的个数
                int num = 0;
                for (int j = 0; j < ch.length; j ++){
                    if (ch[j] == ch[i]){
                        num ++;
                    }
                }
                a[i] = num;
            }
            for (int i = 0; i < ch.length; i ++){ // 根据比较大小来判断数组a中谁最大谁最小
                if (maxn < a[i]){
                    maxn = a[i];
                }
                if (minn > a[i]){
                    minn = a[i];
                }
            }
            if (Prime(maxn , minn) == true){
                System.out.println("Lucky Word");
                System.out.println(maxn - minn);
            } else {
                System.out.println("No Answer");
                System.out.println("0");
            }
        }
        public static boolean Prime(int a, int b){ // 自定义一个方法判断是不是质数
            if (a - b < 2)
                return false; // 利用Math.sqrt(double)将a-b强制转换为duoble型
            for (int i = 2; i <= Math.sqrt((double) (a - b)); i ++){
                if ((a - b) % i == 0){
                    return false;
                }
            }
            return true;
    
            
        }
    

    23
    明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了 NN 个 1 到 1000 之间的随机整数(N leq 100N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。

    输入描述
    第 1 行为 1 个正整数,表示所生成的随机数的个数:NN。

    第 2 行有 NN 个用空格隔开的正整数,为所产生的随机数。

    输出描述
    输出 2 行,第 1 行为 1 个正整数 MM,表示不相同的随机数的个数。

    第 2 行为 MM 个用空格隔开的正整数,为从小到大排好序的不相同的随机数。

     public static void main(String[] args) {
            
    	        Scanner sc = new Scanner(System.in);
    	    	int N =sc.nextInt();
    	    	int[] arr =new int[N];
    	      for(int i=0;i set = new HashSet<>();
    	      for(int i=0;i 
    

    24
    学校里有一个水房,水房里一共装有 mm 个龙头可供同学们打开水,每个龙头每秒钟的供水量相等,均为 1。

    现在有 nn 名同学准备接水,他们的初始接水顺序已经确定。将这些同学按接水顺序从 1 到 nn编号,ii 号同学的接水量为 w_iw
    i

    。接水开始时,1 到 mm 号同学各占一个水龙头,并同时打开水龙头接水。当其中某名同学 jj 完成其接水量要求 w_iw
    i

    后,下一名排队等候接水的同学 kk 马上接替 j 同学的位置开始接水。这个换人的过程是瞬间完成的,且没有任何水的浪费。即 jj 同学第 xx 秒结束时完成接水,则 kk 同学第 x+1x+1 秒立刻开始接水。若当前接水人数 n’n’不足 mm,则只有 n’n’个龙头供水,其它 m−n’m−n’个龙头关闭。

    现在给出 nn 名同学的接水量,按照上述接水规则,问所有同学都接完水需要多少秒。

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            int m=sc.nextInt();
            int a[]=new int[n];
            for (int i = 0; i < n; i++) {
                a[i]=sc.nextInt();
            }
            int time=0;
            if(m==1) {
                for (int i = 0; i < n; i++) {
                    time += a[i];
                }
            }
            else {
                for (int i = m; i < n; i++) {
                    int min=0;
                    for (int j = 1; j < m; j++) {
                        if(a[j]a[i]?time:a[i];
                }
            }
            System.out.println(time);
    
            
        }
    

    25
    国王将金币作为工资,发放给忠诚的骑士。第一天,骑士收到一枚金币;之后两天(第二天和第三天),每天收到两枚金币;之后三天(第四、五、六天),每天收到三枚金币;之后四天(第七、八、九、十天),每天收到四枚金币…;这种工资发放模式会一直这样延续下去:当连续 NN 天每天收到 NN 枚金币后,骑士会在之后的连续 N+1N+1 天里,每天收到 N+1N+1 枚金币。

    请计算在前 KK 天里,骑士一共获得了多少金币。

     public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int K= scan.nextInt();
            int day=0;
            int sum=0;
            for(int i=1;i<=K;i++){
              for(int j=1;j<=i;j++){
                sum+=i;
                day++;
                if(day==K){
                   System.out.println(sum);
                }
    
              }
            }
        }
    

    26
    给定两个整数 a 和 b,请你求出这两个整数的和。

    输入描述
    输入两个正整数 a, b, a 和 b 都不超过 100100 位。

    输出描述
    输出 a+b。

      public static void main(String[] args) {
         Scanner scan = new Scanner(System.in);
         String a = scan.next();
         BigInteger aa = new BigInteger(a);
         String b = scan.next();
         BigInteger bb = new BigInteger(b);
         BigInteger cc = aa.add(bb);
         System.out.println(cc);
         scan.close();
     }
      //加法
            BigDecimal result1 = num1.add(num2);
            BigDecimal result12 = num12.add(num22);
     
            //减法
            BigDecimal result2 = num1.subtract(num2);
            BigDecimal result22 = num12.subtract(num22);
     
            //乘法
            BigDecimal result3 = num1.multiply(num2);
            BigDecimal result32 = num12.multiply(num22);
     
            //绝对值
            BigDecimal result4 = num3.abs();
            BigDecimal result42 = num32.abs();
     
            //除法
            BigDecimal result5 = num2.divide(num1,20,BigDecimal.ROUND_HALF_UP);
            BigDecimal result52 = num22.divide(num12,20,BigDecimal.ROUND_HALF_UP);
    
    

    27
    小蓝每天都锻炼身体。

    正常情况下,小蓝每天跑 11 千米。如果某天是周一或者月初(11 日),为了激励自己,小蓝要跑 22 千米。如果同时是周一或月初,小蓝也是跑 22 千米。

    小蓝跑步已经坚持了很长时间,从 20002000 年 11 月 11 日周六(含)到 20202020 年 1010 月 11 日周四(含)。请问这段时间小蓝总共跑步多少千米?

    public static void main(String[] args) {
            Calendar c = Calendar.getInstance();
            c.set(2000,0,1);
            int year = 0;
            int month = 0;
            int day = 0;
            int week = 0;
            int sum = 0;
            while(true) {
                if(year==2020&&month==10&&day==1) {
                    break;
                }
                year = c.get(Calendar.YEAR);
                month = c.get(Calendar.MONTH)+1;//月从0开始
                day = c.get(Calendar.DAY_OF_MONTH);
                week = c.get(Calendar.DAY_OF_WEEK);//周日为1
                if(week==2||day==1) {
                    sum = sum + 2;
                }else {
                    sum = sum + 1;
                }
                c.add(Calendar.DAY_OF_MONTH,1);
            }
            System.out.println(sum);
            
        }
    

    28
    问题描述
      FJ在沙盘上写了这样一些字符串:
      A1 = “A”
      A2 = “ABA”
      A3 = “ABACABA”
      A4 = “ABACABADABACABA”
      … …
      你能找出其中的规律并写所有的数列AN吗?
    输入格式
      仅有一个数:N ≤ 26。
    输出格式
      请输出相应的字符串AN,以一个换行符结束。输出中不得含有多余的空格或换行、回车符。
    样例输入
    3
    样例输出
    ABACABA

     public static void main(String args[]) 
        { 
            Scanner cn=new Scanner(System.in); 
            int t=cn.nextInt(); 
            System.out.println(kk("",0,t)); 
        } 
           
        public static String kk(String str,int numb,int t)   //利用递归的方式 
        { 
            if(t==numb)return str; 
            else  
            { 
                str=str+(char)('A'+numb)+str; 
                str=kk(str,numb+1,t); 
            } 
            return str; 
        } 
    
    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/781663.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号