模拟题
java没有指针 模拟链表
测试点有问题 正确代码拿不到全部分数
…自己定义的类内部不能写输入函数…
细节颇多
代码不规范,仅供参考~~
import java.util.Scanner;
import java.util.Arrays;
/*
7
1 2 3 4
6 4 3 3
4 3 7 4
3 2 5 2
5 9 2 2
4 2 9 2
4 2 9 8
本程序运行结果:8 4 2 9 4 3 7 4 6 4 3 3 1 2 3 4 4 2 9 2 3 2 5 2 5 9 2 2
测试点答案:4 3 7 4 6 4 3 3 1 2 3 4 4 2 9 2 3 2 5 2 5 9 2 2
测试点答案错误 测试点是去掉旋转函数之后就对了
*/
public class Main {
public static class NumberCard {
Scanner scan = new Scanner(System.in);
// 包含构造函数和方法 rotate 和 getLeft,getRight,分别表示顺时针旋转卡片,返回左侧数字,返回右侧数字
// 1 2 3 4 上下左右
private int num;// 旋转次数
private int[] a = new int[4];
public int id, nextid, pre;// 链表
public void scanf(int id, int[] t) {
for (int i = 0; i < 4; i++)
a[i] = t[i];// 不能在类里面写输入函数 不然一个一个敲,手动输入 不能复制。
num = 0;
this.id = id;
pre = -1;
nextid = -1;
}
public void rotate() {
num++;
}
public int getLeft() {
if (num % 4 == 1)
return a[2];
if (num % 4 == 2)
return a[1];
if (num % 4 == 3)
return a[0];
return a[3];
}
public int getRight() {
if (num % 4 == 1)
return a[0];
if (num % 4 == 2)
return a[3];
if (num % 4 == 3)
return a[2];
return a[1];
}
public int getUp() {
if (num % 4 == 1)
return a[3];
if (num % 4 == 2)
return a[2];
if (num % 4 == 3)
return a[1];
return a[0];
}
public int getDown() {
if (num % 4 == 1)
return a[1];
if (num % 4 == 2)
return a[0];
if (num % 4 == 3)
return a[3];
return a[2];
}
}
public static class CardGame {
/*
* 包含方法int getIndexForFit(NameCard),boolean insertCard(NameCard),print()
* getIndexForFit返回用最少的旋转数能将卡片插入到游戏板中的最靠前的位置,头部位置为0.
* insertCard将卡片通过最少的旋转插入到游戏板,按旋转完成的方向插入getIndexForFit返回的位置,返回插入成功与否。
* print函数按顺序输出每个卡片,每个卡片按照上右下左的顺序输出各个边上的数字。
*/
Scanner scan = new Scanner(System.in);
NumberCard[] mp = new NumberCard[105];
int Star = 0;// 链表的head
public CardGame() {
mp = new NumberCard[105];
for (int i=0;i<105; i++) {
mp[i] = new NumberCard();
}
}
int pree = -1;
public int getIndexForFit(NumberCard z) {
// System.out.print(idex);
// for (int i = 0; i < 4; i++) {
int idex = Star;
while (idex != -1) {
int temp = 0;
if (z.getRight() == mp[idex].getLeft())
temp++;
if (mp[idex].pre == -1 || z.getLeft() == mp[mp[idex].pre].getRight())
temp++;
if (temp == 2)
return idex;
pree = idex;
idex = mp[idex].nextid;
}
// 插在最后面
if (z.getLeft() == mp[pree].getRight())
return -2;
// z.rotate();
// }
return -1;
}
public boolean insertCard(NumberCard z, int idex) {
if (idex == -1)
return false;
if (idex == -2) {
mp[pree].nextid = z.id;
z.pre = pree;
return true;
}
z.pre = mp[idex].pre;
mp[idex].pre = z.id;
z.nextid = idex;
if (z.pre != -1)//下标不能为-1
mp[z.pre].nextid = z.id;
if (z.pre == -1) {// 更新头坐标
Star = z.id;
}
return true;
}
public void print() {
int t = Star;
while (mp[t].nextid != -1) {
System.out.printf("%d %d %d %d ", mp[t].getUp(), mp[t].getRight(), mp[t].getDown(), mp[t].getLeft());
t = mp[t].nextid;
}
System.out.printf("%d %d %d %d", mp[t].getUp(), mp[t].getRight(), mp[t].getDown(), mp[t].getLeft());
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int sum = 0;
int[] a = new int[4];
sum = scan.nextInt();
CardGame game = new CardGame();
for (int i = 0; i < sum; i++) {
for (int j = 0; j < 4; j++) {
a[j] = scan.nextInt();
}
NumberCard c = new NumberCard();
c.scanf(i, a);
game.mp[i] = c;
game.insertCard(game.mp[i], game.getIndexForFit(game.mp[i]));
}
game.print();
}
}