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 year1 month26 day Afternoon2: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 XOR(^) The ingenious use of budget: Get a sum of numbers0 Exclusive or self
            // A number is different from itself0 Principle, And the exclusive or operation can be exchanged , So the final result is the number of times
        }
        System.out.println(t);

    }


}

Method two

     utilizeASCII Code creates a subscript to256 Array, The initial values are all assigned as0, Then traverse the array Characters appear every time array value is added1 After traversal
Number organizationn The emergence ofn second, This method can calculate the number of characters

Or as follows


Each character corresponds to one anywayASCII code, Count the subscript of each character.
Need2 Special constants2 Medium condition: 1, This character doesn't appear( In the bid0) 2, This character appears more than once( In the bid-1)


 private static char firstAppearsOnlyonce(String str) {
            int[] hash = new int[256];                 
// Record the number of each character, Share256 individualASCII 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.
Input description:
Input data a string, Including letters, Figures, etc..

package example;
/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time:2018 year1 month26 day Afternoon2: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.
Input description:
Input data a string, Including letters, Figures, 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 elementvalue Set as1
                            }
                            else{
                                int val = map.get(c[i])+1;// If you already have child elements
Of this elementvalve+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 year1 month26 day Afternoon2: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 linesi=0; becausei=0 Only times,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. produceN Random number, And the number of digits is1,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 year1 month26 day Afternoon2: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 numbersN");
        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(" Single digit0 The total number of is:"+sum);
System.out.println(" Single digit1 The total number of is:"+sum1);
System.out.println(" Single digit2 The total number of is:"+sum2);
System.out.println(" Single digit3 The total number of is:"+sum3);
System.out.println(" Single digit4 The total number of is:"+sum4);
System.out.println(" Single digit5 The total number of is:"+sum5);
System.out.println(" Single digit6 The total number of is:"+sum6);
System.out.println(" Single digit7 The total number of is:"+sum7);
System.out.println(" Single digit8 The total number of is:"+sum8);
System.out.println(" Single digit9 The total number of is:"+sum9);

    }

}

Method two: Using arraysx[] 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 year1 month26 day Afternoon2: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 numbersN");
        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 as0
        }
            
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(" Single digit0 The total number of is:"+x[i]);
        }}
    }
}

5. Realize the function of sending red packets via wechat, Input amountm, Number of red packetsn, producen Random number, cutn The sum of the red packets ism

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. Realizationvip Login function Only daily3 Input error opportunities

package example2;

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time:2018 year1 month26 day Afternoon2: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);
        System.out.println(" Please enter your account number");
        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 + " Second chance");
                System.out.println(" Please re-enter your account");
                s = in.next();}
                if (m == 1) {
                    x = 1;
                }
            } else {
                break;
            }
        } while (m > 0);
        if (x == 1) {
            System.out.println(" Bye");
        } else {
            System.out.print(" Welcomevip");
        }

    }

}
7. seeks=a+aa+aaa+aaaa+aa...a Value, amonga It's a number. For example, input2 output2+22+222+2222+22222+222222=246912
 package example2;

/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time:2018 year1 month26 day Afternoon2: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 year1 month26 day Afternoon7: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 Correspondingvalve 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]
*@date Creation time:2018 year1 month27 day Afternoon7:00:43
*            Double chromosphere
         Dichroic demand: Choose six red balls, Choose a basketball player
             Red ball6 individual:1--33
             Blue ball1 individual:1--16            
        1. Build an array of red balls33 Element, Each element is1.2.3.4.5.....33                        
        2. Random sampling6 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[] luckRadball=new int[6];
        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, hereb[i] Default valuefalse
                luckRadball[j]=r.nextInt(33)+1;
                b[j]=true;// At this time, the random number appears in the array, holdb[i] Marked astrue
            }else {// In other cases, random numbers are generated uniformly, and they are not repeated
                do {
                    luckRadball[j]=r.nextInt(33)+1;
                } while (b[j]=true);
            }
        }
        for (int i = 0; i < luckRadball.length; i++) {
            result+=luckRadball[i]+" ";
        }
        System.out.println(" The result of this double color ball is:"+" Red ball: "+result+" Blue ball: "+luckBlueBall)
    }
}

9 seek100 reach1000 Number of daffodils within
/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time:2018 year1 month27 day Afternoon7: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 year1 month28 day Afternoon3: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 before1, Positive number not added
        }
        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 inputas1254dad14  output1254


/* Algorithmic thought: usemax Represents the maximum number length passed,count Represents a digital counter, Reset to when letter0
*end Represents the end of a number, Every time the number is met, Yesmax Judge, Whenmax Less thancount Time, To updatemax andend
*/


/*
*@author Wang Zhihua
*@E-mail: [email protected]
*@date Creation time:2018 year1 month28 day Afternoon12: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));
          }
}