Blue Bridge Cup · basic practice (Java)

catalogue

1. Sequence sorting

2. Hexadecimal to octal number

3. Hexadecimal to decimal

4. Decimal to hexadecimal

5. Number of special palindromes

6. Palindromes

7. Special numbers

8. Yang Hui triangle

10. Sequence characteristics

11. Letter graphics

12, 01 string

13. Leap year judgment

14. Fibonacci sequence

15. Area of circle

16. Sequence summation

1. Sequence sorting

Given a sequence of length N, the sequence is arranged from small to large. 1<=n<=200
Input format

The first line is an integer n.
The second row contains n integers, which are the numbers to be sorted, and the absolute value of each integer is less than 10000.

Output format

Output a row, and output the sorted sequence from small to large.

import java.util.*;
public class Main {
public static void main(String[] args) {
	Scanner in= new Scanner(System.in);
	int n=in.nextInt();
	int []a=new int[n];
	for(int i=0;i<n;i++) {
		a[i]=in.nextInt();
	}
	Arrays.sort(a);
	for(int i =0;i<n;i++) {
		System.out.printf("%d ",a[i]);
	}
}

}

2. Hexadecimal to octal number

Given n hexadecimal positive integers, output their corresponding octal numbers.

Input format
The first line of input is a positive integer n (1 < = n < = 10).
In the next n lines, a string composed of 0 ~ 9 and uppercase letters A~F in each line represents the hexadecimal positive integer to be converted, and the length of each hexadecimal number shall not exceed 100000.

Output format
Output n lines, and input the corresponding octal positive integer for each line.

[note]
The entered hexadecimal number will not have a leading 0, such as 012A.
The output octal number cannot have a leading 0.

import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
	Scanner in =new Scanner(System.in);
	int n=in.nextInt();
	String []a = new String[n];
	for(int i=0;i<n;i++){
		a[i]=in.next();
		}
	for(int i=0;i<n;i++){
	System.out.println(new BigInteger(a[i],16).toString(8));
	}
	}
}

3. Hexadecimal to decimal

Input a positive hexadecimal digit string of no more than 8 digits from the keyboard, convert it into a positive decimal number and output it.
Note: 10 ~ 15 in hexadecimal numbers are represented by uppercase English letters A, B, C, D, E and F respectively.

import java.util.*;
public class Main {
public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String a=sc.next();
		System.out.print(new BigInteger(a,16).toString(10));
	}
}

   

4. Decimal to hexadecimal

Hexadecimal number is an integer representation often used in programming. It has 16 symbols of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, B, C, D, e and F, representing 0 to 15 decimal numbers respectively. The hexadecimal counting method is full 16 into 1, so the decimal number 16 is 10 in hexadecimal, while the decimal number 17 is 11 in hexadecimal, and so on. The decimal number 30 is 1E in hexadecimal. Give a nonnegative integer and express it in hexadecimal form.

Input format

The input contains a nonnegative integer a representing the number to convert. 0<=a<=2147483647

Output format

Outputs the hexadecimal representation of this integer

import java.util.*;
public class Main {
public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String a = sc.next();
		System.out.print(new BigInteger(a,10).toString(16).toUpperCase());
	}

}
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		System.out.print(Integer.toHexString(a).toUpperCase());
	}

}

5. Number of special palindromes

123321 is a very special number. It reads from the left and from the right.
Enter a positive integer n, and program to find all such five and six digit decimal numbers, so that the sum of each number is equal to n.

Input format

Enter a line containing a positive integer n.

Output format

