有趣的兩位數
有數學家發現⼀些兩位數很有意思,⽐如, 34 * 86 = 43 * 68 也就是說,如果把他們的十位數和個位數交換,二者乘積不變。 編程求出滿足該性質的兩位數組合。 提示,暴力解法非最優解。
解題思路
先給出暴力求解的答案。考慮到11X99=99X11,這種情況不屬於解題集。另外,12X24=21X42和21X42=12X24這種情況看作一個解。
代碼邏輯實現
import java.util.HashSet;
import java.util.Set;
public class swap {
public static void main(String[] args) {
fun();
}
public static void fun() {
Set<String> set=new HashSet<>();
int sum = 0;
for (int i = 10; i <= 99; i++) {
for (int j = i + 1; j <= 99; j++) {
//排除兩個數相等的情況,排除交換后還是自身的情況如12X21=21X12
//排除十位和個位相等的情況,如11X22=11X22
//排除重復的結果,如12X42=21X24和21X24=12X42
if (i != j && Math.abs(j - i) % 9!=0 && i % 11 != 0 && j % 11 != 0 && i * j == trans(i) * trans(j)&&!set.contains(i+""+j)) {
sum++;
set.add(i+""+j);
set.add(trans(i)+""+trans(j));
System.out.println(i + "X" + j + "=" + trans(i) + "X" + trans(j));
}
}
}
System.out.println("總數為:" + sum);
}
public static int trans(int cur) {
int units = cur % 10;
int tens = cur / 10;
return units * 10 + tens;
}
}
/**
12X42=21X24
12X63=21X36
13X62=31X26
13X93=31X39
14X82=41X28
23X64=32X46
23X96=32X69
24X63=42X36
24X84=42X48
26X31=62X13
26X93=62X39
28X41=82X14
34X86=43X68
36X42=63X24
36X84=63X48
39X62=93X26
46X96=64X69
48X63=84X36
總數為:18
* /
單鏈表處理
假設線性表 L = {A1, A2, A3, A4, …, An-2, An-1, An},采⽤帶頭節點的單鏈表保存。鏈接節點定義如 下:
typedef struct node {
int data;
struct node * next;
} NODE;
請設計⼀個算法,編程實現,重新排列 L 中的各節點,得到線性表 L’ = {A1, An, A2, An-1, A3, An2, … }。
解題思路
如果把這個問題的單鏈表換成數組,那問題就很簡單了。考慮到單鏈表是單向的,因此我采用一個集合保存所有的單鏈表節點。然后讓集合的前半部分元素和后半部分元素依次連接。
代碼邏輯實現
import java.util.ArrayList;
import java.util.List;
public class SingleLinkedList {
static class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
public static void main(String[] args) {
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
Node node6 = new Node(6);
Node node7 = new Node(7);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node7;
print(node1);
arrange(node1);
print(node1);
}
/**
* 從head遍歷打印出整個單鏈表
* @param head 頭結點
*/
public static void print(Node head) {
Node cur = head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
/**
* 重新排列單鏈表
* @param head 頭結點
*/
public static Node arrange(Node head) {
List<Node> nodeList = new ArrayList<>();
//存儲所有節點
while (head != null) {
nodeList.add(head);
head = head.next;
}
Node cur = null;
//因為本質上類似交換數組首尾元素,因此只需要遍歷1/2的數據就可以
for (int i = 0; i < nodeList.size() / 2; i++) {
//如果是第一個元素則當前節點為i所在節點
//如果不是第一個元素,當前節點此時為上一次操作的最后一個元素
//此時需要把當前節點指向i所在節點
if (cur == null) {
cur = nodeList.get(i);
} else {
cur.next = nodeList.get(i);
cur = nodeList.get(i);
}
//獲取列表另一半對應的結點
Node last = nodeList.get(nodeList.size() - i - 1);
if (last != null) {
cur.next = last;
//每次把last的指向設置為空
last.next = null;
}
//將操作的后一半節點設置為當前節點
cur = last;
//如果單鏈表的個數為奇數,則在最后一次遍歷的時候把最后一個節點指向中間節點
if (i == nodeList.size() / 2 - 1 && nodeList.size() % 2 != 0 && cur != null) {
Node middle = nodeList.get(nodeList.size() / 2);
cur.next = middle;
middle.next = null;
}
}
return nodeList.get(0);
}
}
/**
* 原鏈表:1 2 3 4 5 6 7
* 重排后:1 7 2 6 3 5 4
*/