1. Find the number that only appears once in the array , Other elements appear twice .

Method 1

package example;
/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 2:42:37
**/
public class FindOne {
public static void main(String[] args) {
int[] a={2,2,4,5,9,9,4};
int t=0;
for (int i = 0; i < a.length; i++) {
t^=a[i];// Use exclusive or (^) The ingenious use of budget ： Get a sum of numbers 0 Exclusive or self
// A number is different from itself 0 Principle of , And the exclusive or operation can be exchanged , So the final result is the number of times
}
System.out.println(t);

}

}

Method 2

utilize ASCII Code creates a subscript to 256 Array of , The initial values are all assigned as 0, Then traverse the array Characters appear every time array value is added 1 After traversal
Number organization is n That's what happened n second , This method can calculate the number of characters

Or as follows

Each character corresponds to one anyway ASCII code , Count the subscript of each character .
need 2 Special constants 2 Middle case ： 1, This character doesn't appear （ In the standard process 0） 2, This character appears more than once （ In the standard process -1）

private static char firstAppearsOnlyonce(String str) {
int[] hash = new int[256];
// Record the number of each character , share 256 individual ASCII code
for(int i=0; i<256; i++){
hash[i] = 0;
}
for(int i=0; i<str.length(); i++){
hash[str.charAt(i)] ++;                 // Establish a reflection relation between a character and its number !
}
for(int i=0; i<str.length(); i++){           // Traverse the string again , Find the first occurrence of a character
if(hash[str.charAt(i)] == 1){
return str.charAt(i);
}
}

2 Given an English string , Please write a piece of code to find the English character that appears three times in this string .
Enter a description :
Input data a string , Include letters , Number, etc .

package example;
/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 2:42:37
**/
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
* Given an English string , Please write a piece of code to find the English character that appears three times in this string .
Enter a description :
Input data a string , Include letters , Number, etc .
*
*/

public class FindChar {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Map<Character,Integer> map = new HashMap<Character,Integer>();
System.out.println(" Please enter the string you want to test ");
String s=in.next();
char[] c=s.toCharArray();
for (int i = 0; i < c.length; i++) {
if(!(c[i]>='a'&&c[i]<='z') && !(c[i]>='A'&&c[i]<='z'))
continue;
if(!map.containsKey(c[i])){
map.put(c[i], 1);// If ,map Does not contain this element , For each element value Set to 1
}
else{
int val = map.get(c[i])+1;// If you already have child elements
Of this element valve+1
if(val == 3){
System.out.println(c[i]);
break;
}
map.put(c[i],val);

}
}
}
}

3, Print Yanghui triangle

package yanghui;

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 2:42:37
**/

