练习题

28,对10位整数进行排序

 1 /**
 2  * 对10个数进行排序
 3  * 从小到大
 4  */
 5 @Test
 6 public void test28(){
 7     
 8     System.out.println("请输入十位整数:");
 9     
10     Scanner input = new Scanner(System.in);
11     int[] array = new int[10];
12     String str = "";
13     
14     for(int i=0;i<10;i++){
15         array[i] = input.nextInt();
16         if(i==9){
17             System.out.println("十位整数录入完毕");
18         }
19     }
20     
21     for(int j=0;j<9;j++){
22         for(int k=0;k<9-j;k++){
23             if(array[k] > array[k+1]){
24                 int temp = array[k+1];
25                 array[k+1] = array[k];
26                 array[k] = temp;
27             }
28         }
29     }
30     
31     for(int a:array){
32         str+=a+" ";
33     }
34     System.out.println("排序后的数组:"+str);
35 }
10位整数排序

 以上用的是冒泡排序算法

,最常用的排序算法有:冒泡排序,选择排序,插入排序,以及java自带的Arrays.sort();

需要注意的是Arrays.sort不返回任何东西。它直接修改arr,而不是把修改后的东西返回。
Arrays.sort(arr);

//本题考察栈中的数据不能通过方法的形式修改,基本数据类型都是存储在栈中的,
    //String是复合类型,存储在字符串常量池中,所以也不可以通过方法的方式修改本身.
    public void TestChange(){
        String name="yin";
        int age=20;
        changeDate(name,age);
        System.out.println("查看name值是否改变:"+name+"*"+age);//输出name为:yin*20
    }

    public void changeDate(String name,int age){
    
        name = "yinyanling";
        age = 26;

    }
        //本题考查switch和break的用法,switch语句就是一个多条件选择执行语句,简称开关语句,类似于if.. else if.. else 语句;
    //使用switch语句有两点必须注意: 
    //一.在每一个分支里面都必须写break,此语句表示退出整个switch()循环; 
    //如果不使用break语句则所有的操作将在第一个满足条件之后的语句全部输出,直到遇到break语句为止; 下面例子中将会从条件满足5的地方往后全部执行,直到遇到break为止
    //二.switch选择条件只能是数字或者字符或者枚举类型.
    
    public void testSwitch(){
        int num1 = 5;
        int result = 2;
        switch(num1){
            case 0:
                result = num1*result;
            case 3:
                result = num1*result;
            case 5:
                result = num1*result;//10
            case 7:
                result = num1*result;//50
            case 10:
                result = num1*result;//250
        }
        System.out.println(result);//输出结果250
        
    }
//本题考查ArrayList的存储结构为数组类型 如果在遍历的过程中删除某个元素将会报异常(java.util.ConcurrentModificationException)
    
    public void TestList(){
        
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<5;i++){
            list.add(i);
        }
        
        for(int a:list){
            if(a == 1){
                list.remove(a);
            }
        }
        
        System.out.println("集合长度:"+list.size());
    }
    
    public void testList3(){
        
        List<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<5;i++){
            list.add(i);
        }
        
        for(int j=0;j<list.size();j++){
            if(list.get(j)==1){
                list.remove(j);
                j--;
            }
        }
        System.out.println("集合长度:"+list.size());
    }
    
    public void testList4(){
        
        List<String> list=new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        
        System.out.println();
        
    }
    //@Test
    public void testList2(){
        
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i=0;i<5;i++){
            list.add(i);
        }
        
        Iterator<Integer> iterator = list.iterator();
        while(iterator.hasNext()){
            Integer integer = iterator.next();
            if(integer==2)
                list.remove(integer);//报异常
        }
    }

