1. 輸出1+1 = ? 輸入2,則輸出正確,輸入其他則顯示錯誤
創(chuàng)新互聯(lián)一直通過網(wǎng)站建設(shè)和網(wǎng)站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實(shí)效"的一站式服務(wù),以成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、移動互聯(lián)產(chǎn)品、網(wǎng)絡(luò)營銷推廣服務(wù)為核心業(yè)務(wù)。十載網(wǎng)站制作的經(jīng)驗,使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價格便宜而且實(shí)用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡單易用,維護(hù)方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。
2. 24點(diǎn)游戲,輸出4個數(shù),輸入一行表達(dá)式,程序內(nèi)部計算表達(dá)式的值(比較費(fèi)勁),若=24則返回勝利,否則失敗
3. 五子棋(無AI,否則難),黑白方輪流輸入坐標(biāo),每次輸入完坐標(biāo)都重新輸出一次棋盤上所有棋子,具體方式就是把存儲棋子的矩陣挨個輸出,無棋輸出空格,黑輸出*,白輸出o,循環(huán)直到判斷游戲是否結(jié)束的函數(shù)返回值為真,即有5子相連才結(jié)束。
4. 貪食蛇。這個需要查閱有關(guān)**的資料:線程(或計時器),響應(yīng)鍵盤按鍵,隨機(jī)數(shù)等等。蛇每隔單位時間向當(dāng)前方向前進(jìn)一步,然后刷新(也就是把畫面重新輸出一遍),在隨機(jī)位置產(chǎn)生食物,蛇的尾巴經(jīng)過食物(即已經(jīng)消化)后加長一段。按方向鍵時改變記錄當(dāng)前方向變量的值。加入判斷死亡的機(jī)制。
這個用控制臺(就是和DOS似的,只有press any key to continue之類文字的界面)也可以實(shí)現(xiàn),但是相關(guān)資料較少很難找,選擇圖形編程(windows下常用的軟件都是這樣,有窗口的)資料更多一些,如果你是在windows上編,就查C語言windows api,剛開始編時不會很方便。由易到難地嘗試吧,不必急于用圖形編程,控制臺也是可以編出好游戲的,這是基礎(chǔ)。
#includeiostream.h
#includemath.h
/*(1) 螺旋方陣,如下圖:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
*/
void op1(int n)
{
int i = 0, //i表示方陣的行數(shù)
j = 0; //j表示方陣的列數(shù)
int *p = new int[n*n];//p表示方陣數(shù)組
int temp = 1;
int t;
for (t = 0; t ((float)n / 2); t++) //t表示圈數(shù)
{
for (; j n - t; j++) //行數(shù)不變,方陣的值隨著列數(shù)的增加而增加(1,2,3,4,5)(17,18,19)(25)
p[i*n+j] = temp++;
j--;
i++;
for (; i n - t; i++) //列數(shù)不變,方陣的值隨著行數(shù)的增加而增加(6,7,8,9)(20,21)
p[i*n+j] = temp++;
j--;
i--;
for (; j = t; j--) //行數(shù)不變,方陣的值隨著列數(shù)的減少而減少(10,11,12,13)(22,23)
p[i*n+j] = temp++;
j++;
i--;
for (; i t; i--) //列數(shù)不變,方陣的值隨著行數(shù)的減少而減少(14,15,16)(24)
p[i*n+j] = temp++;
j++;
i++;
}
for (i = 0; i n; i++)
{
for (j = 0; j n; j++)
{
coutp[i*n+j]" ";//show.Text += p[i][j] + " ";
}
coutendl;
// show.Text += "\r";
}
}
/*
2) 蛇形方陣,如下圖:
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16
*/
void op2(int n)
{
int i, j, k; //i表示行數(shù),j表示列數(shù),k表示行數(shù)和列數(shù)的和
int m = 2 * n - 2; //m表示方陣行數(shù)最大和列數(shù)最大的和
int temp = 1; //temp表示遞增變量
int *array = new int[n*n]; //array表示矩陣數(shù)組
for (k = 0; k = m; k++) //根據(jù)矩陣的對稱性求解;(array[2,0],array[1,1],array[0,2]的行數(shù)和列數(shù)的和相等)
{
int l = k % 2; //根據(jù)k值的遞增,改變行數(shù)和列數(shù)遞增的方向;當(dāng)k為奇數(shù)先行后列,當(dāng)k為偶數(shù)先列后行
for (i = 0; i n; i++)
{
j = k - i;
if (j n j = 0) //判斷j的值范圍,以免超過數(shù)組長度
{
if (l == 1)
{
array[i*n+j] = temp;
}
if (l == 0)
{
array[j*n+i] = temp;
}
temp++;
}
}
}
for (i = 0; i n; i++)
{
for (j = 0; j n; j++)
{
/* show.Text += array[i*n+j];
show.Text += " "; */
coutarray[i*n+j]" ";
}
// show.Text += "\n";
coutendl;
}
}
int main()
{
int n1,n2;
while(cinn1n2)
{
op1(n1);
cout"***************"endl;
op2(n2);
}
return 0;
}
不是我做的,不過你可以也看一下!!!!!!!
你如果用TC
#includegraphics.h
#includestdlib.h
#define N 200
#define up 0x4800
#define down 0x5000
#define left 0x4b00
#define right 0x4d00
#define esc 0x011b
#define Y 0x1579
#define n 0x316e
int gamespeed; /* 游戲速度 */
int i,key,color;
int score=0; /* 游戲分?jǐn)?shù) */
char cai48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x04,0x00,0x18,0x00,0x00,0x00,0x0E,0x00,
0x1C,0x00,0x00,0x00,0x1C,0x00,0x1C,0x00,
0x00,0x00,0x20,0x00,0x38,0x00,0x00,0x00,
0x40,0x00,0x78,0x00,0x00,0x01,0x80,0x40,
0x70,0x00,0x00,0x03,0x80,0xC0,0xE0,0x00,
0x00,0x07,0x80,0x80,0xC0,0x00,0x00,0x0E,
0x11,0x81,0xC0,0x00,0x00,0x08,0x61,0x01,
0x80,0x00,0x00,0x00,0x23,0x03,0x04,0x00,
0x00,0x02,0x02,0x00,0x06,0x00,0x00,0x1E,
0x04,0x00,0x0F,0x00,0x00,0x1C,0x1F,0x80,
0x1E,0x00,0x00,0x08,0x3F,0x80,0x3C,0x00,
0x00,0x00,0xFF,0x80,0x38,0x00,0x00,0x03,
0xFF,0x80,0x78,0x00,0x00,0x0F,0xF8,0x00,
0xF0,0x00,0x00,0x7F,0xF0,0x00,0xE0,0x00,
0x03,0xFF,0xFC,0x01,0x80,0x00,0x03,0xC0,
0xFF,0x01,0x03,0x80,0x01,0x01,0xFF,0x00,
0x03,0x80,0x00,0x01,0x3F,0x00,0x07,0x80,
0x00,0x02,0x11,0x00,0x07,0x00,0x00,0x00,
0x10,0x00,0x07,0x00,0x00,0x00,0x10,0x00,
0x0E,0x00,0x00,0x08,0x10,0x00,0x1C,0x00,
0x00,0x30,0x10,0x00,0x18,0x00,0x00,0x70,
0x10,0x00,0x30,0x00,0x01,0xE0,0x10,0x00,
0x70,0x00,0x03,0x80,0x10,0x00,0x60,0x00,
0x00,0x00,0x30,0x00,0xE0,0x00,0x00,0x00,
0xF0,0x01,0xC0,0x00,0x00,0x00,0x70,0x03,
0xC0,0x00,0x00,0x00,0x10,0x07,0x80,0x00,
0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,
0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x3C,
0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,
0x00,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char she48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,
0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,
0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x00,
0x00,0x0E,0x00,0x00,0x00,0x03,0x00,0x07,
0x00,0x00,0x00,0x02,0x00,0x03,0x00,0x00,
0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x02,
0x00,0x00,0xF8,0x00,0x00,0x02,0x00,0x07,
0x86,0x00,0x00,0x02,0x00,0x18,0x03,0x00,
0x00,0x02,0x00,0x00,0x07,0x80,0x00,0x03,
0xF0,0x00,0x07,0x80,0x00,0x0F,0xFC,0x00,
0x0C,0x00,0x00,0x7E,0x3F,0x80,0x00,0x00,
0x01,0xFE,0x1F,0x80,0x00,0x00,0x01,0xE2,
0x39,0x8C,0x00,0x00,0x00,0xC2,0x30,0x08,
0x00,0x00,0x00,0xC2,0x60,0x08,0x00,0x00,
0x00,0xC3,0xE0,0x08,0x60,0x00,0x00,0x7F,
0xE0,0x01,0xE0,0x00,0x00,0x3F,0x80,0x1F,
0xE0,0x00,0x00,0x1E,0x00,0x1F,0x80,0x00,
0x00,0x1E,0x00,0x1F,0x00,0x00,0x00,0x02,
0x38,0x1E,0x00,0x00,0x00,0x07,0xFC,0x1C,
0x00,0x20,0x00,0x07,0xFC,0x18,0x00,0x20,
0x00,0x1F,0x0C,0x10,0x00,0x20,0x00,0x7C,
0x04,0x10,0x00,0x60,0x01,0xF0,0x00,0x10,
0x00,0x60,0x01,0xE0,0x00,0x08,0x00,0xF0,
0x00,0x80,0x00,0x08,0x03,0xF0,0x00,0x00,
0x00,0x07,0xFF,0xF0,0x00,0x00,0x00,0x07,
0xFF,0xF0,0x00,0x00,0x00,0x03,0xFF,0xE0,
0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char tun48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x3E,
0x00,0x00,0x00,0x00,0x00,0x7F,0x00,0x00,
0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,
0x03,0xC0,0x00,0x00,0x00,0x00,0x1F,0x00,
0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,
0x00,0x01,0xF8,0x00,0x00,0x00,0x00,0x03,
0xF8,0x00,0x40,0x00,0x00,0x00,0x06,0x07,
0xC0,0x00,0x00,0x00,0x07,0xFF,0xE0,0x00,
0x00,0x00,0x07,0xFF,0xE0,0x00,0x00,0x00,
0x0F,0xFF,0x80,0x00,0x00,0x00,0x7F,0xF8,
0x00,0x00,0x00,0x1F,0xFF,0xF8,0x00,0x00,
0x00,0x1F,0xFF,0xF8,0x00,0x00,0x00,0x1F,
0xFC,0x3C,0x00,0x00,0x00,0x0F,0xF8,0x0E,
0x00,0x00,0x00,0x04,0x70,0x07,0x00,0x00,
0x00,0x00,0x60,0x03,0x80,0x00,0x00,0x00,
0xC0,0x00,0xC0,0x00,0x00,0x01,0x80,0x00,
0x30,0x00,0x00,0x01,0x00,0x3C,0x18,0x00,
0x00,0x02,0x03,0xFF,0x0C,0x00,0x00,0x0C,
0x7F,0xFF,0x8E,0x00,0x00,0x18,0xFF,0xFF,
0xC7,0x80,0x00,0x78,0xFE,0x07,0x87,0xE0,
0x01,0xF0,0x70,0x07,0x03,0xF8,0x07,0xE0,
0x70,0x0E,0x03,0xFE,0x00,0x00,0x38,0x1E,
0x01,0xFE,0x00,0x00,0x3F,0xFE,0x00,0x0C,
0x00,0x00,0x1F,0xFE,0x00,0x00,0x00,0x00,
0x1F,0xFE,0x00,0x00,0x00,0x00,0x0F,0xFE,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char dan48H[]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFC,0x00,0x00,0x00,0x00,0x07,0xFF,
0x00,0x00,0x00,0x00,0x7F,0xC0,0x80,0x00,
0x00,0x03,0xFF,0x80,0x40,0x00,0x00,0x01,
0xF1,0x80,0x40,0x00,0x00,0x01,0x81,0x80,
0xE0,0x00,0x00,0x00,0x01,0x93,0xF0,0x00,
0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,
0x21,0xFF,0xF0,0x00,0x00,0x00,0x21,0xF8,
0x00,0x00,0x00,0x00,0x61,0xC0,0x00,0x00,
0x00,0x00,0x61,0x80,0x00,0x00,0x00,0x00,
0xF3,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,
0x00,0x00,0x00,0x01,0xFF,0xC0,0x00,0x00,
0x00,0x03,0xFF,0xF8,0x00,0x00,0x00,0x02,
0x00,0xFC,0x00,0x00,0x00,0x04,0x02,0x1F,
0x00,0x00,0x00,0x08,0x03,0x01,0xC0,0x00,
0x00,0x38,0x03,0x00,0x7C,0x00,0x00,0xF8,
0x07,0xF8,0x3F,0xC0,0x01,0xF0,0x3F,0xFE,
0x3F,0xF8,0x03,0xC1,0xFF,0x0F,0x1F,0xF8,
0x00,0x01,0xE3,0x0F,0x0F,0xF0,0x00,0x01,
0xC3,0x0E,0x00,0x00,0x00,0x01,0x83,0xFC,
0x00,0x00,0x00,0x00,0xC7,0xF8,0x00,0x00,
0x00,0x00,0xFF,0xF8,0x00,0x00,0x00,0x00,
0x7F,0xF0,0x00,0x00,0x00,0x00,0x3F,0x03,
0x80,0x00,0x00,0x00,0x03,0x04,0x00,0x00,
0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,
0x1F,0xF8,0x20,0x00,0x00,0x00,0xFF,0xFF,
0xE0,0x00,0x00,0x07,0xFF,0x81,0xE0,0x00,
0x00,0x07,0xE0,0x00,0xE0,0x00,0x00,0x03,
0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char zuo16H[]={
0x18,0xC0,0x18,0xC0,0x19,0x80,0x31,0xFE,
0x33,0xFE,0x76,0xC0,0xF0,0xFC,0xB0,0xFC,
0x30,0xC0,0x30,0xC0,0x30,0xFE,0x30,0xFE,
0x30,0xC0,0x30,0xC0,0x30,0xC0,0x00,0x00,
};
char zhe16H[]={
0x03,0x00,0x03,0x0C,0x1F,0xCC,0x1F,0xD8,
0x03,0x30,0xFF,0xFE,0xFF,0xFE,0x03,0x00,
0x0F,0xF8,0x3F,0xF8,0xEC,0x18,0xCF,0xF8,
0x0C,0x18,0x0F,0xF8,0x0F,0xF8,0x00,0x00,
};
char tian16H[]={
0x00,0x00,0x3F,0xFC,0x3F,0xFC,0x31,0x8C,
0x31,0x8C,0x31,0x8C,0x3F,0xFC,0x3F,0xFC,
0x31,0x8C,0x31,0x8C,0x31,0x8C,0x3F,0xFC,
0x3F,0xFC,0x30,0x0C,0x00,0x00,0x00,0x00,
};
char xue16H[]={
0x33,0x18,0x19,0x98,0x08,0xB0,0x7F,0xFC,
0x7F,0xFC,0x60,0x0C,0x1F,0xF0,0x1F,0xF0,
0x00,0xC0,0x7F,0xFC,0x7F,0xFC,0x01,0x80,
0x01,0x80,0x07,0x80,0x03,0x00,0x00,0x00,
};
char ke16H[]={
0x00,0x00,0x0C,0x18,0xFD,0x98,0xF8,0xD8,
0x18,0x58,0xFE,0x18,0xFE,0x98,0x18,0xD8,
0x3C,0x58,0x7E,0x1E,0xDB,0xFE,0x9B,0xF8,
0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,
};
struct Food/*定義結(jié)構(gòu)體存儲食物的屬性*/
{
int x; /* 食物的坐標(biāo) */
int y;
int yes; /* 值為0表示屏幕上沒有食物,值為1表示屏幕上有食物 */
int color; /* 食物顏色 */
}food;
struct Snake/*定義結(jié)構(gòu)體存儲蛇的屬性*/
{
int x[N]; /* 每一節(jié)蛇的坐標(biāo) */
int y[N];
int color[N];/*存儲每一節(jié)蛇的顏色*/
int node; /* 蛇的節(jié)數(shù) */
int direction; /* 蛇移動的方向 */
int life; /* 蛇的生命,如果為1,蛇死,游戲結(jié)束 */
}snake;
void init(void)/*圖形驅(qū)動*/
{
int driver=DETECT,mode=0;
registerbgidriver(EGAVGA_driver);
initgraph(driver,mode,"");
}
void drawmat(char *mat,int matsize,int x,int y,int color)/*漢字點(diǎn)陣*/
{
int i,j,k,m;
m=(matsize-1)/8+1;
for(j=0;jmatsize;j++)
for(i=0;im;i++)
for(k=0;k8;k++)
if(mat[j*m+i](0x80k))
putpixel(x+i*8+k,y+j,color);
}
void showword(void)
{/* 調(diào)用漢字點(diǎn)陣輸出程序,顯示標(biāo)題和作者信息 */
drawmat(cai48H,48,249,-4,7);
drawmat(she48H,48,329,-4,7);
drawmat(tun48H,48,409,-4,7);
drawmat(dan48H,48,489,-4,7);
drawmat(cai48H,48,250,-5,4);
drawmat(she48H,48,330,-5,4);
drawmat(tun48H,48,410,-5,4);
drawmat(dan48H,48,490,-5,4);
/*作者 田學(xué)科*/
drawmat(zuo16H,16,515,465,7);
drawmat(zhe16H,16,530,465,7);
drawmat(tian16H,16,550,465,7);
drawmat(xue16H,16,565,465,7);
drawmat(ke16H,16,580,465,7);
}
void draw(void)/*畫出四周的墻*/
{
if(color==15)
color=0;
setcolor(++color);
setlinestyle(SOLID_LINE,0,1);
for(i=30;i=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i450;i+=10)
{
rectangle(30,i,39,i+10);
rectangle(601,i,610,i+10);
}
}
void prscore(void)
{/* 打印游戲分?jǐn)?shù) */
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,10,200,30);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,15,str);
}
void gameover(void)
{
cleardevice(); /* 清屏函數(shù) */
for(i=0;isnake.node;i++)/* 畫出蛇死時的位置 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
prscore(); /* 顯示分?jǐn)?shù) */
draw();
showword();
settextstyle(0,0,6);
setcolor(7);
outtextxy(103,203,"GAME OVER");
setcolor(RED);
outtextxy(100,200,"GAME OVER");
}
void gameplay(void)/* 玩游戲的具體過程 */
{
int flag,flag1;
randomize();
prscore();
gamespeed=50000;
food.yes=0;/* food.yes=0表示屏幕上沒有食物 */
snake.life=1;/* snake.life=1表示蛇是活著的 */
snake.direction=4;/* 表示蛇的初始方向為向右 */
snake.node=2; /* 蛇的初始化為兩節(jié) */
snake.color[0]=2; /*兩節(jié)蛇頭初始化為綠色*/
snake.color[1]=2;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
food.color=random(15)+1;
while(1)
{
while(1)
{
if(food.yes==0) /* 如果蛇活著 */
{
while(1)
{
flag=1;
food.yes=1;
food.x=random(56)*10+40;
food.y=random(40)*10+50;
for(i=0;isnake.node;i++)
{
if(food.x==snake.x[i]food.y==snake.y[i])
flag=0;
}
if(flag) break;
}
}
if(food.yes)
{
setcolor(food.color);
rectangle(food.x,food.y,food.x+10,food.y+10);
}
for(i=snake.node-1;i0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case 1: snake.y[0]-=10;break;
case 2: snake.y[0]+=10;break;
case 3: snake.x[0]-=10;break;
case 4: snake.x[0]+=10;break;
}
for(i=3;isnake.node;i++)
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
gameover();
snake.life=0;
break;
}
}
if(snake.x[0]40||snake.x[0]590||snake.y[0]50||snake.y[0]440)
{
gameover();
snake.life=0;
}
if(snake.life==0)
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*蛇吃掉食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y+10);
snake.x[snake.node]=-20;
snake.y[snake.node]=-20;
snake.color[snake.node]=food.color;
snake.node++;
food.yes=0;
food.color=random(15)+1;
score+=10;
prscore();
if(score%100==0score!=0)
{
for(i=0;isnake.node;i++)/* 畫出蛇 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
sound(200);
delay(50000);delay(50000);delay(50000);
delay(50000);delay(50000);delay(50000);
nosound();
gamespeed-=5000;
draw();
}
else
{
sound(500);
delay(500);
nosound();
}
}
for(i=0;isnake.node;i++)/* 畫出蛇 */
{
setcolor(snake.color[i]);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]+10);
}
delay(gamespeed);
delay(gamespeed);
flag1=1;
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]+10);
if(kbhit()flag1==1)/*如果沒按有效鍵就重新開始循環(huán)*/
{
flag1=0;
key=bioskey(0);
if(key==esc)
exit(0);
else if(key==upsnake.direction!=2)
snake.direction=1;
else if(key==downsnake.direction!=1)
snake.direction=2;
else if(key==leftsnake.direction!=4)
snake.direction=3;
else if(key==rightsnake.direction!=3)
snake.direction=4;
}
}
if(snake.life==0)/*如果蛇死了就退出循環(huán)*/
break;
}
}
void main(void)
{
while(1)
{
color=0;
init();
cleardevice();
showword();
draw();
gameplay();
setcolor(15);
settextstyle(0,0,2);
outtextxy(200,400,"CONTINUE(Y/N)?");
while(1)
{
key=bioskey(0);
if(key==Y||key==n||key==esc)
break;
}
if(key==n||key==esc)
break;
}
closegraph();
}
求一個n×n矩陣對角線元素之和的源代碼如下
public class lianxi28 {
public static void main(String[] args) {
System.out.println("請輸入九個整數(shù):");
Scanner in = new Scanner(System.in);
int[][] a = new int[3][3];
for(int i=0;i3;i++){
for(int j=0;j3;j++){
a[i][j]=in.nextInt();
}
}
System.out.println("你輸入的n×n矩陣是:");
for(int i=0;i3;i++){
for(int j=0;j3;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println();
int sum=0;
for(int i=0;i3;i++){
for(int j=0;j3;j++){
if(i==j){
sum=sum+a[i][j];
}
System.out.println(sum);
}
擴(kuò)展資料
1、用arrange生成一個數(shù)組,元素為0,1,...,8,然后利用reshape生成一個n×n的矩陣。
2、利用for循環(huán)將該矩陣的主對角線上的元素相加,然后輸出結(jié)果。