Blue Bridge Cup - java (group b) provincial

        content

        1. Arithmetic sequence

Problem solving ideas:

The source code is attached:

 

 2. Sunday at the end of the century

Problem solving ideas 

Commonly used calendar fields

The source code is attached: 

1. Arithmetic sequence

arithmetic progression

Problem solving ideas:

The source code is attached:

import java.util.Scanner;
import java.util.Arrays;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt(); //输入n个数
        int [] arr=new int[n];
        for(int i=0;i<arr.length;i++){
          arr[i]=scan.nextInt(); //输出 n里面的每一位数字
        }
        Arrays.sort(arr,0,n);//进行升序排列
        int d=0;
        for(int i=1;i<n-1;i++){  //求相邻两个数最大公约数
          d=gcd(d,arr[i+1]-arr[i]);
        }
        if(d==0){  //如果公差为0 直接输出 n
          System.out.println(n);
        }
        else{
          int len=(arr[n-1]-arr[0])/d+1;
          System.out.println(len);

        }





       
        
    }
    public static int gcd(int a,int b){ //求出两个数的最大公约数的方法
      return b==0?a:gcd(b,a%b);
    }
}

 2. Sunday at the end of the century

end of the century week

Problem solving ideas 

Here I recommend that you use the api in java to solve the problem 

That is to use the calendar date class

Commonly used calendar fields

It should be noted that the MONTH field is counted from 0. Be sure to pay attention (doge)

So the value corresponding to MONTH here should be 11

In DAY_OF_WEEK, Sunday corresponds to 1, and week 2 corresponds to 2. Saturday corresponds to 7

The source code is attached: 

import java.util.Scanner;
import java.util.Calendar;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        Calendar calendar=Calendar.getInstance(); //赋值给calendar
        for(int year=1999;year<10000;year+=100){ //题目要求必须是xx99年 也就是每次加一百年
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,11);//MONTH字段是从0月开始计数的
        calendar.set(Calendar.DAY_OF_MONTH,31);
        if( calendar.get(Calendar.DAY_OF_WEEK)==1){ // 国外星期天对应的是1 星期一对应的是2 以此类推
          System.out.println(year);
            break;
        }


        }
        
    }
}

 The above two questions are the two group B questions prepared by Xiao Wang for the friends. I hope that the friends will be in the competition in less than a month.

Fight for the prize! Mainly for exercising

  


Guess you like

Origin blog.csdn.net/weixin_59796310/article/details/123441464