Algorithmic Practice of Blue Bridge Cup Question Bank

Keywords: Java Programming

Special palindrome number

Problem description
123321 is a very special number. It reads from the left as it reads from the right.
Input a positive integer n, programming to find all such five-digit and six-digit decimal numbers, satisfying the sum of the digits equal to n.
Input format
Enter a line containing a positive integer n.
Output format
Output qualified integers in small to large order, each of which takes up one line.
sample input
52
sample output
899998
989989
998899
Data size and conventions
  1<=n<=54.

Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(bf.readLine());
		int a[]=new int[6];
		int b[]=new int[5];
		for(int i=1;i<=9;i++){
			for (int j = 0; j <=9; j++) {
				for (int k = 0;  k<=9; k++) {
				
					b[0]=i;b[1]=j;b[2]=k;
					b[3]=j;b[4]=i;
					if((b[0]+b[2]+b[1]+b[3]+b[4])==n)
					{
						for(int y:b)
						{System.out.print(y);
						}
						System.out.println();
					}
					
					}
					
				}
			}
		for(int i=1;i<=9;i++){
			for (int j = 0; j <=9; j++) {
				for (int k = 0;  k<=9; k++) {
					a[0]=i;a[1]=j;a[2]=k;
					a[3]=k;a[4]=j;a[5]=i;
					
					if(2*(a[0]+a[2]+a[1])==n)
					{
						for(int h:a)
						{System.out.print(h);
						}
						System.out.println();
					
					}
				
				}
			}
	
		}
	}
}

01 string

Problem description

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

00000

00001

00010

00011

00100

Please output these 32 01 strings in order from small to large.

Input format
This question is not entered.
Output format
Output 32 lines, in order from small to large, each line has a length of 5.01 strings.
sample output
00000
00001
00010
00011
<The following parts are omitted>
public class Main {
	public static void main(String[] args) {
		int n=1;
		for(int i=0;i<2;i++)
		{
			
			for(int j=0;j<2;j++)
			{
				
				for(int k=0;k<2;k++)
				{
					
					for(int l=0;l<2;l++)
					{
						
						for(int m=0;m<2;m++)
						{	
							System.out.print(i+""+j+""+k+""+l+""+m);

							System.out.println();
						}
					}
				}
			}
		}
	}

}  
Judgment of leap year
Problem description

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

This year is a leap year when one of the following is satisfied:

1. Years are multiples of 4, not 100.

2. 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 a given year is a leap year, then output yes, otherwise output no.

Explanation: When the test assigns you to output a string as a result (such as yes or no for this question), you need to strictly follow the case given in the test. Writing the wrong case will not score.

sample input
2013
sample output
no
sample input
2016
sample output
yes
Data Size and Agreement
1990 <= y <= 2050.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
		int i=Integer.parseInt(strin.readLine());
		if((i%4==0&&i%100!=0)||(i%400==0))
			System.out.println("yes");
		else System.out.println("no");
	}

}
Factorial computation
Problem description
Input a positive integer n and output the value of n!
Among them, n!=1*2*3* ____________ *n.
Algorithm description
n! It may be very large, but the integer range that the computer can represent is limited, so it needs to use the method of high precision calculation. An array A is used to represent a large integer a, A[0] is used to represent the bits of a, A[1] is used to represent the ten bits of a, and so on.
Multiply a by an integer k to multiply every element of array A by K. Please pay attention to the corresponding carry.
First set a to 1, then multiply 2, multiply 3, and when multiplied by n, we get the value of n!
Input format
The input contains a positive integer n, n<=1000.
Output format
Output the exact value of n!
sample input
10
sample output
3628800
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		int n=Integer.parseInt(bf.readLine());
		int a[] =new int[5000];
		int c=0;
		int s=0;
		a[0]=1;
		for(int i=2;i<=n;i++)
		{
		for(int j=0;j<5000;j++)
		{	
			s=a[j]*i+c;
			a[j]=s%10;	
			c=s/10;	
			
			
		}
		}
		int i;
		for(i=4999;a[i]==0;i--);
		for(;i>=0;i--)
		System.out.print(a[i]);
	}
}

High Precision Addition
Problem description
Input two integers a and b and output the sum of the two integers. Neither a nor b is more than 100.
Algorithm description
Because both a and b are large, they cannot be stored directly using standard data types in the language. For this problem, arrays are generally used.
Define an array A, A[0] to store the bits of a, A[1] to store the ten bits of a, and so on. An array B can also be used to store B.
When calculating C = a + b, we first add A[0] and B[0], if carry occurs, carry (that is, the decimal number of sum) is stored in r, and sum number is stored in C[0], that is, C[0] is equal to (A[0] + B[0]% 10. Then calculate the sum of A[1] and B[1], and then add up the value r of the lower carry, that is, C[1] should be the sum of A[1], B[1] and r. If carry occurs, the new carry can still be stored in r, and the sum in C[1]. By analogy, all bits of C can be found.
Finally, C can be output.
Input format
The input consists of two lines, the first is a non-negative integer a and the second is a non-negative integer b. Neither integer is more than 100 digits, and neither integer has the highest digit of 0.
Output format
Output a line representing the value of a + b.
sample input
20100122201001221234567890
2010012220100122
sample output
20100122203011233454668012
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
	BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
	int A[]=new int[101];
	int B[]=new int[101];
	int C[]=new int[101];
	String n1=bf.readLine();
	String n2=bf.readLine();
	for(int i=0;i<n1.length();i++)
		A[n1.length()-i-1]=Integer.parseInt(n1.substring(i,i+1));
	for(int i=0;i<n2.length();i++)
		B[n2.length()-i-1]=Integer.parseInt(n2.substring(i,i+1));
	int m=(n1.length()>n2.length()? n1.length():n2.length());
	int r1=0,r2;
	for(int i=0;i<=m;i++)
	{
		r2=(A[i]+B[i]+r1);
		C[i]=(r2)%10;
		
		r1=(A[i]+B[i]+r1)/10;
		
	}
		int j;
		for(j=m;C[j]==0;j--);
		for(int i=j;i>=0;i--)
		System.out.print(C[i]);

}
}


Posted by shab620 on Thu, 21 Mar 2019 21:03:52 -0700