/*    public static void main(String[] args)  {
        
        List<Integer> list=new ArrayList<Integer>();
        for(int i=0;i<5;i++){
            list.add(i);
        }
        
        Iterator<Integer> iterator=list.iterator();
        while(iterator.hasNext()){
            Integer integer=iterator.next();
            if(integer==2){
                iterator.remove();//能正常刪除相应数据
            }
        }
        System.out.println(list.size());//输出4
    }*/
    
    /**
     * 找出字符串中连续数字最长的串
     */
    public void test3(){
        
        Scanner input=new Scanner(System.in);
        System.out.println("请输入目标字符串:");
        String targetStr=input.nextLine();
        
        String numString="";
        for(char c:targetStr.toCharArray()){
            if(c>'0' && c<'9'){
                numString+=c;
            }else{
                numString+="a";
            }
        }
        System.out.println(numString);
        
        String[] numArray=numString.split("a");
        
        int max=0;
        int index=0;
        
        for(int i=0;i<numArray.length;i++){
            max=numArray[i].length()>max?numArray[i].length():max;
            index=i;
        }
        System.out.println("最长数字串为:"+numArray[index]+" 长度为:"+max);
    }
    
    /** 
     * 约瑟夫环是一个数学的应用问题: 
     * 已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列; 
     * 他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。 
     */
    //@Test
    public void test(){
        putDate(1000,3);
    }
    public void putDate(int n,int m){
        
        LinkedList<Integer> list=new LinkedList<Integer>();
        for(int i=0;i<n;i++){
            list.add(i);
        }
        
        int count=1;
        loopData(list,m,count);
    }
    StringBuffer sb = new StringBuffer();
    public void loopData(LinkedList<Integer> list,int m,int count){
        
        int size=list.size();
        if(size>1){
            
            for(int i=0;i<list.size();i++){
                if(count==m){
                    sb.append(list.get(i)).append(",");
                    list.remove(i);
                    count=0;
                    i--;
                }
                count++;
            }            
            loopData(list,m,count);
            
        }else{
            sb.append(list.get(0));
            System.out.println("按顺序出来的字符串为:"+sb.toString()+"\n最后一个出局的数字是:"+list.get(0));
        }
        
    }
    public void listToArray1(){
        
        List list=new ArrayList();
        list.add("aa");
        list.add("bb");
        //方法一
        Object[] array=list.toArray();
        //方法二 也可给数组指定大小
        String[] array1=(String[]) list.toArray(new String[list.size()]);
        //方法三 通过笨方法循环遍历将集合转换成数组
        
    }
    public void arrayToList(){
        String[] array={"aa","bb"};
        List list=new ArrayList();
        //方法一
        Collections.addAll(list, array);
        //方法二
        List<String> List1 = Arrays.asList(array);
        //另:Arrays.asList()返回一个受指定数组支持的固定大小的列表。所以不能做Add、Remove等操作。
        List list2 = new ArrayList(Arrays.asList(array));//这样操作就可以了。
        //方法三  通过笨方法循环遍历将数组转集合
    }
    
    //斐波那契数列(黄金分割,有名“兔子数列")
    //@Test
    public void lianxi01(){
        System.out.println("第1个月兔子对数:1");
        System.out.println("第2个月兔子对数:1");
        int f1=1,f2=1,f,M=24;
        for(int i=3;i<M;i++){
            f=f2;
            f2=f1+f2;
            f1=f;
            System.out.println("第"+i+"个月兔子对数:"+f2);
        }
    }
    //输出101到200之间的所有素数,及素数的个数(只能被1或本身整除的数)
    public void lianxi02(){
        int count=0;
        for(int i=101;i<200;i+=2){
            boolean b = false;
            for(int j=2;j<Math.sqrt(i);j++){
                if(i%j==0){
                    b=false;
                    break;
                }else{
                    b=true;
                }
            }
            if(b){
                count++;
                System.out.println("素数:"+i);
            }
        }
        System.out.println("素数的个数为:"+count);
    }
    //水仙花数(指一个三位数,其各位数的三次方和等于该数本身)
    //@Test
    public void lianxi03(){

        int b1,b2,b3;
        for(int i=101;i<1000;i++){
            b1 = i%10; //取个位数
            b2 = i%100/10; //取十位数
            b3 = i/100; //取百位数
            if((b1*b1*b1)+(b2*b2*b2)+(b3*b3*b3)==i){
                System.out.println(i+" 是水仙花数");
            }
        }
    }
    //将一个整数分解质因数
    
    public void lianxi04(){
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("请输入整数:");
        
        int num = input.nextInt();
        
        int k=2;
        
        System.out.print(num+"=");
        
        while(k <= num){
            if(k==num){
                System.out.print(k);
                break;
            }else if(num%k==0){
                System.out.print(k+"*");
                num=num/k;
            }else{
                k++;
            }
        }
    }
    //输出学生成绩90以上得A,60-89得B,60以下得C
    
    public void lianxi05(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入份数:");
        int score = input.nextInt();
        char grade;
        grade = score>=90?'A':score>=60?'B':'C';
        
        System.out.println("学生成绩为:"+grade);
    }
    
    //求两个数的最大公约数,最小公倍数
    
    public void lianxi06(){
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("请输入一个整数:");
        int num1 = input.nextInt();
        
        System.out.println("请再输入一个整数:");
        int num2 = input.nextInt();
        
        int a=0;//最大公约数
        int b=0;//最小公倍数
        a=maxNum(num1,num2);
        b=num1*num2/a;  //两数的最小公倍数为: 两数相乘除以最大公约数
        System.out.println("最大公约数为:"+a);
        System.out.println("最小公倍数为:"+b);
        
    }
    public int maxNum(int m,int n){
        int k;
        if(m<n){
            k=m;
            m=n;
            n=k;
        }
        //此处的逻辑为数学中的 "辗转相除法"求两数的最大公约数
        while(n!=0){
            if(m==n){
                return m;
            }else{
                int x=m%n;
                m=n;
                n=x;
            }
        }
        
        return m;
    }
    
    /**
     * 统计字符串中字母、数字、空格、及其他字符的个数
     */
    public void test07(){
        
        int character = 0;//字母个数
        int digital = 0;//数字个数
        int blank = 0;//空格个数
        int other = 0;//其他字符个数
        
        Scanner input = new Scanner(System.in);
        System.out.println("请输入目标字符串:");
        String target = input.nextLine(); 
        char[] a = target.toCharArray();
        for(char b:a){
            if(b >= '0' && b <= '9'){
                digital++;
            }else if((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')){
                character++;
            }else if(b == ' '){
                blank++;
            }else{
                other++;
            }
        }
        
        System.out.println("字母个数为:"+character);
        System.out.println("数字个数为:"+digital);
        System.out.println("空格个数为:"+blank);
        System.out.println("其他字符个数为:"+other);
    }
    /**
     * 题目:s=a+aa+aaa+aa...a,其中a是一个数字,几个数相加由键盘控制
     */
    
    public void test08s(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入数字:");
        int num1 = input.nextInt();
        System.out.println("请输入个数:");
        int num2 = input.nextInt();
        
        int sum = 0;
        int c = 0;
        StringBuffer sb = new StringBuffer();
        sb.append("s=");
        for(int i=1;i<=num2;i++){
            c = c*10+num1; 
            sum = sum+c;
            if(i == num2){
                sb.append(c+"="+sum);
            }else{
                sb.append(c+"+");
            }
        }
        System.out.println(sb.toString());
    }
    
    public void test08(){
        
        Scanner input = new Scanner(System.in);
        System.out.println("请输入相加数字:");
        int a = input.nextInt();
        System.out.println("请输入相加项的个数:");
        int b = input.nextInt();
        
        int sum = 0;//相加之和
        int i = 1;//相加次数
        int c = 0;//加数
        
        StringBuffer sb = new StringBuffer();
        sb.append("s=");
        while(i <= b){
            
            c = c + a;
            sum = sum + c;
            a = a * 10;
            
            if(i == b){
                sb.append(c+"=");
                break;
            }else{
                sb.append(c+"+");    
            }
            
            i++;
        }
        System.out.println(sb.toString()+" "+sum);
    }
    /**
     * 输出1到1000之间的所有完数
     * eg: 6=1+2+3
     */
    
    public void test09s(){
        for(int i=1;i<=1000;i++){
            StringBuffer sbs = new StringBuffer();
            String ss="";
            int k=0;
            for(int j=1;j<=i/2;j++){
                if(i%j==0){
                    k+=j;
                    sbs.append(j+"+");
                }
                if(k==i){
                    ss = sbs.toString();
                    System.out.println("完数:"+i+"="+ss.substring(0,ss.length()-1));
                    break;
                }
            }
        }
    }
    
    public void test09(){
        System.out.println("输出1到1000之间所有的'完数'");
        for(int i =1 ;i <= 1000;i++){
            int k=0;
            for(int j = 1;j <= i/2;j++){
                if(i%j==0){
                    k = k + j;
                }
            }
            if(k == i){
                System.out.println(k+" ");
            }
        }
    }
    
    /**
     * 题目:一个球从100米高处自由下落,每次落地后反跳回原高度的一半,
     * 再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
     */
    
    public void test10s(){
        int distance = 100;//球的运动历程,第一次落地经历了100米
        int hight = 100;//高度
        int lastHight = 0;//最后一次反弹的高度
        for(int i=2;i<=10;i++){
            hight = hight/2;
            distance = distance + hight*2;
        }
        lastHight = hight/2;
        System.out.println("共经过"+distance+"米第10次反弹的高度"+lastHight+"米");
    }
    
    
    public void test10(){
        
        int h = 100;//下落高度
        int s = 200;//路径
        
        for(int i=1;i<10;i++){
            h = h / 2;
            s = s + h*2;
        }
        
        System.out.println("第10次落地时共经过:"+s+"米");
        System.out.println("第10次反弹的高度为:"+h/2+"米");
    }
    
    /**
     * 题目:有1,2,3,4四个数字,能组成多少个互不相同的切无重复的三位数字?都是多少?
     */

    public void test11s(){
        List<Integer> list = new ArrayList<Integer>();
        for(int i=1;i<5;i++){
            list.add(i);
        }
        List lists = new ArrayList();
        while(true){
            Collections.shuffle(list);//洗牌
            int num = list.get(0)*100+list.get(1)*10+list.get(2);
            if(!lists.contains(num)){
                lists.add(num);
                System.out.println(num+"**"+lists.size());
            }
        }
    }
    
    public void test11(){
        int count = 0;
        for(int x=1 ; x<5 ; x++ ){
            for(int y=1 ; y<5;y++){
                for(int z=1 ; z<5 ; z++){
                    if(x != y && y != z && x != z){
                        count ++;
                        System.out.print(x*100+y*10+z+",");
                    }
                }
            }
        }
        System.out.println("共组成"+count+"个数");
    }
    
    /**
     * 题目:一个数,加上100是一个完全平方数,再加上168又是一个完全平方数,请问该数是啥?
     */
    
    public void test12(){
        
        for(int i=1;i<100000;i++){
            if(Math.sqrt(i+100)%1 == 0){
                if(Math.sqrt(i+168)%1 == 0){
                    System.out.println(i+"加上100是完全平方数,再加168又是一个完全平方数");
                }
            }
        }
    }
    /**
     * 查看一个数是否为整数:
     * 满足 X%1 == 0 即为整数 
     */
    
    public void test13(){
        System.out.println("Math.sqrt(7)="+Math.sqrt(7)+"\nMath.sqrt(7)%1="+Math.sqrt(7)%1);
        System.out.println("5%1="+5%1+"\n5/1="+5/1);
    }
    
    /**
     * 题目:输入年月日,判断这一天是这一年的第几天
     */
    public void test14(){
        
        Scanner input = new Scanner(System.in);
        
        int year,month,day;
        int days = 0;
        int d = 0;
        int e = 0;
        
        do{
            e = 0;
            
            System.out.println("请输入年:");
            year = input.nextInt();
            System.out.println("请输入月:");
            month = input.nextInt();
            System.out.println("请输入日:");
            day = input.nextInt();
            
            if(year<0 || month<0 || month>12 || day<0 || day>31){
                e = 1;
                System.out.println("非法日期,请重新输入......");
            }
            
        }while(e == 1);
        
        for(int i=1;i<month;i++){
            switch(i){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;
                    break;
                case 2:
                    if((year%400 == 0) || (year%4 == 0 && year%100 != 0)){
                        days = 29;
                    }else{
                        days = 28;
                    }
                    
                
            }
            d += days;
        }
        
        System.out.println("这是这一年的第"+(d+day)+"天");
    }

    /**
     * hashMap的key和value均可以为null
     */
    public void testMap(){
        HashMap hash = new HashMap();
        hash.put("name",null);
        hash.put("name","Jack");
        hash.put(null, "Rouce");
        System.out.println("输出结果1:"+hash.get("name"));//输出 Jack
        System.out.println("输出结果2:"+hash.get(null));//输出Rouce
    }

    /**
     * 排序
     * 从小到大
     */
    
    public void test15s(){
        System.out.println("请输入四个整数");
        Scanner s = new Scanner(System.in);
        List<Integer> list = new ArrayList<Integer>();
        for(int i=1;i<=4;i++){
            list.add(s.nextInt());
        }
        Collections.sort(list);
        for(int a:list){
            System.out.print(" "+a+" ");
        }
    }
    
    public void test15(){
        int a = 0,b = 0,c = 0,e=0;
        System.out.println("请输入任意四个整数:");
        Scanner input = new Scanner(System.in);
        a = input.nextInt();
        b = input.nextInt();
        c = input.nextInt();
        e = input.nextInt();
        int[] array = {a,b,c,e};
        int n = array.length;
        
        for(int i=0;i<n-1;i++){
            
            for(int j=0;j<n-i-1;j++){
                
                int temp = 0;
                if(array[j]>array[j+1]){
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }
        
        System.out.println("排序后的数组:");
        
        for(int d:array){
            
            System.out.print(d+"  ");
        
        }
        
    }
    
    /**
     * 9*9乘法表
     */
    
    public void test16(){
        for(int i=1;i<10;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+(j*i)+" ");
                if(i*j<10)
                    System.out.print(" ");
            }
             System.out.println();
        }
    }
    
    /**
     * 猴子吃桃问题
     * 每天都吃掉拥有桃子的一半零一个
     * 第十天还剩一个,问第一天一共吃多少个桃子
     * 答案:第1天总数3070天吃了:1536个桃子
     */
    
    public void test17s(){
        int result = 1;
        int eatNUm = 0;
        int day=10;
        for(int i=1;i<=10;i++){
            result = (result+1)*2;
            eatNUm = result/2+1;
            System.out.println("第"+day+"天总数"+result+"天吃了:"+eatNUm+"个桃子");//1536
            day--;
        }
        
    }
    
    public void test17(){
        int surplus = 1;
        for(int i=1;i<=10;i++){
            surplus = (surplus+1)*2;
        }
        System.out.println("第一天一共有 "+(surplus/2+1)+" 个桃子");
    }
    /**
     * 验证猴子吃桃问题
     */
    
    public void test18(){
        int a = 3070;
        for(int i=1;i<=10;i++){
            a = a/2-1; 
        }
        System.out.println("十天后剩余桃子数量:"+a);
    }
    /**
     * 打印实心菱形
     */
    
    public void test19(){
        
        Scanner input = new Scanner(System.in);
        int num = 0;
        int e = 0;
        do{
            System.out.println("请输入菱形行数:");
            num = input.nextInt();
            e = 0;
            if(num<3){
                
                System.out.println("菱形行数不能低于3行,请重新输入行数...");
                e = 1;
                
            }
            if(num>3 && num%2 == 0){
                System.out.println("菱形为基数行,请重新输入行数...");
                e = 1;
            }
            
        }while(e==1);
        
        int n = (num-1)/2+1;
        for(int i=1 ; i<= n; i++){

            //输入空格
            for(int k = n-i ; k>0 ; k--){
                System.out.print(" ");
            }
            //输入星
            for(int j = 1;j <= 2*i-1;j++){
                System.out.print("*");
            }
            //换行
            System.out.println();
        }
        
        for(int x=n-1 ; x>0 ; x--){
            
            for(int z=1 ; z<=n-x ; z++){
                System.out.print(" ");
            }
            for(int y=1 ; y<=(2*x-1) ; y++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
    /**
     * 打印空心菱形
     */
    
    public void test19s(){
        
        Scanner input = new Scanner(System.in);
        
        int num = 0;
        int e = 0;
        do{
            e = 0;
            
            System.out.println("请输入空心菱形的行数:");
            num = input.nextInt();
            
            if(num<3){
                 System.out.println("菱形不能低于三行,请重新输入...");
                 e = 1;
             }
             
             if(num%2 == 0){
                 System.out.println("请输入奇数行...");
                 e = 1;
             }
            
        }while(e==1);
        
        //
        int up = (num+1)/2;
        for(int i=1 ; i<=up ; i++){
            //打印空格
            for(int j = up-i ; j>0 ; j--){
                System.out.print(" ");
            }
            //打印星
            System.out.print("*");
            //如果是第一行结束本次循环,进入下次循环
            if(i==1){
                System.out.println();
                continue;
            }
            //打印空格
            for(int k=0 ; k<2*i-3 ; k++){
                System.out.print(" ");
            }
            //打印星
            System.out.print("*");
            
            System.out.println();
        }
        //
        int down = (num-1)/2;
        for(int x=1 ; x<=down ; x++){
            
            for(int y=0 ; y<x ; y++){
                System.out.print(" ");
            }
            
            System.out.print("*");
            //如果是最后一次循环,结束整个循环
            if(x==down){
                break;                
            }
            
            for(int z=(down-x)*2-1 ; z>0 ; z--){
                System.out.print(" ");
            }
            
            System.out.print("*");
            System.out.println();
        }
        
    }
    /**
     * 求分数序列的前5项之和
     * 2/1,3/2,5/3,8/5,13/8,21/13...
     */
    
    public void test20(){
        int sum = 0 ;
        int x=1,y=2;
        int t;
        
        System.out.print("20项之和为:sum=");
        
        for(int i=0;i<5;i++){
            sum += y/x;
            
            if(i == 19){
                System.out.print(y+"/"+x+"="+sum);
            }else{
                System.out.print(y+"/"+x+"+");
            }
            
            
            t = x;
            x = y;
            y = x+t;
        }
    }
    
    /**
     * 求前n项的阶乘之和
     *  1!+2!+3!+4!+5!=?
     */
    
    public void test21(){
        System.out.println("请输入几位整数的阶乘相加:");
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int sum = 0;
        int a=1;
        for(int i=1;i<=n;i++){
            
            a=1;
            
            for(int j=1;j<=i;j++){
                
                a*=j;
                if(j==i){
                    
                    if(j==n){
                        System.out.print(j);
                    }else{
                        System.out.print(j+"+");
                    }
                    
                }else{
                    System.out.print(j+"*");
                }
            }
            
            sum += a;
        }
        System.out.print("="+sum);
    }
    
    /**
     *用递归的方式求5! 
     */
    @Test
    public void test22(){
        
        Digui digui = new Digui();
        
        System.out.print("="+digui.getResult(5)+"=5!");
    }
    
    /**
     * 问第5个人多大年龄
     * 第5个比第4个,第四个比第三个,第三个比第二个,第二个比第一个大2岁,第一个10岁
     */
    
    public void test23(){
        int age = 10;
        for(int a=1;a<5;a++){
            age+=2;
        }
        System.out.println("第五个人的年龄为:"+age);
    }
    /**
     * 输入一个不多于5位的整数
     * 打印这个数的位数,并逆序打印各个数字
     */
    
    public void test24(){
        Scanner input = new Scanner(System.in);
        int num = 0;
        int e=0;
        
        do{
            System.out.println("请输入不多于五位的数字:");
            num = input.nextInt();
            
            if(num/100000 != 0){
                
                System.out.println("非法数字");
                e=1;
            }
            
        }while(e==1);
        
        String nums = num+"";
        
        System.out.println(num+"的位数为:"+ (nums.trim().length()));
        
        char[] s = nums.toCharArray();
        
        String numNew = "";
        for(int i=s.length-1 ; i>=0 ; i--){
            numNew += String.valueOf(s[i]);
        }
        System.out.println("反转后的顺序为:"+numNew);
    }
    /**
     * 判断一个数是否为回文数
     * 12321
     * (0是最小的回文数)
     */
    
    public void test25(){
        
        System.out.println("请输入回文数字:");
        
        Scanner input = new Scanner(System.in);
        
        int num = input.nextInt();
        String nums = num+"";
        
        char[] array = nums.toCharArray();
        int leng = array.length;
        
        boolean isHW = true;
        
        for(int i=0;i<leng/2;i++){
            
            if(array[i] != array[leng-i-1]){
                isHW = false;
            }
        }
        
        if(isHW){
            System.out.println("是回文数");
        }else{
            System.out.println("不是回文数");
        }
    }
    /**
     * 题目:输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母
     * Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday
     */
    
    public void test26(){
        
        System.out.println("Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday");
        
        Scanner input = new Scanner(System.in);
        String value = "";
        
        int e=0;
        int i=1;
        String st="";
        do{
            e = 0;
            
            System.out.println("请输入星期几的第"+i+"个字母:");
            
            value = input.next();
            
            if(i==1){
                
                if(value.toUpperCase().equals("M")){
                    
                    System.out.println("Monday 星期一");
                    return;
                    
                }

                if(value.toUpperCase().equals("F")){
                    
                    System.out.println("Monday 星期五");
                    return;
                    
                }
                
                if(value.toUpperCase().equals("T")){
                    i++;
                    e=1;
                    st="t";
                    System.out.println("存在多个首字母为T的星期几");
                }
                if(value.toUpperCase().equals("S")){
                    i++;
                    e=1;
                    st="s";
                    System.out.println("存在多个首字母为S的星期几");
                }
            }
            
            if(i==2 ){
                if("T".equals(st.toUpperCase())){
                    if(value.toUpperCase().equals("U")){
                        System.out.println("Tuesday 星期二");
                        return;
                    }
                    if(value.toUpperCase().equals("H")){
                        System.out.println("Thursday 星期四");
                        return;
                    }
                }
                
                if("S".equals(st.toUpperCase())){
                    if(value.toUpperCase().equals("A")){
                        System.out.println("Saturday 星期六");
                        return;
                    }
                    if(value.toUpperCase().equals("U")){
                        System.out.println("Sunday 星期日");
                        return;
                    }
                }
            }
            
            
        }while(e==1);
    }
    
    /**
     * 打印100以内的所有素数(只能被1或者本身整除的数),也称质数
     */

    public void test27(){
        
        int[] base = {2,3,5,7};
        System.out.print("100以内的素数为:");
        int sum = 4;
        
        for(int k=0;k<base.length;k++){
            System.out.print(base[k]+" ");
        }
        for(int i=11;i<100;i+=2){
            
            boolean sushu = true;
            for(int j=0;j<base.length;j++){
                
                if(i%base[j] == 0){
                    sushu = false;
                }
            }
            
            if(sushu){
                sum++;
                System.out.print(i+" ");
            }
        }
        System.out.println("个数为:"+sum);
    }
    
    public void test27s(){
        
        System.out.print("100以内的素数为:");
        System.out.print(2+" ");
        int sum = 1;
        
        for(int i=3;i<100;i+=2){
            
            boolean sushu = true;
            
            for(int j=2;j<=Math.sqrt(i);j++){
                if(i%j==0){
                    sushu = false;
                }
            }
            
            if(sushu){
                sum++;
                System.out.print(i+" ");
            }
        }
        System.out.println("个数为:"+sum);
    }
    
    /**
     * 给10个数进行排序
     * 从大到小
     */
    
    public void test28(){
        System.out.println("请输入10个数:");
        int[] array = new int[10];
        Scanner input = new Scanner(System.in);
        String str="";
        
        for(int i=0;i<10;i++){
            array[i] = input.nextInt();
            str += array[i]+" ";
        }
        
        System.out.println("排序之前:"+str);
        
        for(int j=0;j<array.length;j++){
            
            for(int k=j+1;k<array.length;k++){
                
                if(array[j]<array[k]){
                    
                    int temp = array[j];
                    array[j] = array[k];
                    array[k] = temp;
                }
            }
        }
        
        System.out.print("排序之后:");
        for(int z=0;z<array.length;z++){
            System.out.print(array[z]+" ");
        }
    }
    
    /**
     * 求3*3矩阵对角线之和
     */

    public void test29(){
        
        System.out.println("请输入矩阵数据:");
        Scanner input = new Scanner(System.in);
        int[][] array = new int[3][3];
        int sumLift = 0;
        int sumRight = 0;
        
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                array[i][j] = input.nextInt();
            }
        }
        
        System.out.println("你输入的矩阵为:");
        
        
        for(int i=0;i<3;i++){
            
            for(int j=0;j<3;j++){
                
                if(i == j){
                    sumRight += array[i][j];
                }
                if(i+j == 2){
                    sumLift += array[i][j];;
                }
                
            System.out.print(array[i][j]+" ");
            
            }
            
            System.out.println();
        }
        System.out.println("左边对角线之和为:"+sumLift+" 右边对角线之和为:"+sumRight);
    }
    
    /**
     * 在有序数组中新添元素并排序
     */
    
    public void test30(){
        int[] arrayOld = new int[]{1,12,32};
        int[] arrayNew = new int[arrayOld.length+1];
        int length = arrayOld.length;
        
        System.out.println("插入新数之前的有序数组为:1 12 32");
        System.out.println("请输入要插入的新数字");
        
        Scanner input = new Scanner(System.in);
        int newNum = input.nextInt();
        
        for(int i=0;i<length;i++){
            arrayNew[i] = arrayOld[i];
        }
        arrayNew[length] = newNum;
        //排序方法一
        int lengthNew = arrayNew.length;
        for(int i=0;i<lengthNew;i++){
            int temp;
            for(int j=0;j<lengthNew-1;j++){
                if(arrayNew[j]>arrayNew[j+1]){
                    temp = arrayNew[j];
                    arrayNew[j] = arrayNew[j+1];
                    arrayNew[j+1] = temp;
                }
            }
        }
        //排序方法二
    /*    for(int j=0;j<length;j++){
            for(int k=0;k<length-j;k++){
                int temp;
                if(arrayNew[k]>arrayNew[k+1]){
                    temp = arrayNew[k+1];
                    arrayNew[k+1] = arrayNew[k];
                    arrayNew[k] = temp;
                }
            }
        }*/
        System.out.print("插入新数之后的有序数组为:");
        for(int a:arrayNew){
        System.out.print(a+" ");    
        }
    }
    
    /**
     * 将数组逆序输出
     */
    
    public void test31(){
        int[] a = new int[5];
        
        String s="";
        String ss="";
        
        System.out.println("请向数组中添加数据:");
        Scanner in = new Scanner(System.in);
        for(int i=0 ; i<5 ; i++){
            a[i] = in.nextInt();
            s+=a[i]+" ";
        }
        System.out.println("数组内容:"+s);
        
        for(int j=0;j<5;j++){
            ss+=a[a.length-1-j]+" ";
        }
        System.out.println("反转内容:"+ss);
    }
    
    /**
     *一个数乘以4后顺序反过来 
     */
    
    public void test32(){
        for(int i=10000;i<=99999 ; i++){
            
            String j = i*4+"";

            char[] b = j.toCharArray();
            char[] c = (i+"").toCharArray();

            int z=0;
            for(int k=0;k<=4;k++){
                
                if(b[k] == c[4-k]){
                    z++;
                }
            }
            if(z==5){
                System.out.println(i+"成以4之后:"+(i*4));
            }
            
        }
    }
    
    /**
     *前180个正偶数之和减后180个正奇数之和的差是多少
     */
    
    public void test33(){
        
        int evenSum=0;//偶数和
        int oddSum=0;//奇数和
        
        for(int i=1;i<=180;i++){
            if(i%2==0){
                evenSum += i;
            }else{
                oddSum += i;
            }
        }
        
        System.out.println(evenSum+"-"+oddSum+"="+(evenSum-oddSum));
    }
    
    /**
     * 输出字符串中连续数字最长的串
     */
    public void test34(){
        
        System.out.println("请输入字符串:");
        Scanner input = new Scanner(System.in);
        String str = input.next();
        String n = "";//连续数字
        String[] array=null;//所有连续数字
        
        char[] strNew = str.toCharArray();
        
        for(int i=0;i<strNew.length;i++){
            if((strNew[i]>'a' && strNew[i]<'z') || (strNew[i]>'A' && strNew[i]<'Z')){
                n+="a";
            }else{
                n+=strNew[i];
            }
        }
        
        System.out.println("变更过的数据:"+n);
        array = n.split("a");
        int length=array[0].length();
        int index=0;
        
        for(int j=0;j<array.length;j++){
            if(array[j].length() > length){
                length = array[j].length();
                index = j;
            }
        }
        
        System.out.println("连续数字最长的是:"+array[index]+" \n长度为:"+length);
    }
    
    /**
     * 约瑟夫环
     */
    public void test35(){
        putDate1(5,3);
    }
    
    public void putDate1(int length,int index){
        
        LinkedList link = new LinkedList();
        
        for(int i=0;i<length;i++){
            link.add(i);
        }
        
        int current = 1;
        loopDate(link,index,current);
    }
    
    StringBuffer sbuffer = new StringBuffer();
    
    public void loopDate(LinkedList list,int index,int current){
        
        if(list.size()>1){
            
            for(int i=0;i<list.size();i++){
                
                if(current == index){
                    sbuffer.append(list.get(i)+" ");
                    list.remove(i);
                    current = 0;
                    i--;
                }
                current++;
            }
            
            loopDate(list,index,current);
            
        }else{
            
            sbuffer.append(list.get(0));
            System.out.println("输出顺序为:"+sbuffer.toString());
            System.out.println("最后一个输出的是:"+list.get(0));
        }
    }
    
    /**
     * 杨辉三角形
     */
    public void test36s(){
        int linkNum = 10;
        test36();
    }
    //@Test
    public void test36(){
        
        int[][] a = new int[10][10];
    
        for(int i=0;i<10;i++){
            a[i][i] = 1;
            a[i][0] = 1;
        }
        
        for(int i=2;i<10;i++){
            for(int j=1;j<i;j++){
                a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
        
        for(int i=0;i<10;i++){
            
            for(int j=0;j<2*(10-i)-1;j++){
                System.out.print(" ");//此处一个空格
            }
            
            for(int k=0;k<=i;k++){
                System.out.print(a[i][k]+"  ");//此处两个空格
            }
            System.out.println();
        }
        
    }
    
    //合并两个有序数组
    @Test
    public void test37() {
        int[] a = { 1, 3, 5 };
        int[] b = { 2, 3, 4, 7 };
        int l = a.length + b.length;
        int[] temp = new int[l];
        int i = 0, j = 0, h = 0;
        // 这里必须用while,不能用for
        while (i < a.length || j < b.length) {
            if (i == a.length && j < b.length) {
                temp[h++] = b[j++];
            } else if (i < a.length && j == b.length) {
                temp[h++] = a[i++];
            } else if (a[i] <= b[j]) {
                temp[h++] = a[i++];
            } else if (a[i] > b[j]) {
                temp[h++] = b[j++];
            }
        }
        for (int m : temp) {
            System.out.print(m + ", ");
        }
    }

 

 

//获取数组中重复元素的最大距离
    public int getMaxDistance(int[] array) {
        Map<Integer,Integer> map = new HashMap<>();
        int maxDistance = 0;
        for(int i=0;i<array.length;i++) {
            if(map.containsKey(array[i])) {
                maxDistance = Math.max(maxDistance, i-map.get(array[i]));
            }else {
                map.put(array[i], i);
            }
        }
        return maxDistance;
    }

 

/**
*描述
*输入两个正整数,输出这两个正整数之间(左闭右闭,即判断包括这两个整数在内有多少素数)有多少个素数。

*示例
*输入:1 100
*输出:25
*1到100之间有25个的素数
*
* 1 <= L <= R <= 1000
*/
public int primeCount(int L, int R) {
  int sum=0;
  for(int i=L;i<=R;i++){
    int k=2;
    for(int j=2;j<=i;j++){
         if(i%j==0 && i!=j){/////
        k=1;
        break;
      }
    }
    if(k==2){
     sum++;
    }
  }
  return sum;
}

 

posted @ 2017-07-01 18:17  存钱罐  阅读(227)  评论(0编辑  收藏  举报