JAVA经典试题四十道

一个月前看到了JAVA经典试题五十道,很感兴趣,也当做是机会提升自己的能力,本想着一个周做完,但是计划太唐突,根本想不到做这些题目这么需要花时间,而且基本都是无聊的数组数组数组!!!就靠你的纯逻辑逻辑逻辑!!!说是五十道,到最后剩下10道题的时候,我的意志力崩溃了,深深愚昧的觉得太浪费我时间了,还不如放弃剩下的题目(毕竟已经做了40道了)去学点新知识,所以我就开始准备这篇博文,就当做是总结,也为之后的朋友留下点有用的东西。

正:

程序1 TestRabbit.java
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.
程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....


【程序2 FindPrimeNumber.java
题目:判断101-200之间有多少个素数,并输出所有素数。
1.
程序分析:判断素数的方法:用一个数分别去除2sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。


【程序3FindDaffodilNumber.java
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:
153
是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
1.
程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。


【程序4FenJie.java
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)
如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)
如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。
(3)
如果n不能被k整除,则用k+1作为k的值,重复执行第一步。


【程序5 ConditionOperator.java
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
1.
程序分析:(a>b)?a:b这是条件运算符的基本例子。


【程序6Test1.java GcdTest.java后者是辗转相除法
题目:输入两个正整数mn,求其最大公约数和最小公倍数。
1.
程序分析:利用辗除法。


【程序7 StChar.java
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.
程序分析:利用while语句,条件为输入的字符不为'\n'.


【程序8 TestAdd.java
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。


【程序9 WanShu.java
题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=123.编程 找出1000以内的所有完数。


【程序10TestBall.java
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?


【程序11 TestTN.java

题目:有1234个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.
程序分析:可填在百位、十位、个位的数字都是1234。组成所有的排列后再去 掉不满足条件的排列。


【程序12 MoneyAward.java
题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%20万到40万之间时,高于20万元的部分,可提成5%40万到60万之间时高于40万元的部分,可提成3%60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
1.
程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。


【程序13FindNumber.java
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。请看具体分析:


【程序14 TestDay.java
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.
程序分析:以35日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。


【程序15TestCompare.java
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.
程序分析:我们想办法把最小的数放到x上,先将xy进行比较,如果x>y则将xy的值进行交换,然后再用xz进行比较,如果x>z则将xz的值进行交换,这样能使x最小。
【程序16Nine.java
题目:输出9*9口诀。
1.
程序分析:分行与列考虑,共99列,i控制行,j控制列。


【程序17MonkeyEatPeach.java

题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
1.
程序分析:采取逆向思维的方法,从后往前推断。


【程序18 Prog.java
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
1.
程序分析:判断素数的方法:用一个数分别去除2sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。


【程序19LingXing.java
题目:打印出如下图案(菱形)
    *
   ***
 ******
********
 ******
   ***
    *
1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制行,第二层控制列。


【程序20TestAdd2.java
题目:有一分数序列:2/13/25/38/513/821/13...求出这个数列的前20项之和。
1.
程序分析:请抓住分子与分母的变化规律。


【程序21TestJieCheng.java
题目:求1+2!+3!+...+20!的和
1.程序分析:此程序只是把累加变成了累乘。


【程序22
题目:利用递归方法求5! TestJieCheng.java
1.程序分析:递归公式:fn=fn_1*4!


【程序23TestAge.java
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
1.
程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。


【程序24TestNumber.java
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。


【程序25 HuiWenShu.java
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
【程序26Ex26.java
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
1.
程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
【程序27 SuShu.java
题目:求100之内的素数
【程序28 TestSort.java
题目:对10个数进行排序
1.
程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第

二个元素与后8个进行比较,并进行交换。
【程序29 TestAdd3.java
题目:求一个3*3矩阵对角线元素之和
1.
程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
【程序30 ArraySort.java
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1.
程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,

依次后移一个位置。
【程序31 ArrayConverse.java
题目:将一个数组逆序输出。
1.
程序分析:用第一个与最后一个交换。
【程序32 Ex32.java
题目:取一个整数a从右端开始的47位。
程序分析:可以这样考虑:
(1)
先使a右移4位。
(2)
设置一个低4位全为1,其余全为0的数。可用~(~0<<4)
(3)
将上面二者进行&运算。
【程序33YangHui.java
题目:打印出杨辉三角形(要求打印出10行如下图)
1.
程序分析:
     1
    1 1
   1 2 1
  1 3 3 1
 1  4 6 4 1
1 5 10 10 5 1

【程序34 前面更复杂的已经做过了
题目:输入3个数a,b,c,按大小顺序输出。
1.
程序分析:利用指针方法。
【程序35 ArrayChange.java
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
【程序36 Array1.java
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
【程序37 Test3Quit.java
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从13报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
【程序38 TestLength.java
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
【程序39 Test2.java
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数

