1.找出数组中只出现一次的数,其他元素出现两次。

方法一


package example;
/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午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];//利用异或(^)预算的巧用:得出一个数和0异或还是自己
            //一个数和自己异或是0的原理,且异或运算可交换 ,故最终的结果就是出现一次的那个数
        }
        System.out.println(t);

    }


}

方法二

     利用ASCII码创建一个下标到256的数组,初始值都赋值为0,然后遍历数组 出现的字符每次数组值加1遍历结束后
数组织为n的就出现n次,这种方法可以计算字符出现的个数

或者如下


每个字符反正对应一个ASCII码,计每个字符出现的下标即可。
需要2个特别的常量来表示2中情况: 1、这个字符没出现过(标程里为0) 2、这个字符多次出现(标程里为-1)


 private static char firstAppearsOnlyonce(String str) {
            int[] hash = new int[256];                 
//记录每个字符对应的个数,共有256个ASCII码
            for(int i=0; i<256; i++){
                hash[i] = 0;
            }
            for(int i=0; i<str.length(); i++){
                hash[str.charAt(i)] ++;                 //建立一个字符与个数反映射关系!
            }
            for(int i=0; i<str.length(); i++){           //再遍历一遍字符串,找第一个出现一次字符
                if(hash[str.charAt(i)] == 1){
                    return str.charAt(i);
                }
            }


2 给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。
输入描述:
输入数据一个字符串,包括字母,数字等。

package example;
/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午2:42:37
**/
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
 * 给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符。
输入描述:
输入数据一个字符串,包括字母,数字等。
 *
 */

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("请输入你要测试的字符串");
        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);//如果,map不包含此元素,将每个元素的value设为1
                            }
                            else{
                                int val = map.get(c[i])+1;//如果已经包含子元素
则此元素的valve+1
                                if(val == 3){
                                    System.out.println(c[i]);
                                    break;
                                }
                                map.put(c[i],val);                         
         
        }        
        }
    }
    }


3,打印杨辉三角

package yanghui;

/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午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("请输入你要打印杨辉三角的行数");
        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){//找到规律赋值,切记不能用行数i=0;因为i=0只有次,j=0可以通过内循环获得多次
                     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.产生N个随机数,并且计算个位数分别为1,2,3,4,5,6,7,8,9的数个数
方法一:用九个变量记录不同个位数数的个数

package example1;
/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午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("请输入随机数的个数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("产生的随机数为:");
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("个位数为0的数的总数为:"+sum);
System.out.println("个位数为1的数的总数为:"+sum1);
System.out.println("个位数为2的数的总数为:"+sum2);
System.out.println("个位数为3的数的总数为:"+sum3);
System.out.println("个位数为4的数的总数为:"+sum4);
System.out.println("个位数为5的数的总数为:"+sum5);
System.out.println("个位数为6的数的总数为:"+sum6);
System.out.println("个位数为7的数的总数为:"+sum7);
System.out.println("个位数为8的数的总数为:"+sum8);
System.out.println("个位数为9的数的总数为:"+sum9);

    }

}

方法二:用数组x[]记录不同个位数的数的个数,显然代码量要小的多,故数组还以当变量一样的记录或标记;

/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午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("请输入随机数的个数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;//初始化数组为0
        }
            
for (int i = 0; i < a.length; i++) {
    a[i]=r.nextInt(100);
}
System.out.println("产生的随机数为:");
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("个位数为0的数的总数为:"+x[i]);
        }}
    }
}

5.实现微信发红包功能,输入金额m,和红包个数n,产生n个随机数,切n个红包的金额和为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("请输入你要发红包的个数");
    int n=in.nextInt();
    System.out.println("请输入你要发红包的总额");
    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);
            //精确到 小数点后两位
            System.out.println(mData);           
            }
         System.out.println("是否要继续执行程序:Y:继续    N结束");
         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.实现vip登录功能 每天只有3次输入错误机会

package example2;

/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午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);
        System.out.println("请输入您的账号");
        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("你今天还有" + m + "次机会");
                System.out.println("请重新输入您的账号");
                s = in.next();}
                if (m == 1) {
                    x = 1;
                }
            } else {
                break;
            }
        } while (m > 0);
        if (x == 1) {
            System.out.println("再见");
        } else {
            System.out.print("欢迎你vip");
        }

    }

}
7.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如输入2输出2+22+222+2222+22222+222222=246912
 package example2;

/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午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("请输入一个整数");
        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;//利用字符串和任何类型计算还是字符串
    }
    return str;
  }
}
8.给定一个数组,找出相同元素总数大于数组大小一半的元素,并输出

package example4;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
package example4;
/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月26日 下午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("请输入你要测试的数组的大小");
        int n=in.nextInt();
        int[] a=new int[n];
        int i;
        System.out.println("请输入你要测试的数组的元素");
        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;//如果出现相同的元素,key对应的valve加一
                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("不存在");
                }
            }
    }
}
9.写出双色球开奖系统(标记数组的经典运用)
/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月27日 下午7:00:43
*            双色球
        双色球需求:红球选六个,篮球选一个
             红球6个:1--33
             蓝色球1个:1--16            
        1.构建一个红球的数组33个元素,每个元素是1.2.3.4.5.....33                        
        2.随机抽取6个球存到一个数组中
        3.蓝色球 1个
**/
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]) {//如果随机数还没有出现在在数组里面,此时b[i]为默认值false
                luckRadball[j]=r.nextInt(33)+1;
                b[j]=true;//此时随机数出现在在数组里面,把b[i]标记为true
            }else {//其他情况一致产生随机数知道不重复
                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("本次双色球开奖结果为:"+"红色球: "+result+"蓝色球: "+luckBlueBall)
    }
}

9求100到1000以内的水仙花数
/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月27日 下午7:50:27
**/

package array.flowerNumber;

public class FlowerNumber {

    public static void main(String[] args) {  
        System.out.println("100-1000中的水仙花数有:");  
        for(int i=100;i<1000;i++){  
            int ge  = i%10;  
            int shi = i/10%10;  
            int bai = i/10/10%10;  
              
            //水仙花数判断要求  
            if(i == (ge*ge*ge+shi*shi*shi+bai*bai*bai)){  
                System.out.println(i);  
            }  
        }
 
    }
}

10.给定一个整数,求它的二进制数,并输入。

/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月28日 下午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("请输入一个整数");
        int num=in.nextInt();
        int num1=Math.abs(num);//无论正负都先用它的绝对值
        StringBuffer str1=new StringBuffer();    
        while(num1/2!=0||num1%2!=0){        
            str1.append(num1%2+"");
            num1=num1/2;        
        }
        if (num<0) {
            str1.append(1+"");//如果是负数,在前面加一个符号位1,正数不加
        }
        StringBuffer str=str1.reverse();//因为是从后往前加的,故要把字符串倒置过来
        System.out.println(str);
    }
}

11.输入一段字符串,找出字符串里面连续数字最长的数字串,如 输入as1254dad14  输出1254


/*算法思想:用max表示经过的数字长度最大值,count表示数字计数器,当为字母时重置为0
*end表示数字尾部,每次满足数字时,对max进行判断,当max小于于count时,更新max和end
*/


/*
*@author 王志华
*@E-mail: 2602565232@qq.com
*@date 创建时间:2018年1月28日 下午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));
          }
}


友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信