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