1/1+1/3+...+1/n(
利用指针函数)
【程序40 Test3.java
题目:字符串排序。
【程序41 MonkeyPeach.java
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一

个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中

,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

【程序42 Test4.java
题目:809*??=800*??+9*??+1
其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
【程序43 Test5.java
题目:求0—7所能组成的奇数个数。
【程序44 TestEven.java
题目:一个偶数总能表示为两个素数之和。
【程序45TestPrime9.java
题目:判断一个素数能被几个9整除
【程序46 TestString.java
题目:两个字符串连接程序
【程序47 TestPrint.java
题目:读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。
【程序48 TestCode.java
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字

都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

【程序49 TestString2.java
题目:计算字符串中子串出现的次数
【程序50TestStu.java

题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算 


。。。。。。。。。。。我是分割线。。。。。。。。。。。。。


原创代码,需要分析的朋友可以留言,小白会第一时间认真解答,要是有哪里不对的请及时帮我指出。


package package01;

/**
 * Created by sakura on 16/5/28.
 */
/**
 * 有5个人坐在一起,
 * 问第五个人多少岁?他说比第4个人大2岁。
 * 问第4个人岁数,他说比第3个人大2岁。
 * 问第三个人,又说比第2人大两岁。
 * 问第2个人,说比第一个人大两岁。
 * 最后问第一个人,他说是10岁。
 * 请问第五个人多大?
 *
 *
 *  使用递归算法
 *
 * */
public class Age {
    public static void main(String[] args){
        System.out.print(getAge(5));
    }
    public static int getAge(int person){
        if(person == 1)
            return 10;
        else
            return getAge(person - 1) + 2;
    }
}

package package01;

import java.util.Scanner;

/**
 * Created by sakura on 16/5/30.
 */
/**
*
*       将一个数组逆序输出
 *
 *      当a数组以实参传入函数Fun()时,函数内部对形参的操作改变了原始数组a的值,
 *      这是因为数组传递的是引用,对引用的操作就是对原本数组的操作
 *
* **/
public class ArrayChange {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int a[] = new int[5];
        for(int fi = 0;fi < a.length;fi++){
            a[fi] = in.nextInt();
        }
        Fun(a);
        for(int fj = 0;fj < a.length;fj++){
            System.out.print(a[fj]);
        }
    }
    public static void Fun(int[] a){
        int i,j,k;
        int time;
        if(a.length % 2 == 0){
            i = 0;
            j = a.length - 1;
            while(i + 1 != j){
                time = a[i]; a[i] = a[j]; a[j] = time;
                i++; j--;
            }
        }else{
            i = 0;
            j = a.length - 1;
            k = (a.length + 1) / 2 - 1;     //中间数组的下标
            while(i != k){
                time = a[i]; a[i] = a[j]; a[j] = time;
                i++; j--;
            }
        }
    }
}

package package01;

/**
 * Created by sakura on 16/5/14.
 */
/**
 * 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
 *
 * */
public class Ball {
    public static void main(String[] args){
        int time = 1;
        double h = 100;
        double totalH = 0;
        while(time <= 10){
            totalH = totalH + h + h/2;
            h = h/2;
            time++;
        }
        System.out.println("10次落地共经过: "+totalH+"\n第10次反弹高度为 "+h);
    }
}

package package01;
import static java.lang.Math.pow;
/**
 * Created by sakura on 16/5/12.
 */
/**
 * 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+222,2+222,22(此时共有5个数相加),
 * 几个数相加有键盘控制。
 * 
1.程序分析:关键是计算出每一项的值。
 *
 *          答案正确
 * */
public class composition {
    public static void main(String[] args){
        System.out.println(calculate(1,10));
    }
    public static int calculate(int baseData,int comTime){
        int total = 0;
        int time=0;
        int nowData = 0;
        int lastData = 0;
        while(time < comTime){
            nowData = baseData * (int)pow(10,time++) + lastData;
            total = total + nowData;
            lastData = nowData;
            System.out.println(nowData);
        }
        return total;
}
}

package package01;

/**
 * Created by sakura on 16/5/10.
 */
/**
 * 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
 *
 *          已测试 答案正确
 * */