import java.util.Scanner;
public class PrintYanghui {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
System.out.println(" Please input the number of lines you want to print Yanghui triangle ");
int num=in.nextInt();
int[][] a=new int[num][num];
for (int i = 0; i <a.length; i++) {
for (int j = 0; j <=i; j++) {
if(j==0||j==i){// Find rule assignment , Remember not to use the number of lines i=0; because i=0 Only once ,j=0 Multiple times can be obtained through internal circulation
a[i][j]=1;
}else{
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < 8-i; j++) {
System.out.print(" ");
}
for (int j = 0; j <=i; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println("\n");
}
}
}

4. produce N Random number , And the number of digits is 1,2,3,4,5,6,7,8,9 Number of
Method 1 ： Record the number of different digits with nine variables

package example1;
/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 2:42:37
**/
import java.util.Random;
import java.util.Scanner;

public class random {

public static void main(String[] args) {
Random r=new Random();
Scanner in=new Scanner(System.in);
System.out.println(" Please enter the number of random numbers N");
int N=in.nextInt();
int[] a=new int[N];
int sum=0;
int sum1=0;
int sum2=0;
int sum3=0;
int sum4=0;
int sum5=0;
int sum6=0;
int sum7=0;
int sum8=0;
int sum9=0;
int m=0;

for (int i = 0; i < a.length; i++) {
a[i]=r.nextInt(100);
}
System.out.println(" The resulting random number is ：");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
for (int i = 0; i < a.length; i++) {
m=a[i]-((a[i]/10)*10);
if (m==0) {
sum++;
}else if (m==1){
sum1++;
}
else if (m==2){
sum2++;
}
else if (m==3){
sum3++;
}
else if (m==4){
sum4++;

}else if (m==5){
sum5++;

}else if (m==6){
sum6++;

}else if (m==7){
sum7++;

}else if (m==8){
sum8++;
}
else if (m==9){
sum9++;
}
}
System.out.println(" The number of digits is 0 The total number of is ："+sum);
System.out.println(" The number of digits is 1 The total number of is ："+sum1);
System.out.println(" The number of digits is 2 The total number of is ："+sum2);
System.out.println(" The number of digits is 3 The total number of is ："+sum3);
System.out.println(" The number of digits is 4 The total number of is ："+sum4);
System.out.println(" The number of digits is 5 The total number of is ："+sum5);
System.out.println(" The number of digits is 6 The total number of is ："+sum6);
System.out.println(" The number of digits is 7 The total number of is ："+sum7);
System.out.println(" The number of digits is 8 The total number of is ："+sum8);
System.out.println(" The number of digits is 9 The total number of is ："+sum9);

}

}

Method 2 ： Using arrays x[] Record the number of different digits , Obviously, the amount of code is much smaller , Therefore, arrays are also marked with the same records as variables ;

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 2:42:37
**/

public class random {
public static void main(String[] args) {
Random r=new Random();
Scanner in=new Scanner(System.in);
System.out.println(" Please enter the number of random numbers N");
int N=in.nextInt();
int[] a=new int[N];
int[] x=new int[9];
int m=0;
for (int i = 0; i < x.length; i++) {
x[i]=0;// Initialize array as 0
}

for (int i = 0; i < a.length; i++) {
a[i]=r.nextInt(100);
}
System.out.println(" The resulting random number is ：");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
for (int i = 0; i < a.length; i++) {
m=a[i]-((a[i]/10)*10);
if (m==0) {
x[0]++;
}else if (m==1){
x[1]++;
}
else if (m==2){
x[2]++;
}
else if (m==3){
x[3]++;
}
else if (m==4){
x[4]++;

}else if (m==5){
x[5]++;

}else if (m==6){
x[6]++;

}else if (m==7){
x[7]++;

}else if (m==8){
x[8]++;
}
else if (m==9){
x[9]++;
}
}
for (int i = 0; i < x.length; i++) {
System.out.println(" The number of digits is 0 The total number of is ："+x[i]);
}}
}
}

5. Realize the function of sending red packets via wechat , Enter amount m, Number of red packets n, produce n Random number , cut n The sum of the red packets is m

package example3;
import java.math.BigDecimal;
import java.util.Random;
import java.util.Scanner;
public class RedPacket {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String choice;
do{
System.out.println(" Please input the number of red packets you want to send ");
int n=in.nextInt();
System.out.println(" Please enter the total amount of red packets you want to send ");
double sum=in.nextDouble();
double []  money=new double[n];
for (int i = 0; i < money.length-1; i++) {
money[i]=random(sum);
sum-=money[i];
}
money[money.length-1]=sum;
for (int i = 0; i < money.length; i++) {
double d=money[i];
BigDecimal mData = new BigDecimal(d).setScale(2,
BigDecimal.ROUND_HALF_UP);
// Accurate to Two decimal places
System.out.println(mData);
}
System.out.println(" Do you want to continue with the program ：Y: continue     N end ");
choice=in.next();
}while(choice.equals("Y"));
}
public static double random(double money){
double m;
Random r=new Random();
m= r.nextDouble();
if (m<=0) {
m= r.nextDouble();
}

return m*money;
}

}

6. realization vip Login function Every day 3 Input error opportunities

package example2;

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 2:42:37
**/

import java.util.Scanner;
public class isVip {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String s = in.next();
int a = 60;
int m = 3;
int x = 0;

do {
if (!s.equals("888888")) {
--m;
if(m>0){
System.out.println(" You still have " + m + " Opportunities ");
s = in.next();}
if (m == 1) {
x = 1;
}
} else {
break;
}
} while (m > 0);
if (x == 1) {
System.out.println(" bye ");
} else {
System.out.print(" Welcome vip");
}

}

}
7. seek s=a+aa+aaa+aaaa+aa...a Value of , among a It's a number . For example, input 2 output 2+22+222+2222+22222+222222=246912
package example2;

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 2:42:37
**/import java.util.Scanner
public class deno {
public static void main(String[] args) {
int sum=0;
Integer m=0;
String str1="";
Scanner in=new Scanner(System.in);
System.out.println(" Please enter an integer ");
int a=in.nextInt();
for (int i = 1; i <=a; i++) {
m+=Integer.valueOf(solution(i,a));
sum+=m;
}
for (int i = 1; i <=8; i++) {
str1+=solution(i,a)+"+";
}
System.out.println(str1+"="+m);

}

public static String  solution(int a,int b){
String str="";
for (int i = 0; i < a; i++) {
str+=b;// Evaluate or string with string and any type
}
return str;
}
}
8. Given an array , Find out the elements whose total number of the same elements is greater than half the size of the array , And output

