2017-03-13-考试
Xplorist Lv6

2017-03-13-考试

实例1:

有1、2、3三个数字,能组成多少个互不相同且无重复数字的三位数?都是多少

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Test01 {
    public static void main(String[] args) {
        
        new Test01().test1();
        new Test01().test(1,5,7);
        
        
    }
    
    public int test1(){
        int count=0;
        int f=1,s=2,t=3;
        
        for(int i=1;i<4;i++){
            for(int j=1;j<4;j++){
                for(int n=1;n<4;n++){
                    if(i!=j&&i!=n&&j!=n){
                        count++;
                        System.out.println("第"+count+"个数:"+i+""+j+""+n);
                    }
                }
            }
        }
        System.out.println("1,2,3三个数能组成 "+count+" 个互不相同且无重复数字的三位数");
        return count;
    }
    
    public int test(int f,int s,int t){
        int count=0;
        int[] a={f,s,t};
        
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a.length;j++){
                for(int k=0;k<a.length;k++){
                    if(a[i]!=a[j]&&a[i]!=a[k]&&a[j]!=a[k]){
                        count++;
                        System.out.println("第"+count+"个数:"+a[i]+""+a[j]+""+a[k]);
                    }
                }
            }
        }
        
        System.out.println(f+""+s+""+t+"三个数能组成"+count+"个互不相同且无重复数字的三位数");
        return count;
    }
    
}

实例2:

某次选秀活动找你做一个打分系统,设定一共有5位评委,他们分别对选手诶打分,需要程序统计选手得分。要求如下:5位评委打分后,输出选手得分
 * 【得分规则:去掉最高分和最低分,剩下3个分数的平均分就是选手的得分】

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class Test02 {
    public static void main(String[] args) {
        new Test02().test();
    }
    
    public void test(){
        Scanner sc=new Scanner(System.in);
        
        System.out.println("请输入第一个评委的打分:");
        int first=sc.nextInt();
    
        System.out.println("请输入第二个评委的打分:");
        int second=sc.nextInt();
        System.out.println("请输入第三个评委的打分:");
        int third=sc.nextInt();
        System.out.println("请输入第四个评委的打分:");
        int fourth=sc.nextInt();
        System.out.println("请输入第五个评委的打分:");
        int fifth=sc.nextInt();
        
        int[] a={first,second,third,fourth,fifth};
        
        for(int i=0;i<a.length-1;i++){
            for(int j=i+1;j<a.length-1;j++){
                if(a[i]>a[j]){
                    a[i]=a[i]+a[j];
                    a[j]=a[i]-a[j];
                    a[i]=a[i]-a[j];
                }
            }
        }
        
        int average=(a[1]+a[2]+a[3])/3;
        
        System.out.println("去掉最高分和最低分,剩下3个分数的平均分就是选手的得分:"+average);
        
    }
}

实例3:

编程创建一个student类,在其中包括属性(学号,年级,姓名,性别,年龄)并创建对象s。添加构造方法初始化所有的属性,
 * 并增加一个show()方法,把该类中那个的所有属性信息组合成一个字符串,并输出显示。
 * 创建两个student类的对象,比较二者年龄,输出其中年龄较大的学生的姓名。

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Student {
    public int number;//学号
    public int degree;//年级
    public String name;//姓名
    public char sex;//性别
    public int age;//年龄
    
    public Student(int number,int degree,String name,char sex,int age){
        this.number=number;
        this.degree=degree;
        this.name=name;
        this.sex=sex;
        this.age=age;
    }
    
    public void show(){
        String message="学号:"+number+" 年级:"+degree+" 姓名:"+name+" 性别:"+sex+" 年龄:"+age;
        System.out.println(message);
    }
    
    public static String compare(Student s1,Student s2){
        String str="";
        
        if(s1.age>s2.age){
            str=s1.name;
        }else{
            str=s2.name;
        }
        
        return str;
                
    }
    
    public static void main(String[] args) {
        Student s=new Student(1001, 5, "张三", '男', 12);
        s.show();
        
        Student s1=new Student(1002, 6, "李四", '男',13);
        s1.show();
        Student s2=new Student(1003, 7, "王五", '女',14);
        s2.show();
        
        String str=Student.compare(s1, s2);
        System.out.println("年龄较大的学生是:"+str);
    }
}

思考:

如果实例1的三个数改成不确定的n个数,怎么算出排列的个数?数学中的排列组合问题。

n个数中选出n个数来进行排列,公式是:n!种排列。那么就可以直接套公式。

阶乘要用到递归。

思考:

如何生成一个随机的数组,数组中的数字是0~9之间的,但是顺序是随机的。

思路:

1.生成数组的时候产生随机数,然后再筛选,如果前面的数和后面的某个数相同的时候,就把后面的某个数再随机一次直到和前面的数不相同为止。

2.顺序生成数组0~9,然后再将顺序打乱。

1核心代码(有错):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**生成0~9的数组,然后将数组里的数的顺序打乱,随机打乱*/
    public void test4(){
        int[] a=new int[10];
        for(int i=0;i<10;i++){
            a[i]=i;
        }
        
        for(int i=0;i<a.length-1;i++){
            int x=(int)(Math.random()*10);
            System.out.println(x);
            
            int t=a[i];
            a[x]=a[i];
            a[i]=x;
        }
        
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
        
        
    }

2核心代码(有错):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
     * 生成一个随机数组,然后判断里面的每个数和后面的数是否重复,如果重复则将后面的数再随机一遍,直到随机到不相同为止
     * */
    public void test7(){
        int[] a=new int[10];
        for(int i=0;i<a.length;i++){
            a[i]=(int)(Math.random()*10);
        }
        
        System.out.println(Arrays.toString(a));
        
        for(int i=0;i<a.length-1;i++){
            for(int j=i+1;j<a.length;j++){
                if(a[i]==a[j]){
                    while(a[i]==a[j]){
                        a[j]=(int)(Math.random()*10);
                    }
                }
            }
        }
        System.out.println(Arrays.toString(a));
    }

实例4:

/**
     * 晚自习练习题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同
     * */

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void test5(int n){
        if(n>99){
        String s=Integer.toString(n);
        char[] c=new char[s.length()];
        for(int i=0;i<s.length();i++){
            c[i]=s.charAt(i);
        }
        
        for(int i=0,j=s.length()-1;i<=j;i++,j--){
                if(c[i]!=c[j]){
                    System.out.println(s+"不是回文数");
                    return;
                }
        }
        System.out.println(s+"是回文数");
        }else{
            System.out.println(n+"不是回文数");
        }
    }

实例5:

/**
     * 将一个正整数分解质因数。例如:输入90,打印出90=233*5。 
     * */

核心代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void test6(int n){
        System.out.print(n+"=");
        for(int i=2;i<n;i++){
            
            if(n%i==0){
                System.out.print(i+"*");
                n=n/i;
                i-=1;
            }
            
        }
        System.out.println(n);
    }

 评论