import java.util.Scanner;
import java.lang.*;
public class DecomposeInteger {
    public static void main(String[] args){
        int[] a = new int[30];
        int i = 0;
        int number = 0;
        int time = 2;
        Scanner in =new Scanner(System.in);
        try{
            number = in.nextInt();
        }catch(Exception e){
            System.out.println("输入有误!");
        }
        while(calculatePrimeNumber(number) == false) {
            while (number % time != 0) {
                time++;
            }
            number = number / time;
            a[i++] = time;
            time = 2;
        }
        a[i] = number;
        for(int forTime = 0;forTime <= i;forTime++){
            if(forTime == 0){
                System.out.print("="+a[forTime]);
            }else {
                System.out.print("*"+a[forTime]);
            }
        }

    }
    public static boolean calculatePrimeNumber(int number){
        for(int forTime = 2;forTime <=Math.sqrt(number);forTime++){
            if(number%forTime == 0){
                return false;
            }
        }
        return true;
    }
}

package package01;

/**
 * Created by sakura on 16/5/15.
 */
/**
 * 求1+2!+3!+...+20!的和
 *
 * !是阶乘的符号。对于所有正整数n,n!=1*2*3... *n;而规定0!=1
 *
 *              答案正确 268040729
 *
 * */
public class Factorial {
    public static void main(String[] args){
        int count = 20;
        int i = 1;
        int total = 0;
        while(i <= count){
            total += calculate(i++);
        }
        System.out.println(total);
    }
    public static int calculate(int number){
        int i = 1;
        int total = 1;
        while(i <= number){
            total = total * i;
            i++;
        }
        return total;
    }
}

package package01;

/**
 * Created by sakura on 16/5/15.
 */
//    *
//   ***
//  *****
// *******
//  *****
//   ***
//    *
public class Figure {
    public static void main(String[] args){
        int rbombusH = 10;
        int space = (rbombusH - 1) / 2 ;
        int countUp = 1;
        int whileTime = 1;
        int i = 0;
        while(whileTime <= (rbombusH - 1) / 2){
            i = 0;
            while(i < space) {
                System.out.print(" ");
                i++;
            }
            space -= 1;
            i = 0;
            if(whileTime == 1)
                while(i < countUp) {
                    System.out.print("*");
                    i++;
                }
            else
                while(i < countUp) {
                    System.out.print("*");
                    i++;
                }
            System.out.print("\n");
            countUp += 2;
            whileTime++;
        }

        i = 0;
        while(i < countUp){
            System.out.print("*");
            i++;
        }
        System.out.print("\n");
        whileTime = 1;
        space = 1;
        while(whileTime <= (rbombusH - 1) / 2){
            countUp -= 2;
            i = 0;
            while(i < space){
                System.out.print(" ");
                i++;
            }
            i = 0;
            while(i < countUp){
                System.out.print("*");
                i++;
            }
            System.out.print("\n");
            whileTime++;
            space++;
        }
    }
}

package package01;

/**
 * Created by sakura on 16/5/28.
 */

import java.util.Scanner;

/**
 *有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中
 *
 *      //SP 11112345的情况 开头几个数字是相同的情况
 * */
public class InsertNumberToArray {
    public static int arrayCount = 6;   //设置有几个数字
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int[] number = new int[100];
        for(int i = 0;i < arrayCount;i++){
            number[i] = in.nextInt();
        }
        int[] newNumber = Fun(number,9);
        for(int i = 0;i < arrayCount + 1;i++){
            System.out.println(newNumber[i]);
        }
    }
    public static int[] Fun(int[] number,int i){
        if(number[0] > number[1]){
            if(i >= number[0]){
                for(int time = arrayCount;time > 0;time--){
                    number[time] = number[time - 1];
                }
                number[0] = i ;
            }else{
                int time = 0;
                while(true){
                    if(number[time] >= i && number[time + 1] <= i){
                        break;
                    }
                    if(time != arrayCount - 1)
                        time++;
                    else
                        break;
                }
                time++;
                for(int time1 = arrayCount - 1;time1 >= time;time1 --){
                    number[time1 + 1] = number[time1];
                }
                number[time] = i;
             }
            return number;
        }else if(number[0] < number[1]){
            if(i <= number[0]){
                for(int time = arrayCount;time > 0;time--){
                    number[time] = number[time - 1];
                }
                number[0] = i ;
            }else{
                int time = 0;
                while(true){
                    if(number[time] <= i && number[time + 1] >= i){
                        break;
                    }
                    if(time != arrayCount - 1)
                        time++;
                    else
                        break;
                }
                time++;
                for(int time1 = arrayCount - 1;time1 >= time;time1 --){
                    number[time1 + 1] = number[time1];
                }
                number[time] = i;
            }
            return number;
        }else{
            int k = 1;
            int j = 2;
            int count = 0;
            int newArrayCount;
            while(number[k] == number[j]){
                k++;
                j++;
            }
            newArrayCount = arrayCount - k - 1;   //除了开头相同的数字剩下有几个数字
            if(number[k] > number[j]){
                if(i >= number[0]){
                    for(int time = arrayCount;time > 0;time--){
                        number[time] = number[time - 1];
                    }
                    number[0] = i ;
                }else{
                    int time = k;
                    while(true){
                        if(number[time] >= i && number[time + 1] <= i){
                            break;
                        }
                        if(time != arrayCount - 1)
                            time++;
                        else
                            break;
                    }
                    time++;
                    for(int time1 = arrayCount - 1;time1 >= time;time1 --){
                        number[time1 + 1] = number[time1];
                    }
                    number[time] = i;
                }
                return number;
            }else if(number[k] < number[j]){
                if(i <= number[k]){
                    for(int time = arrayCount;time > 0;time--){
                        number[time] = number[time - 1];
                    }
                    number[0] = i ;
                }else{
                    int time = k;
                    while(true){
                        if(number[time] <= i && number[time + 1] >= i){
                            break;
                        }
                        if(time != arrayCount - 1)
                            time++;
                        else
                            break;
                    }
                    time++;
                    for(int time1 = arrayCount - 1;time1 >= time;time1 --){
                        number[time1 + 1] = number[time1];
                    }
                    number[time] = i;
                }
                return number;
            }
        }
        return number;
    }
}

