第一天测试题目_6.25

1. Java/C++)打印9*9乘法表(要求时间复杂度为O(n)java

for(int i = 1, j = 1; i <= 9; j++){
            System.out.print(j + "*" + i + "=" + (i * j) + "\t");
            if(j == i){
                System.out.println();
                i++;
                j = 0; 
            }
        }

考点:用单循环解决多循环问题,时间复杂度
设计模式

2. Java/C++)求100~200之间的所有素数(除1和本身自己以外,没有其余约数)数组

for(int i = 101; i <= 199; i+=2)//偶数不会是素数
        {
            int j = 0;
            for(j = 2; j <= i/2; j++)//小于其1/2,则继续向后查找
            {
                if(i % j == 0)
                    break;
            }

            if(j > i/2)//全部的查找完可是没有跳出,说明没有约数即为素数
            {
                System.out.print(i + "  ");
            }
        }
        System.out.println();

考点:for循环运行结构
for(初始化表达式; 判断表达式; 修正表达式)
{
}
进入for的时候作初始化表达式,作完以后判断条件是否成立,若是不成立,则直接跳过整个循环语句块;若是成立,则进入循环体,循环体执行完毕以后,则作修正表达式,再去判断条件是否成立数据结构

3. Java/C++)将十进制的整数转换为32位的二进制补码形式输出(要求输入的整数包含正负状况)ide

public class Ten_to_32 {
    public static void ten_to_32binary(int iNum){
        int iCount = 0;
        System.out.printf("%d", iNum > 0 ? 0 : 1);
        iCount++;
        for(int i = 30; i >= 0; i--)
        {
            iCount ++;
            System.out.printf("%d", (iNum & (1 << i)) > 0 ? 1 : 0);
            if(iCount % 8 == 0)
                System.out.printf(" ");
        }
        System.out.printf("\n");
    }
    
    public static void main(String[] args) {
        System.out.print("请输入一个整数:");
        int iNum = new Scanner(System.in).nextInt();

        ten_to_32binary(iNum);
    }
}

考点:
函数

计算机中数据都是以补码形式存储的,最高位表示符号位,0表示正,1表示负
正数的原码、反码、补码都是本身自己。
负数的原码、反码、补码该怎么求呢?
-10
原码:10000000 00000000 00000000 00001010
反码:11111111 11111111 11111111 11110101
补码:11111111 11111111 11111111 11110110
反码:原码的符号位不变,其他位按位取反
补码:反码+1(原码符号位不变,其他位按位取反后+1)
//原码符号位不变以及最后一个1和其后面的0不变,其他位按位取反
10000000 00000000 00000000 00001010
11111111 11111111 11111111 11110110

为何须要补码呢?
由于计算机只能进行加法运行(累加器)
-1:
10000000 00000001
2-1=2+(-1)
  00000000 00000010   --> short
+ 11111111 11111111
-------------------
1 00000000 00000001
00000000 00000001-->1

1-2=1+(-2)
  00000000 00000001
+ 11111111 11111110
-------------------
  11111111 11111111
11111111 11111111是一个补码
二进制转10进制的时候,会将补码转为原码:
10000000 00000001 原码---》 -1


位操做:
& : 按位与
1&1=1  1&0=0  0&1=0  0&0=0
公式:1 & a = a      0 & a = 0
做用:&能够用来检测位,取位,清除位(会跟~结合使用)
&=~ 表示位清除

1100110011010011100
0000000011100000000 &
-------------------
0000000011000000000

| : 按位或
1|1=1 1|0=1 0|1=1 0|0=0
公式:1|a=1    0|a=a
做用:设置位
1100110011010011100
0000000000001111000 |
-------------------
1100110011011111100

~ : 按位取反
^ : 按位异或
<<: 左移
>>: 右移
>>>: 不带符号的右移(java)

-10
10000000 00000000 00000000 00001010
11111111 11111111 11111111 11110110
this

4. Java/C++)假设有10个数已经按照从小到大的顺序存放在数组中,要求向从键盘输入一个整数,插入这10个数中,使数组还是从小到大的顺序排列。spa