Output qualified integers from small to large, and each integer occupies one line.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
	  Scanner sc = new Scanner(System.in);
	  int n=sc.nextInt();
	  for(int i = 10000;i<=99999;i++) {
		  int a = i/10000%10;
		  int b = i/1000%10;
		  int c = i/100%10;
		  int d = i/10%10;
		  int e = i%10;
		  if(a==e&&b==d&&a+b+c+d+e==n) {
			  System.out.print(i+"\n");
		  }
	  }
	  for(int i = 100000;i<=999999;i++) {
		  int f = i/100000%10;
		  int a = i/10000%10;
		  int b = i/1000%10;
		  int c = i/100%10;
		  int d = i/10%10;
		  int e = i%10;
		  if(f==e&&a==d&&b==c&&a+b+c+d+e+f==n) {
			  System.out.print(i+"\n");
		  }
	  }
	}
}

 

6. Palindromes

1221 is a very special number. It reads from the left and from the right. It is programmed to find all such four digit decimal numbers.

Output format

Output four decimal numbers that meet the conditions in the order from small to large.

public class Main {

	public static void main(String[] args) {
		int i;
		for(i=0;i<10000;i++) {
			if(i>1000) {
			int a = i/1000%10;
			int b = i/100%10;
			int c = i/10%10;
			int d = i%10;
			if(a==d&&b==c) {
				System.out.print(i+"\n");
			}
			}
		}
	}
}

7. Special numbers

Problem description

153 is a very special number, which is equal to the cubic sum of each number, that is, 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3. Programming to find all three decimal numbers that meet this condition.

Output format

Output three decimal numbers that meet the conditions from small to large, and each number occupies one line.

public class Main {

	public static void main(String[] args) {
		int i;
		for(i=0;i<1000;i++) {
			if(i>99) {
				int a = i/100%10;
				int b = i/10%10;
				int c = i%10;
				if(a*a*a+b*b*b+c*c*c==i) {
					System.out.print(i+"\n");
				}
			}	
		}
	}
}

8. Yang Hui triangle

Problem description

Yang Hui triangle is also called Pascal triangle. Its i+1 line is the coefficient of the expansion of (a+b)i.   

One of its important properties is that each number in a triangle is equal to the sum of the numbers on its two shoulders

The first four rows of Yang Hui triangle are given below:

   1 

  1 1 

 1 2 1 

1 3 3 1 

Give n and output its first n lines.

Input format

The input contains a number n.

Output format

Output the first n rows of Yang Hui triangle. Each line is output from the first number of this line, separated by a space. Please do not output extra spaces in front.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a[][] = new int [n][n];
		for(int i=0;i<n;i++) {
			a[i][0]=1;//Each line begins with a 1
			a[i][i]=1;//The value in row i and column i is 1
		}
		for(int i=2;i<n;i++) {
			for(int j=1;j<=i-1;j++) {
				a[i][j]=a[i-1][j-1]+a[i-1][j];
			}
		}
       for(int i=0;i<n;i++) {
    	   for(int j=0;j<=i;j++) {
    	   System.out.print(a[i][j]+" ");
    	   }
    	   System.out.println();
       }    	   
	}
}

9. Find integer

Problem description

Given a sequence containing n integers, ask the first occurrence of integer a in the sequence.

Input format

The first line contains an integer n.

The second row contains n non negative integers, which is a given sequence, and each number in the sequence is not greater than 10000.

The third line contains an integer a, which is the number to be found.

Output format

If a appears in the sequence, output the position where it first appears (the position is numbered from 1), otherwise output - 1.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int []a=new int[n];
		for(int i=0;i<n;i++) {
			a[i]=sc.nextInt();
		}
		int b = sc.nextInt();
		Boolean find = false;
		for(int i=0;i<n;i++) {
			if(b==a[i]) {
				find=true;
				System.out.print(i+1);
				break;
			}					
		}
		if(!find) {
			System.out.println(-1);
		}
	}
}

10. Sequence characteristics

Problem description

Give n numbers and find the maximum, minimum, and.

Input format

The first line is an integer n, which represents the number of numbers.

The second line has n numbers, which are given n numbers, and the absolute value of each number is less than 10000.

Output format