package package01;

/**
 * Created by sakura on 16/5/14.
 */
/***
 *
 *一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少
 */
public class JudgeNumber {
    public static void main(String[] args){
        int i = 11;
        int number =9;
        //System.out.println((int)Math.sqrt(5));
        while(true){
            if(calculate(number + 100) == true && calculate(number + 268) == true){
                System.out.println(number);
                break;
            }
            number++;
        }
    }
    public static boolean calculate(int number){
        if((int)Math.sqrt(number) * (int)Math.sqrt(number) == number)
            return true;
        return false;
    }
}

package package01;

/**
 * Created by sakura on 16/5/14.
 */

import java.util.Scanner;

/**
 * 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;
 * 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;
 * 20万到40万之间时,高于20万元的部分,可提成5%;
 * 40万到60万之间时高于40万元的部分,可提成3%;
 * 60万到100万之间时,高于60万元的部分,可提成1.5%,
 * 高于100万元时,超过100万元的部分按1%提成,
 * 从键盘输入当月利润I,求应发放奖金总数?
 *
 *
 * */
public class MoneyAward {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        double profit = in.nextDouble();
        double moneyAward = 0;
        if(profit <= 100000){
            moneyAward = profit * 0.1;
        }else if(profit < 200000){
            moneyAward = 100000 * 0.1 + (profit - 100000) * 0.075;
        }else if(profit < 400000){
            moneyAward = 200000 * 0.1 + (profit - 200000) * 0.05;
        }else if(profit < 600000){
            moneyAward = 400000 * 0.1 +(profit - 400000) * 0.03;
        }else if(profit < 1000000){
            moneyAward = 600000 * 0.1 +(profit - 600000) * 0.15;
        }else{
            moneyAward = 1000000 * 0.1 +(profit - 1000000) * 0.01;
        }
        System.out.println(moneyAward);
    }
}

package package01;

/**
 * Created by sakura on 16/5/15.
 */
/**
 * 猴子吃桃问题:
 * 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
 * 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。
 * 以后每天早上都吃了前一天剩下的一半零一个。
 * 到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
 *
 *      答案: 1534个
 *
 * */


public class MonkeyEat {
    public static void main(String[] args){
        int peach = 1;
        int whileTime = 9;
        while(whileTime >= 1){
            peach = (peach + 1) * 2;
            whileTime--;
        }
        System.out.println(peach);
    }
}

package package01;

/**
 * Created by sakura on 16/6/6.
 */
/**
 *  题目:海滩上有一堆桃子,五只猴子来分。
 *  第一只猴子把这堆桃子凭据分为五份,多了一个,
 *  这只猴子把多的一 

个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,
 *  又多了一个,它同样把多的一个扔入海中 

,拿走了一份,第三、第四、第五只猴子都是这样做的,
 *  问海滩上原来最少有多少个桃子?
 *
 * */
public class MonkeySeabeach {
    public static void main(String[] args){
        int x = 2;
        int m = 1;
        int y = 0;
        int i = 0;
        int t = 0;
        while(true){
            for(i = 0 ; i < 5 ; i ++){
                t = 0;
                if(i == 0)
                    y = x;
                if((x - 1) % 5 == 0){
                    x = (x - 1) - (x - 1) / 5;
                }else{
                    t = 1;
                    x = y;
                    break;
                }
            }
            if(i == 5 && t == 0)            //i == 5 ,  不是 i == 4 !!!!!!!!
                break;
            x++;
        }
        System.out.println("y="+y);
    }
}

package package01;
import static java.lang.Math.pow;
/**
 * Created by sakura on 16/5/10.
 */