public class Insert {
    public static void main(String[] args) {
        int[] a = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
        int[] b = new int[11];
        System.arraycopy(a, 0, b, 0, a.length);
        System.out.println(b.length);
        System.out.printf("原数组的内容以下:\n");
        for(int i = 0; i < b.length - 1; i++)
        {
            System.out.printf("%d  ", b[i]);
        }
        System.out.printf("\n");

        System.out.printf("请输入你要插入的元素:");
        int iNum = new Scanner(System.in).nextInt();

        int i = 0;
        for(i = b.length-2; i >= 0; i--)
        {
            if(iNum > b[i])
            {
                b[i+1] = iNum;
                break;
            }
            else
            {
                b[i+1] = b[i];
            }
        }

        if(i < 0)
            b[0] = iNum;

        System.out.printf("改变以后数组的内容以下:\n");
        for(i = 0; i < b.length; i++)
        {
            System.out.printf("%d  ", b[i]);
        }
        System.out.printf("\n");
    }
}

考点:数据结构中的插入操做,Java控制台读取数据(new  scanner(system.in).nextInt())设计

5. Java/C++)单态设计模式code

class SingleTon
{
    private int a = 0;
    private static SingleTon ps = null;
    private SingleTon(int a){
        this.a = a;
    }
    
    public static SingleTon getInstance(int a){
        if(ps == null){
            ps = new SingleTon(a);
        }
        return ps;
    }
    
    public void disp(){
        System.out.println(this.a);
    }
}

public class Test {

    public static void main(String[] args) {
        SingleTon p = SingleTon.getInstance(10);
        p.disp();
        
        SingleTon p2 = SingleTon.getInstance(20);
        p2.disp();
    }
}

考点:设计模式,单例

单件、单例、单态设计模式:游戏
对于构造函数私有的该怎么实例化对象
1. 将构造函数私有
2. 定义本身类自己的静态对象并初始化为null、NULL
3. 定义一个返回类类型的静态方法getInstance(特别注意要判断其为空就再也不建立对象)
须要注意的是,在getInstance里面,要控制其实例化的对象个数。

6. Java/C++)编写学生信息管理系统的登陆界面,要求以下:

public class Test {

    public static void main(String[] args) {
        Console con = System.console();
        
        System.out.println("\t\t********学生信息管理系统********\n\n");
        
        System.out.print("\t\t\t用户名:");
        String name = con.readLine();
        
        System.out.print("\t\t\t密    码:");
        char[] sz = con.readPassword();
        String passwd = new String(sz);
        
        
        System.out.println("用户名为:" + name);
        System.out.println("密码为:" + passwd);
    }

}

考点:读取数据的方式

        a.  System.in.read();只能针对一个字符的获取,同时,获取进来的变量的类型只能是char

        b.用到BufferedReader类和InputStreamReader类(jdk1.5以前比较经常使用词方法)

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        c.最简单,最强大的,就是用Scanner类(如今比较经常使用并且功能强大)

            Scanner sc = new Scanner(System.in);

        d.Console比较特别的就是本身特有的读取密码的方法          

Console con = System.console();

7. Java/C++)定义一个学生类,里面有姓名、年龄、成绩三个属性,要求按成绩由高到低排序,若是成绩相等,则按照年龄由低到高排序。(请使用集合类)

class Student implements Comparable
{
    private String name;
    private int age;
    private int score;
    public Student(){
        
    }
    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "姓名:" + this.name + ", 年龄:" + this.age + ", 成绩:" + this.score;
    }
    @Override
    public int compareTo(Object o) {
        Student stu;
        if(o instanceof Student)
        {
            stu = (Student)o;
            if(this.score == stu.score)
                return this.age - stu.age;
            return stu.score - this.score;
        }
        return 0;
    }
}

public class Test {

    public static void main(String[] args) {
        Set<Student> set = new TreeSet<Student>();
        
        set.add(new Student("张三", 24, 90));
        set.add(new Student("李四", 25, 87));
        set.add(new Student("钱七", 21, 79));
        set.add(new Student("王五", 23, 100));
        set.add(new Student("赵六", 22, 79));
        
        Iterator<Student> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}

实现了comparable接口,插入集合之中时就能够直接插入(TreeSet是有序的,插入时必须比较,所以插入的数据要可以比较),或者传递一个比较器给Set,使其在插入的时候能够比较

考点:Collection--->List-----Set(SortedSet)ArrayList  LinkedList  HashSet  TreeSetMap

相关文章
相关标签/搜索