Ⅰ 用C語言編程問題
比較次的一個代碼……不一定能解出所有可解的情形。
/*
*先用02062311-snowonion條件進行判斷。
*所謂02062311-snowonion條件就是指:
*"能寫成九宮格的充分條件是,九個數可以表示成如下形式:
*A-d,A,A+d,B-d,B,B+d,C-d,C,C+d"。
*通過判斷的按某一種構造法構造成九宮格,通不過判斷則提示錯誤。
*/
#include<stdio.h>
int is_arithmetical(int*a,int n)
{
int d,i;
if(n>=3)
{
d=a[1]-a[0];
for(i=1; i<n-1; i++)
{
if(a[2]-a[1]!=d)
{
return 0;
}
}
return 1;
}
else return 1;
}
int is_0s(int* a)
{
int b[3]= {a[1],a[4],a[7]};
if(is_arithmetical(a,3)&&is_arithmetical(a+3,3)&&is_arithmetical(a+6,3)&&is_arithmetical(b,3))
{
return 1;
}
else return 0;
}
void sort(int* a)
{
int i,j,t;
for(i=1; i<9; i++)
{
for(j=0; j<9-i; j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void printnine(int* a) //這里用的是一種構造方法……當然把他對稱或者旋轉也是對的……
{
printf("-------------------------\n");
printf("%d\t%d\t%d\n%d\t%d\t%d\n%d\t%d\t%d\n",a[5],a[0],a[7],a[6],a[4],a[2],a[1],a[8],a[3]);
printf("-------------------------\n");
}
int main()
{
int a[9];
int i;
while(1)
{
printf("請輸入九個整數,以空格或回車隔開:\n");
for(i=0; i<9; i++)
{
scanf("%d",&a[i]);
}
sort(a);
if(is_0s(a))
{
printnine(a);
}
else
{
printf("您輸入的九個數沒有通過02062311-snowonion條件的檢測,可能寫不成九宮格╮(╯▽╰)╭\n");
}
}
return 0;
}
Ⅱ 用java設計一個華容道游戲
import java.awt.*;
import java.awt.event.*;
public class MoveExample //主類
{
public static void main(String args[]) //定義主方法
{
new Hua_Rong_Road(); //創建對象
}
}
class Person extends Button implements FocusListener
{
int number;
Color c = new Color(128,128,128);
Person(int number,String s)//構造方法
{
super(s);//調用父類s的構造方法
setBackground(c);//設置組件的背景色
this.number = number;//調用當前的number
c = getBackground();
addFocusListener(this);//添加焦點事件監聽器
}
public void focusGained(FocusEvent e)//焦點事件觸發
{
setBackground(Color.blue);
}
public void focusLost(FocusEvent e)//焦點事件失去
{
setBackground(c);
}
}
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener
{
Person person[] = new Person[10];//person類的數組
Button left,right,above,below;
Button restart = new Button("重新開始");
public Hua_Rong_Road() //華容道的構造方法
{
init(); //初始化
setBounds(100,100,320,360);//設置窗口在屏幕上出現位置,和窗口大小
setVisible(true);//設置窗口可見
setResizable(true);//設置窗口可調節
validate();//刷新
addWindowListener( new WindowAdapter()//獲得窗口事件監視器
{
public void windowClosing(WindowEvent e)//窗口正在被關閉時,窗口監視器調用該方法
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);//設置默認布局
add(restart);//添加重新開始
restart.setBounds(100,320,120,25);//重新開始按鈕大小
restart.addActionListener(this);//事件源獲得監視器
String name[] = {"曹操","關羽","張飛","劉備","趙雲","黃忠","兵","兵","兵","兵"};
for(int k = 0;k<name.length;k++)
{
person[k] = new Person(k,name[k]);//給按鈕添加名字
person[k].addMouseListener(this);//每個按鈕都注冊滑鼠事件
person[k].addKeyListener(this);//每個按鈕都注冊鍵盤事件
add(person[k]);//添加人物
}
person[0].setBounds(104,54,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
person[3].setBounds(204,154,50,100);
person[4].setBounds(54,54,50,100);
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);//為每個人物按鈕設置位置和大小
person[9].requestFocus();//把焦點先設置在這個按鈕上
left = new Button();//畫出遊戲界面邊框,並用定義的left,right,above,below控制大小
right = new Button();
above = new Button();
below = new Button();
add(left);
add(right);
add(above);
add(below);
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate();//刷新
} //完成界面布局
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)//響應鍵盤事件,按鍵,釋放鍵,按下和釋放組合
{
Person man = (Person)e.getSource();//獲得事件源
if(e.getKeyCode()==KeyEvent.VK_DOWN)//響應用戶按下方向游標的操作;用KeyEvent類中的getkeycode()判斷哪個鍵被按下
{
go(man,below); //go方法控制移動
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
go(man,above);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
go(man,left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
go(man,right);
}
}
public void mousePressed(MouseEvent e)
{
Person man = (Person)e.getSource();
int x = -1,y = -1;
x = e.getX();
y = e.getY();
int w = man.getBounds().width;
int h = man.getBounds().height;
if(y>h/2)
{
go(man,below);
}
if(y<h/2)
{
go(man,above);
}
if(x<w/2)
{
go(man,left);
}
if(x>w/2)
{
go(man,right);
}
}
public void mouseReleased(MouseEvent e){}//滑鼠事件
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void go(Person man,Button direction)
{
boolean move = true;
Rectangle manRect = man.getBounds();
int x = man.getBounds().x;
int y = man.getBounds().y;
if(direction==below)//向各個方向移動
{
y = y+50;
}
else if(direction==above)
{
y = y-50;
}
else if(direction==left)
{
x = x-50;
}
else if(direction==right)
{
x = x+50;
}
manRect.setLocation(x,y);
Rectangle directionRect = direction.getBounds();
for(int k = 0;k<10;k++)
{
Rectangle personRect = person[k].getBounds();
if((manRect.intersects(personRect))&&(man.number!=k))//如果覆蓋就不移動
{
move = false;
}
}
if(manRect.intersects(directionRect))
{
move = false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}