/**
 * 【程序3题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
 * 例如: 
153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
 *
 *      答案正确
 * */
public class Narcissus {
    public static void main(String[] args){
        int number = 100;
        int count = 0;
        while(number <= 999){
            if(calculateNarcissus(number)){
                System.out.println(number);
                count++;
            }
            number++;
        }
        System.out.println("共有"+count+"个");
    }
    public static boolean calculateNarcissus(int number){
        if(number == pow((number%10),3) + pow((number%100 - number%10)/10,3) + pow((number - number%100)/100,3))
            return true;
        return false;
    }
}

package package01;

/**
 * Created by sakura on 16/5/10.
 */
/**
 * 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
 * 
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
 *
 *     答案正确
 * */
public class Nesting {
    public static void main(String[] args){
        int grade = 61;
        System.out.println((grade>=90)?"A":(grade>=60)?"B":"C");
    }
}

package package01;

/**
 * Created by sakura on 16/5/31.
 */

import java.util.Scanner;

/**
 *      输入数组,最大的与第一个元素交换,最小的与最后一个
 *
 * */
public class NumberChange {
    public static int numberCount = 5;
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int a[] = new int[100];
        int i;
        for(i = 0; i < numberCount; i++){
            a[i] = in.nextInt();
        }
        int m = a[0],n = 0,time;
        int b[] = a.clone();
        i = 0;
        while(a[i] != '\0'){
            if(a[i + 1] != '\0') {
                if (a[i + 1] >= m){
                    m = a[i + 1];
                    n = i + 1;
                }
            }
            i++;
        }
        time = a[0];
        a[0]= m;
        a[n] = time;
        i = 0;
        while(a[i] != '\0'){
            System.out.print(a[i]+" ");
            i++;
        }
        System.out.println();

        i = 0;
        m = b[0];
        n = 0;
        while(b[i] != '\0'){
            if(b[i + 1] != '\0'){
                if(b[i + 1] < m){
                    m = b[i + 1];
                    n = i + 1;
                }
            }
            i++;
        }
        time = b[numberCount - 1];
        b[numberCount - 1] = m;
        b[n] = time;
        i = 0;
        while(b[i] != '\0'){
            System.out.print(b[i]+" ");
            i++;
        }

    }
}

package package01;
//import static java.lang.Math.sqrt;        //如果用这种方式导入 调用sqr方法是不需要使用Math.来调用 可直接使用函数sqrt()
import java.lang.*;
/**
 * Created by sakura on 16/5/10.
 */
/**
 *
 *  题目:判断101-200之间有多少个素数,并输出所有素数。
 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
 则表明此数不是素数,反之是素数。

        已测试,答案正确

 */
public class PrimeNumber {
    public static void main(String [] args){
        int number = 101,count = 0;
        while(number <= 200){
            if(calculatePrimeNumber(number) == false){
                count++;
                System.out.println(number+"是素数");
            }
            number++;
        }
        System.out.println("共有"+count+"个素数");
    }
    public static boolean calculatePrimeNumber(int number){
        for(int forTime = 2;forTime <=Math.sqrt(number);forTime++){
            if(number%forTime == 0){
                return true;
            }
        }
        return false;
    }

}

package package01;

/**
 * Created by sakura on 16/5/9.
 */
/**
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,
 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....

            解:每一个月兔子总数等于前两个月的兔子总数之和
            已测试 答案正确
 **/
public class Rabbit {
    public static void main(String [] args){
        int[] rabbit = new int[100];
        int month = 0;
        rabbit[0] = 1;
        while(month <= 23){
            if(month == 0){
                System.out.printf("第%d月 = %d\n",month+1,rabbit[month]);
            }
            if(month == 1){
                rabbit[month] = rabbit[month-1];
                System.out.printf("第%d月 = %d\n",month+1,rabbit[month]);
            }
            if(month > 1) {
                rabbit[month] = rabbit[month - 1] + rabbit[month - 2];
                System.out.printf("第%d月 = %d\n", month + 1, rabbit[month]);
            }
                month++;
        }

    }
}

package package01;

/**
 * Created by sakura on 16/5/28.
 */
/**
 *  利用递归方法求5!
 *
 * 网友答案:
 *   public int getResult(int n){
        if(n==1){
            return 1;
        }else{
            return n*getResult(n-1);
        }


 * */
public class RecurrenceJieCheng {
    public static final int time = 4;
    public static int total = 1;

    public static void main(String[] args) {
        System.out.println(JieCheng2(1));
        //System.out.println(getResult(4));


    }