Output three lines, one integer per line. The first row represents the maximum of these numbers, the second row represents the minimum of these numbers, and the third row represents the sum of these numbers.

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++) {
			a[i]=sc.nextInt(); 
		}
		int max=0;
		int min=0;
		int sum=0;
		Arrays.sort(a);
		max=a[n-1];
		min=a[0];
		for(int i=0;i<n;i++) {
			sum=sum+a[i];
		}
		System.out.print(max+"\n");
		System.out.print(min+"\n");
		System.out.print(sum+"\n");
	}
}

11. Letter graphics

Problem description

Some beautiful figures can be formed by using letters. An example is given below:

ABCDEFG

BABCDEF

CBABCDE

DCBABCD

EDCBABC

This is a graph with 5 rows and 7 columns. Please find out the law of this graph and output a graph with n rows and m columns.

Input format

Enter a row containing two integers n and m, which respectively represent the number of rows and columns of the graph you want to output.

Output format

Output n lines, each m characters, for your graphics.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();		
				for(int i=0;i<n;i++){					
					for(int j=i;j>=0 && i-j<m;j--){						
						System.out.print((char)(65+j));	
					}
					for(int k=1;k<m-i;k++){						
						System.out.print((char)(65+k));
					}
					System.out.println();
				}
			}		
	}

12, 01 string

Problem description

For a 01 string with a length of 5 bits, each bit may be 0 or 1. There are 32 possibilities in total. The first few of them are:

00000

00001

00010

00011

00100

Please output these 32 01 strings in descending order.

Output format

Output 32 lines, in order from small to large, each line has a 01 string with a length of 5.

public class Main {

	public static void main(String[] args) {	
	   for(int i=0;i<32;i++)
	   {
		   int j=i+256;  
		  String  a = Integer.toBinaryString(j);		   
		   String c=a.substring(a.length()-5);		   
		  System.out.print(c+"\n");
	   }
	    }
	 }

13. Leap year judgment

Problem description

Given a year, judge whether it is a leap year.

This year is a leap year when one of the following conditions is met:

1. The year is a multiple of 4 rather than a multiple of 100;

2. The year is a multiple of 400.

Other years are not leap years.

Input format

The input contains an integer y representing the current year.

Output format

Output a line. If the given year is a leap year, output yes, otherwise output no.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();
		if(year%4==0 && year%100!=0 || year%400==0) {
			System.out.print("yes");
		}else {
			System.out.print("no");
		}
	}
}

14. Fibonacci sequence

Problem description

The recurrence formula of Fibonacci sequence is: Fn=Fn-1+Fn-2, where F1=F2=1.

When n is large, Fn is also very large. Now we want to know what is the remainder of Fn divided by 10007.

Input format

The input contains an integer n.

Output format

Output a line containing an integer representing the remainder of Fn divided by 10007.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {	
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();		
		int[] f = new int[n+2];
		f[1]=1;f[2]=1;
		if(n>2) {
			for(int i = 3;i<=n;i++) {
				f[i]=(f[i-1]+f[i-2])%10007;
			}
		}
		System.out.print(f[n]);
	}
}

15. Area of circle

Problem description

Given the radius r of a circle, find the area of the circle.

Input format

The input contains an integer r representing the radius of the circle.

Output format

Output a line containing a real number, rounded to 7 digits after the decimal point, indicating the area of the circle.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		//PI=3.14159265358979323
		final double PI=3.14159265358979323;
		Scanner sc = new Scanner(System.in);
		int r = sc.nextInt();		
		double s = PI*r*r;		
		System.out.printf("%.7f",s);
	}
}

16. Sequence summation

Problem description

Find 1 + 2 + 3 ++ The value of n.

Input format

The input includes an integer n.

Output format

Output a line, including an integer, representing 1 + 2 + 3 ++ The value of n.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		int n = sc.nextInt(); 	
		long sum = 0;	
		for(int i=1;i<=n;i++) {
			sum = sum+i;		
		}
		System.out.println(sum);
	}
}

 

Keywords: Java Algorithm

Added by fsumba on Sat, 08 Jan 2022 10:47:53 +0200