卡提諾王國's Archiver

ben5208 發表於 2009-10-18 18:26

(C)如何做出這圖形來?

[i=s] 本帖最後由 風箏惡魔 於 2009-10-19 12:11 編輯 [/i]

[img]http://img14.imageshack.us/img14/494/hw3k.jpg[/img]

構想是棋盤式

構出x*x的畫格

第一個顯示出x 第二個不顯示

如果用雙for該如何打?想半天了...

negotiator 發表於 2009-10-18 21:01

[i=s] 本帖最後由 negotiator 於 2009-10-19 06:24 編輯 [/i]

我是有一個取巧的方法!! 設一個flag來看是否要換行了!
int flag=0;
for(i=0;i<setNum;i++){ //自訂要跑多少行
   if(flag >1){
       if(play =="a") {play = "b";} //如果是a模式,則換b
       else{ play ="a";} //如果是b樣式,則換a
       flag=0;  //規零
   }
   flag++;
   switch(play){
       case a:
                .........................
       case b:
                .........................
       default:
                .........................
   }
}

runeray2000 發表於 2009-11-16 11:27

[b] [url=http://ck101.com/redirect.php?goto=findpost&pid=71669155&ptid=1523993]1#[/url] [i]ben5208[/i] [/b]
給你做參考: 這是用dev_c++寫的:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    const int wx=79;        //畫面寬度
    const int wy=20;        //畫面高度
    const int wbx=3;        //格子寬度
    const int wby=2;        //格子高度
    const char chr_a='x';   //格子顯示字元a
    const char chr_b=' ';   //格子顯示字元b
    int x,y;
    int rx,ry;
    char chr[2]={chr_a,chr_b};
    printf("\n");
    for(y=1;y<=wy;y++)
    {                 
                      ry=y%(wby*2)<wby;         //y軸是奇數格位置=1,偶數位置=0
                      for(x=1;x<=wx;x++)
                      {
                                                 rx=x%(wbx*2)<wbx; //X軸是奇數格位置=1,偶數位置=0
                                                 printf("%c",chr[rx^ry]);  //互斥或運算出該顯示哪個字元                     
                                                 }
                      printf("\n");
                      }
        system("PAUSE");
    return EXIT_SUCCESS;
}

重點有註解,想法是用雙迴圈x與y,用註解處運算出該"點"應顯示何字元,應該很好懂吧?要把3列運算式只寫成一行也是可以的,只是分三行寫比較好懂。

頁: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.