    public static int JieCheng(int a) {  //不是完美递归
        if (a < time) {
            total += total * a;
            a++;
            JieCheng(a);
        }
        return total;
    }
    public static int JieCheng2(int a){     //是递归,不过递归防方向不同,使用了final数据time,使程序繁琐
        if(a > time)
            return 1;
        else{
            return a * JieCheng2(a + 1);
        }

    }
    public static int getResult(int n) {   //完美递归,来自百度知道
        if (n == 1) {
            return 1;
        } else {
            return n * getResult(n - 1);
        }
    }
}

package package01;

/**
 * Created by sakura on 16/5/12.
 */

/**
 * 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
 *          答案正确
 *
 * */
public class StatisticsString {
    public static void main(String[] args){
        int countGrapheme = 0,countDigit = 0,countSpace = 0,countOther = 0;
        String string =new String("askqiwu       1123109q2 w***&^%$#$%1 3m ");
        int time = 0;
        while(time < string.length()){      //如果加了一个= 变成 time <= string.length() 就会报错,越界
            if(((int)string.charAt(time) >= 65 && (int)string.charAt(time) <= 90) || ((int)string.charAt(time) >= 97 && (int)string.charAt(time) <= 122))
                countGrapheme++;
            else if((int)string.charAt(time) >= 48 && (int)string.charAt(time) <= 57)
                countDigit++;
            else if((int)string.charAt(time) == 32)
                countSpace++;
            else
                countOther++;

            time++;
        }
        System.out.println("Grapheme = "+countGrapheme+"\n"+"Digit = "+countDigit+"\n"+"Space = "+countSpace+"\n"+"Other"+countOther);
    }
}

package package01;

/**
 * Created by sakura on 16/6/3.
 */
/**
 *  写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 
 *
 * */
public class StringLengh {
    public static void main(String[] args) {
        System.out.println(Fun("absdn hsjnah"));
    }

    public static int Fun(String string) {
        int i = 0;
        try {
            while (string.charAt(i) != '\0')
                i++;
        } catch (Exception e) {
            return i;
        }
    return 0;
    }
}

package package01;

import java.util.Scanner;

/**
 * Created by sakura on 16/6/5.
 */
/**
 *  字符串排序
 *
 *  */
public class StringRank {
    public static void main(String[] args){
        Scanner in  = new Scanner(System.in);
        int number = 4;
        String[] string = new String[20];
        for(int forTime = 0;forTime < number;forTime ++) {
            string[forTime] = in.next();
        }
        int i ,j,k;
        int time = 0;
        String change = new String();
        change = null;
        for(i = 0; i < number ; i ++){
            for(j = i + 1; j < number; j++){
                k = 0;
                time = 0;
                while(time == 0 && k < string[i].length() && k < string[j].length()){
                    if(string[i].charAt(k) > string[j].charAt(k)){
                        time = 1;
                    }else if(string[i].charAt(k) < string[j].charAt(k)){
                        change = string[i]; string[i] = string[j]; string[j] = change;
                        time = 1;
                    }else if(string[i].charAt(k) == string[j].charAt(k)) {
                        if (k == string[i].length() - 1 && k != string[j].length() - 1) {   //abc abcd的情况
                            change = string[i];
                            string[i] = string[j];
                            string[j] = change;
                            time = 1;
                        } else {
                            time = 0;
                            k++;
                        }
                    }
                }
            }
        }
        for(int forTime = 0; forTime < number ; forTime ++){
            System.out.println(string[forTime]);
        }
    }
}

package package01;

/**
 * Created by sakura on 16/5/14.
 */
/**
 *
 * 有1、2、3、4个数字,能组成多少个 互不相同 且 无重复数字 的三位数?都是多少?
 *
 *      利用for!!!!!no-complicatino
 *
 * */
public class Test1 {
    public static void main(String[] args){
        int i,j,k,count = 0;
        for(i = 1;i <= 4;i++){
            for(j = 1;j <= 4;j++){
                if(i != j){
                    for(k = 1;k <= 4;k++){
                        if(k!=i && k!=j){
                            System.out.println(i*100+j*10+k);
                            count++;
                        }
                    }
                }
            }
        }
        System.out.println("共有"+count);
    }
}

package package01;

/**
 * Created by sakura on 16/5/15.
 */
/**
 * 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
 *
 *
 *                 答案正确
 * */
public class Test2 {
    public static void main(String[] args){
        int count = 20;
        double up = 2, down = 1;
        double total = 0;
        int i = 1;
        double time = 0;
        while(i <= count){
            total += up / down;
            time = down;
            down = up;
            up = time + up;
            i++;
        }
        System.out.println(total);
    }
}

package package01;

/**
 * Created by sakura on 16/5/28.
 */

