01 A+B question (simple)
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
Problem description
Input A, B, output A+B.
Input format
The first line of input consists of two integers separated by spaces, representing A and B respectively.
Output format
Output a line, including an integer, representing the value of A+B.
sample input
12 45
sample output
57
Data scale and agreement
-10000 <= A, B <= 10000.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); System.out.println(a+b); } }
02 sequence summation
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
Problem description
Find the value of 1 + 2 + 3 +... + n.
Input format
The input includes an integer n.
Output format
Output a line, including an integer, representing the value of 1 + 2 + 3 +... + n.
sample input
4
sample output
10
sample input
100
sample output
5050
Data scale and agreement
1 <= n <= 1,000,000,000.
int Max 2147483647
If int is used to store the cumulative result, the result will be wrong when n is large to a certain extent
import java.util.Scanner; public class Main { public static void main(String[] args) { long num=0,sum=0; Scanner a=new Scanner(System.in); num=a.nextLong(); if(num>=1 && num<=1000000000) { sum=num+num*(num-1)/2; System.out.println(sum); } } }
03 area of circle
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
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.
sample input
4
sample output
50.2654825
Data scale and agreement
1 <= r <= 10000.
When doing multiplication, the multiplication of int and double will result in partial result errors
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double r = sc.nextInt(); sc.close(); double PI=3.14159265358979323; System.out.println(String.format("%.7f", PI*r*r)); } }
04 Fibonacci sequence
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
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.
sample input
10
sample output
55
sample input
22
sample output
7704
Data scale and agreement
1 <= n <= 1,000,000.
At first, I wanted to use the function to call recursively, but the result timed out
static long fib(int i) { if(i<3) return 1; else return (fib(i-1)+fib(i-2))%10007; }
Later, I thought of storing the sequence in a one-dimensional array, but the storage length of int type is limited, and the Fibonacci number of 1000000 is too large, resulting in some calculation errors.
In fact, this problem does not require accurate values. As long as the remainder is output, you can directly take the remainder after obtaining the Fibonacci sequence each time, so that each number in the array can be controlled within 10007.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); //Don't write about initialization, n it will lead to cross-border int[] a=new int[n]; for(int i=0;i<n;i++) { if(i<2) { a[i]=1; } if(i>1) { a[i]=(a[i-1]+a[i-2])%10007; } } System.out.println(a[n-1]); } }
1 leap year judgment (simple)
Maybe the first question for every beginner of high-level language is to judge the leap year, so the question will not be posted.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int y = sc.nextInt(); if((y%4==0&&y%100!=0)||y%400==0) { System.out.println("yes"); } else System.out.println("no"); } }
2 01 string
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
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.
Input format
This question is not entered.
Output format
Output 32 lines, in order from small to large, each line has a 01 string with a length of 5.
sample output
00000
00001
00010
00011
< the following parts are omitted >
public class Main { public static void main(String[] args) { for(int i=0;i<32;i++) { System.out.println(i/16%2+""+i/8%2+""+i/4%2+""+i/2%2+""+i%2); } } }
3-letter graphics
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
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.
sample input
5 7
sample output
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
Data scale and agreement
1 <= n, m <= 26.
Just control the beginning and end of the cycle.
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(); String str= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int w=0; for(int i=0;i<n;i++) { if((i-m+1)>0) w=i-m;//Consider the negative case!!!! for(int j=i;j>w;j--) { System.out.print(str.charAt(j)); } for(int j=0;j<m-i;j++) { System.out.print(str.charAt(j)); } System.out.println(); } } }
4 sequence characteristics (simple)
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
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.
sample input
5
1 3 -2 4 5
sample output
5
-2
11
Data scale and agreement
1 <= n <= 10000.
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 min=a[0],max=a[0],sum=a[0]; for(int i=1;i<n;i++) { if(a[i]<min) { min=a[i]; } if(a[i]>max) { max=a[i]; } sum=sum+a[i]; } System.out.println(max); System.out.println(min); System.out.println(sum); } }
5 find integer
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
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.
sample input
6
1 9 4 8 3 9
9
sample output
2
Data scale and agreement
1 <= n <= 1000.
At first I was going to use arrays Binarysearch. As a result, several test cases kept making mistakes. Later, it was found that the dichotomy search must be an ordered array, that is, an array sort ed.
int index=Arrays.binarySearch(a, s); if(index<0) System.out.println(-1); else System.out.println(index+1);
Later, when I changed it, I fell a lot of holes. The subscript of this question starts from 1, and as long as it appears for the first time, it needs to break and prevent coverage in time
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] a=new String[n]; for(int i=0;i<n;i++) { a[i]=sc.next(); } String s=sc.next(); sc.close(); int re=-1; for(int i=0;i<n;i++) { if(s.equals(a[i])) { re=i+1; break; } } System.out.println(re); } }
6 Yanghui triangle
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
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.
sample input
4
sample output
1
1 1
1 2 1
1 3 3 1
Data scale and agreement
1 <= n <= 34.
I've been obsessed with writing strings for a long time. Later, I found that it's super easy to write with two-dimensional arrays...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); int[][] arr=new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<=i;j++) { if(j==0||j==i) { arr[i][j]=1; } else { arr[i][j]=arr[i-1][j-1]+arr[i-1][j]; } System.out.print(arr[i][j]+"\t"); } System.out.println(); } } }
7 special numbers
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
153 is a very special number, which is equal to the cubic sum of each number, that is, 153 = 111 + 555 + 333. 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.
As long as you know math The pow (a, b) function is simple, a^b
public class Main { public static void main(String[] args) { for(int i=100;i<1000;i++) { int a=i/100,b=i/10%10,c=i%10; int re=(int) (Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3)); if(i==re) { System.out.println(i); } } } }
8 palindromes
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
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.
As long as you can use system out. println(""+i+j+j+i); Just output the integer as a string. My basic skills are really nothing left.
public class Main { public static void main(String[] args) { for (int i = 1; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.println(""+i+j+j+i); } } } }
9 number of special palindromes
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
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.
sample input
52
sample output
899998
989989
998899
Data scale and agreement
1<=n<=54.
This question is to sum the fixed palindrome number, which is no different from question 9. I didn't pay attention to the difficulty. I still stayed in the string used in question 12. The code will be more than ten lines shorter with the output method of question 8.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); if(n>0&&n<55) { for (int i = 1; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (i * 2 + j * 2 + k == n) { StringBuffer re=new StringBuffer(""); re.append(i); re.append(j); re.append(k); re.append(j); re.append(i); System.out.println(re); } } } } for (int i = 1; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (i * 2 + j * 2 + k*2 == n) { StringBuffer re=new StringBuffer(""); re.append(i); re.append(j); re.append(k); re.append(k); re.append(j); re.append(i); System.out.println(re); } } } } } } }
10 decimal to hexadecimal
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
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
sample input
30
sample output
1E
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); StringBuffer re=new StringBuffer(""); String b16 = "0123456789ABCDEF"; if(n==0) { re.append("0"); } while (n > 0) { //%Remainder / divisor String tmp=Character.toString(b16.charAt(n%16)); re.insert(0, tmp); n=n/16; } System.out.println(re); } }
11 hex to decimal
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
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.
sample input
FFFF
sample output
65535
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str=sc.next(); String[] b16 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; long re=0;//int maximum 2147483647, not enough for 8-bit positive hexadecimal for(int i=0;i<str.length();i++) { String tmp=Character.toString(str.charAt(i)); int index=Arrays.binarySearch(b16, tmp); int cifang=str.length()-i-1; re=(long) (re+index*Math.pow(16,cifang)); } System.out.println(re); } }
12 decimal to octal
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
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, each line is a string composed of 09 and uppercase letter AF, indicating 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.
sample input
2
39
123ABC
sample output
71
4435274
At first, I wanted to use function conversion directly, but then I unexpectedly exceeded the limit...
for(int i=0;i<n;i++) { System.out.println(Integer.toOctalString(Integer.valueOf(a[i],16))); }
You still have to write it manually
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(); String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = sc.next(); } sc.close(); String[] b16 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" }; String[] b2 = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; String[] b8 = { "0", "1", "2", "3", "4", "5", "6", "7" }; for (int m = 0; m < n; m++) { String c16 = a[m]; StringBuffer c2 = new StringBuffer(); StringBuffer c8 = new StringBuffer(); // Traverse each of hexadecimal to find the corresponding four digit binary for (int j = 0; j < c16.length(); j++) { // Returns a hexadecimal character c String c = Character.toString(c16.charAt(j)); // Find the coordinates corresponding to character c by dichotomy int index = Arrays.binarySearch(b16, c); c2.append(b2[index]); } // Three binaries represent one octal. The beginning is not enough and needs to be supplemented while (c2.length() % 3 != 0) { c2.insert(0, "0"); } // Octal length int len8 = c2.length() / 3; // Take 3 bits of binary at a time and convert it to octal for (int g = 0; g < len8; g++) { String tmp = c2.substring(3 * g, 3 * (g + 1)); //Binary requires 4 bits, with 0 before it int index = Arrays.binarySearch(b2, "0" + tmp); c8.append(b8[index]); } //Convert to hexadecimal and remove the redundant 0 while(c8.charAt(0)=='0') { c8.delete(0, 1); } System.out.println(c8.toString()); } } }
13 sequence sorting
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
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.
sample input
5
8 3 6 4 9
sample output
3 4 6 8 9
Function really fragrant hhhhh
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int[] n=new int[a]; for(int i=0;i<a;i++) { n[i]=sc.nextInt(); } Arrays.sort(n); for(int i=0;i<a;i++) { System.out.print(n[i]+" "); } } }
30 factorial calculation
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
Enter a positive integer n and output n! Value of.
Where n= 123*…*n.
Algorithm description
n! It may be very large, and the range of integers that can be represented by the computer is limited, so it is necessary to use high-precision calculation methods. An array A is used to represent a large integer a, A[0] represents one bit of a, A[1] represents ten bits of a, and so on.
Multiply a by an integer k to multiply each element of array a by K. please pay attention to handling the corresponding carry.
First set a to 1, then multiply 2, multiply 3, when multiplied to N, you get n! Value of.
Input format
The input contains a positive integer n, n < = 1000.
Output format
Output n! The exact value of.
sample input
10
sample output
3628800
The factorial result is too large for a simple int
int w=1; for(int i=1;i<=n;i++) w=w*i; System.out.println(w);
Use an array a to represent a large integer a, A[0] represents one bit of a, and A[1] represents ten bits of a
Multiplying a by an integer k turns into multiplying each element of array a by K. there are two things that are difficult to control, one is carry, the other is the effective array length, which can be expressed by jin and index respectively.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); //initialization int a[] = new int[10000]; a[1]=1; int index=1,jin; for(int i=2;i<=n;i++) {//multiplier jin=0; for(int j=1;j<=index;j++) {//Number of arrays int tmp=a[j]*i+jin; a[j]=tmp%10; jin=tmp/10; } while(jin>0) { a[++index]=jin%10; jin=jin/10; } } for(int m=index;m>0;m--) { System.out.print(a[m]); } } }