package example4;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
package example4;
/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 26 day afternoon 7:25:26
* /
public class FindMax {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Map<Integer,Integer> map=new HashMap();
System.out.println(" Please enter the size of the array you want to test ");
int n=in.nextInt();
int[] a=new int[n];
int i;
System.out.println(" Please enter the elements of the array you want to test ");
for ( i = 0; i < a.length; i++) {
int m=in.nextInt();
a[i]=m;
map.put(a[i], 1);
boolean b=map.containsKey(a[i]);
if (b) {
int p=map.get(a[i])+1;// If the same element appears ,key Corresponding valve add one-tenth
map.put(a[i], p);
}
}
for (int j = 0; j < map.size(); j++) {
if (map.get(a[j])>=a.length/2) {
System.out.println(a[j]);
}else {
System.out.println(" non-existent ");
}
}
}
}
9. Write a two color ball prize system （ The classic application of tag array ）
/*
*@author Wang Zhihua
*@E-mail: [email protected]q.com
*@date Creation time ：2018 year 1 month 27 day afternoon 7:00:43
*            Dichroic sphere
Dichroic demand ： Choose six red balls , Choose a basketball player
Red ball 6 individual ：1--33
Blue ball 1 individual ：1--16
1. Build an array of red balls 33 Elements , Each element is 1.2.3.4.5.....33
2. Random sampling 6 Balls in an array
3. Blue ball 1 individual
**/
package array.douublecolorball;
import java.util.Random;

public class DouubleColorBall {
public static void main(String[] args) {
int[] reaball=new int[33];
String result="";
int luckBlueBall=0;
boolean[] b=new boolean[33];
Random r=new Random();
luckBlueBall=r.nextInt(16)+1;
for (int i = 0; i < reaball.length; i++) {
reaball[i]=i+1;
}
for (int j = 0; j < luckRadball.length; j++) {
if (!b[j]) {// If the random number is not in the array , here b[i] Is the default false
b[j]=true;// At this time, the random number appears in the array , hold b[i] Mark as true
}else {// In other cases, random numbers are generated uniformly, and they are not repeated
do {
} while (b[j]=true);
}
}
for (int i = 0; i < luckRadball.length; i++) {
}
System.out.println(" The result of this double color ball is ："+" Red ball : "+result+" Blue ball : "+luckBlueBall)
}
}

9 seek 100 reach 1000 Number of daffodils within
/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 27 day afternoon 7:50:27
**/

package array.flowerNumber;

public class FlowerNumber {

public static void main(String[] args) {
System.out.println("100-1000 The number of daffodils in ：");
for(int i=100;i<1000;i++){
int ge  = i%10;
int shi = i/10%10;
int bai = i/10/10%10;

// Judgment requirements for number of Narcissus
if(i == (ge*ge*ge+shi*shi*shi+bai*bai*bai)){
System.out.println(i);
}
}

}
}

10. Given an integer , Find its binary number , And input .

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 28 day afternoon 3:30:47
**/

package tentotwo;
import java.util.Scanner;
public class TenToTwo {

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println(" Please enter an integer ");
int num=in.nextInt();
int num1=Math.abs(num);// Use its absolute value first, both positive and negative
StringBuffer str1=new StringBuffer();
while(num1/2!=0||num1%2!=0){
str1.append(num1%2+"");
num1=num1/2;
}
if (num<0) {
str1.append(1+"");// If it's a negative number , Add a sign bit before 1, Plus or minus
}
StringBuffer str=str1.reverse();// Because it's from the back to the front , So we need to invert the string
System.out.println(str);
}
}

11. Enter a string , Find the number string with the longest consecutive number in the string , as input as1254dad14  output 1254

/* Algorithmic idea ： use max Represents the maximum number length passed ,count Represents a digital counter , Reset to when letter 0
*end Represents the end of a number , Every time the number is met , Yes max Make a judgment , When max Less than count Time , to update max and end
*/

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time ：2018 year 1 month 28 day afternoon 12:57:17
**/
package maxstring;
import java.util.Scanner;
public class MaxStr {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.nextLine();
int max = 0;
int count=0,end=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>='0' && str.charAt(i)<='9'){
count++;
if(max<count){
max= count;
end = i;
}
}else{
count = 0;
}
}
System.out.println(str.substring(end-max+1,end+1));
}
}