/**
 *
 * *   给一个正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
* **/
public class Test3 {
    public static void main(String[] atgs){
        System.out.print(getNumber("1989898980"));
    }
    public static int getNumber(String string){
        int oriNumber = Integer.valueOf(string);
        int newNumber = 0;
        int numberCount = string.length();
        int time = string.length() - 1;
        for(int i = 1;i <= string.length();i++){
            newNumber += getSpNumber(i,oriNumber) * Math.pow(10,time--);
        }
        return newNumber;
    }
    public static int getSpNumber(int a,int number){
        if(a == 1){
            return number % 10;
        }else{
            return (int) ((int) ((number % Math.pow(10,a)) - (number % Math.pow(10,a-1))) / Math.pow(10,(a - 1)));
        }
    }
}

package package01;

import java.util.Scanner;

/**
 * Created by sakura on 16/5/30.
 */
/**
 *
 *      取一个整数a从右端开始的4~7位
 *
 * */
public class Test4 {
    public static void main(String[] atgs){
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int n = ( a % (int) Math.pow(10,7) - a % (int) Math.pow(10,3) ) / 1000;
        System.out.println(n);
    }
}

package package01;

/**
 * Created by sakura on 16/6/1.
 */

import java.util.Scanner;

/**
 *      有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
 *
 * */
public class Test5 {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int a[] = new int[100];
        int count = 10;
        int m= 9;
        for(int i =0; i < count ; i ++){
            a[i] = in.nextInt();
        }
        int k = count - m;
        int timeCount = count;
        for(int j = count - m; j < count; j ++){
            a[timeCount++] = a[j];
        }
        System.out.println("**" + a[6]+"**"+a[7]);
        k = count - 1;
        for(int j = count - m - 1; j >= 0; j--){
            a[k--] = a[j];
        }
        k = count;
        for(int j = 0 ; j < m ; j ++){
            a[j] = a[k++];
        }
        for(int j = 0 ; j < count ; j ++){
            System.out.print(a[j]+" ");
        }
    }
}

package package01;

import java.util.Scanner;

/**
 * Created by sakura on 16/6/5.
 */
/**
 *      编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数 

1/1+1/3+...+1/n(利用指针函数)
 *
 * */
public class Test6 {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        double n = in.nextInt();
        double total = 0;
        if(n % 2 == 0){
            while(n > 0){
                total += 1/n;
                System.out.print("1"+"/"+n+" ");
                n -= 2;
            }
            System.out.println(total);
        }else if(n % 2 != 0){
            while(n > 0){
                total += 1/n;
                System.out.print("1"+"/"+n+" ");
                n -= 3;
            }
            System.out.println(total + 1);
        }else{
            System.out.print(n);
        }
    }
}

package package01;

import java.util.Scanner;

/**
 * Created by sakura on 16/6/1.
 */
/**
 *
 *  有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
 * */
public class Test7 {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int a[] = new int[100];
        for(int i = 0;i < n ;i++){
            a[i] = i + 1;
        }
        int i = 0;
        int count = 1;
        int newN = n;
        int j = 0;
        int time = 0;
        while(newN >= 3){
            i = 0;
            while(a[i] != '\0'){
                if(count ==3){
                    j = i;
                    while(a[j] != '\0'){
                        a[j] = a[j + 1];
                        j++;
                    }
                    newN--;
                }else
                    i++;
                if(count == 3)
                    count = 1;
                else
                    count++;
            }
        }
        int k = 0 ;
        while(a[k] != '\0'){
            System.out.print(a[k++]+" ");
        }
    }
}

package package01;

/**
 * Created by sakura on 16/6/7.
 */
/**
 *  题目:809*??=800*??+9*??+1
 *  
其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。求??代表的两位数,及809*??后的结果。
 *
 * */
public class Test8 {
    public static void main(String[] args){
        int i;
        for(i = 10 ; i <= 99 ; i ++){
            if(8 * i >= 10 && 8 * i <= 99)
                if(9 * i >= 100 && 9 * i <= 999)
                    if(809 * i == 800 * i + 9 * i + 1)  //这个题目无解  除非去掉算式最后的+1
                        break;
        }
        System.out.println(i);
    }
}

package package01;

/**
 * Created by sakura on 16/6/14.
 */
/**
 *  求0—7所能组成的奇数个数。
 *
 *      以下代码为 0-7所能组成的7位奇数
 *      未能完成题目,还需求6位奇数
 * */
public class Test9 {
    public static final int lim = 7;
    public static void main(String[] args){
        int i = 0;
        int count = 0;
        while(i <= lim){
            if(i == 0){

            }else if(i % 2 != 0){
                count += Fun(lim + 1 - 1) - Fun(lim + 1 - 2);    //题目为0 - 7 所以要 lim + 1
            }
            i ++;
        }
        System.out.println(count);
    }
    public static int Fun(int a){
        int i = 1;
        int t = 1;
        if(a == 1)
            return 1;
        while(i <= a){
            t = i * t;
            i++;
        }
        return t;
    }
}

