【華為】2016研發
阿新 • • 發佈:2018-11-11
[程式設計題]刪數
有一個數組a[N]順序存放0~N-1,要求每隔兩個數刪掉一個數,到末尾時迴圈至開頭繼續進行,求最後一個被刪掉的數的原始下標位置。以8個數(N=7)為例:{0,1,2,3,4,5,6,7},0->1->2(刪除)->3->4->5(刪除)->6->7->0(刪除),如此迴圈直到最後一個數被刪除。
輸入描述:
每組資料為一行一個整數n(小於等於1000),為陣列成員數,如果大於1000,則對a[999]進行計算。
輸出描述:
一行輸出最後一個被刪掉的數的原始下標位置。
示例1
輸入
8
輸出
6
組成一個環形的連結串列,必會的題!!加油
import java.util.*; class node { int value; node next; public node() { } public node(int value, node next) { this.value = value; this.next = next; } } public class Main { public static void main(String[] args) { first(); } public static void first() { Scanner in = new Scanner(System.in); while (in.hasNext()) { int num = in.nextInt(); node root = new node(); root.value = 0; // 記錄頭的位置 node rootNode = root; for (int i = 1; i < num; i++) { node node = new node(); node.value = i; root.next = node; root = node; } root.next = rootNode; while (rootNode != rootNode.next) { root=rootNode.next.next; rootNode.next.next=root.next; rootNode=root.next; } System.out.println(rootNode.value); } } }
[程式設計題]字元集合
輸入一個字串,求出該字串包含的字元集合
輸入描述:
每組資料輸入一個字串,字串最大長度為100,且只包含字母,不可能為空串,區分大小寫。
輸出描述:
每組資料一行,按字串原有的字元順序,輸出字元集合,即重複出現並靠後的字母不輸出。
示例1
輸入
abcqweracb
輸出
abcqwer
沒什麼說的,用set集合去重
import java.util.*; public class Main { public static void main(String[] args) { delete(); } public static void delete() { Scanner in = new Scanner(System.in); while (in.hasNext()) { char[] c = in.next().toCharArray(); StringBuffer sb = new StringBuffer(); Set<Character> set = new HashSet<Character>(); for (int i = 0; i < c.length; i++) { if (set.add(c[i])) sb.append(c[i]); } System.out.println(sb.toString()); } } }
最後一題數獨,不太想研究了
數獨是一個我們都非常熟悉的經典遊戲,運用計算機我們可以很快地解開數獨難題,現在有一些簡單的數獨題目,請編寫一個程式求解。
輸入描述:
輸入9行,每行為空格隔開的9個數字,為0的地方就是需要填充的。
輸出描述:
輸出九行,每行九個空格隔開的數字,為解出的答案。