Special palindrome number
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
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.
Judgment of leap yearpublic 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(); } } } } } } }
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.
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.
Factorial computationimport 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"); } }
Among them, n!=1*2*3* ____________ *n.
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!
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
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.
2010012220100122
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]); } }