package package01;

/**
 * Created by sakura on 16/5/28.
 */
/**
 *  求1+2!+3!+...+20!的和
 *
 *  注:0! = 1
 *
 *  未测试结果
 *  测试结果为268040729
 * */
public class TestJieCheng {
    public static void main(String[] atgs){
        int total = 0;
        for(int i = 1;i <= 20;i++){
            total += JieCheng(i);
        }
        System.out.print(total);
    }
    public static int JieCheng(int a){
        int i = 1;
        int total = 1;
        while(i <= a){
            total = total * i;
            i++;
        }
        return total;
    }
}

package package01;

/**
 * Created by sakura on 16/5/28.
 */
/**
 *
 *  求一个3*3矩阵对角线元素之和
 *
 * */
public class ThreeArray {
    public static void main(String[] args) {
        int[][] threeArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int total1 = 0;
        int total2 = 0;
        int i, j;
        for( i = 0,j = 0;i < 3 && j < 3;i++,j++) {
            total1 += threeArray[i][j];
        }
        for(i = 0 ,j = 2;i < 3 && j >= 0;i++,j--){
            total2 += threeArray[i][j];
        }
        System.out.println(total1+" "+total2);
    }

}

package package01;

/**
 * Created by sakura on 16/5/11.
 */
/**
 *
 * 题目:输入两个正整数m和n,求其最大公约数和最小公倍数
 *
 *
 *               答案正确
 *              求最大公约数使用google的辗除法,未理解,记住即可,涉及欧几里**,
 *
 * */
public class TwoNumberFunction {
    public static void main(String[] args){
        System.out.println(calculateMax(7,102));
        System.out.println(calculateMin(10,101));
    }
    public static int calculateMax(int a,int b){
        int time = -1;
        if(a>b){
            while(time != 0){               //利用辗除法(记住即可)
                time = a%b;
                a = b;
                b = time;
            }
            return a;
        }else{
            while(time != 0){
                time = b%a;
                b = a;
                a = time;
            }
            return b;
        }
    }
    public static int calculateMin(int a,int b){
        int time;
        if(a>b){
            time = a;
            while(time%a != 0 || time%b !=0){
                time++;
            }
            return time;
        }else{
            time = b;
            while(time%a != 0 || time%b != 0 ){
                time++;
            }
            return time;
        }
    }
}

package package01;

/**
 * Created by sakura on 16/5/14.
 */

/**
 * 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完数
 *
 * */
public class WanShu {
    public static void main(String [] args){
        int total = 0;
        int time = 1;
        int time1 = 0;
        int forTime;
        while(time <= 1000){
            for(forTime = 1;forTime < time;forTime++){      //不能forTime <= time ,不能加上 = ;
                if(time%forTime == 0){
                    total=total+forTime;
                }
            }

            if(time == total){
                System.out.println(time+" 是完数!");
            }
            time++;
            total = 0;
        }
    }
}

package package01;

/**
 * Created by sakura on 16/5/30.
 */
/**
 *                  
 *     1
           5
 *    1 1
          4
 *   1 2 1
         3
 *  1 3 3 1
        2
 * 1 4 6 4 1       1
 *1 5 10 10 5 1
    0
 * */
public class YnagHui {
    public static void main(String[] args){
        int row = 10;
        int timeRow = 1;
        int spaceCount = row - 1;
        int numberCount = 1;
        int[] number = new int[100];
        int[] newNumber = new int[100];
        newNumber[0] = 1;
        number[0] = 1; number[1] = 1;
        int m,n;
        while(timeRow <= row){
            for(int i = 0;i < spaceCount;i++)
                System.out.print(" ");
            spaceCount --;
            if(numberCount == 1)
                System.out.print("1 ");
            else if(numberCount == 2)
                System.out.print("1 1 ");
            else{
                System.out.print("1 ");
                m = 0;n = timeRow - 2;
                while((m + 1) <= n){
                    System.out.print(number[m] + number[m + 1]+" ");
                    newNumber[m + 1] =  number[m] + number[m + 1];      // 1
                    m++;

                }
                newNumber[n + 1] = 1;
                System.out.print("1 ");
                number = newNumber.clone();                     //难点:不能使用number = newNumber,这是按引用传递,在1处会出错
            }                                                   //(在使用1后不是单单对newNumber进行了操作,对number数组也进行了操作,)
                                                                //.clone为复制数组,number的操作不会影响newNumber数组
            numberCount ++;
            timeRow ++;
            System.out.print("\n");
        }
    }
}


  • 17
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值