看板 Programming
作者 標題
本來要出給學弟當練習的題目
時間 2010年11月12日 Fri. PM 03:10:41
時間 2010年11月12日 Fri. PM 03:10:41
讓使用者輸入一個奇數
然後用*畫成正方形 一圈一圈的畫出
如下
9
1 2 3 4 5 6 7 8 9
1*********
2* *
3* ***** *
4* * * *
5* * * * *
6* * * *
7* ***** *
8* *
9*********
13
1 2 3 4 5 6 7 8 910111213
1*************
2* *
3* ********* *
4* * * *
5* * ***** * *
6* * * * * *
7* * * * * * *
8* * * * * *
9* * ***** * *
10* * * *
11* ********* *
12* *
13*************
(數字不用畫上去~)
以下是我寫的code
寫了才發現寫這個是浪費時間
老師也不會考..............
另外 我覺得我寫得很鳥
一定有更好的寫法
歡迎大家討論~
[code=cpp]
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
int main()
{
string x1,x2;
x1=" ";
x2="*";
int n = 0;
cout<<"請輸入邊長 : ";
cin>>n;
cout<<"\n=========================================\n";
//畫隔層輸出*
for(int i =0 ; i<n ;i++)
{
for(int j = 0 ; j < n ;j++)
{
if(i%2==0)
{
if(i<n/2)
{
if( (j<i) && (j%2==1) )
{
cout<<x1;
}
else if((j>=n-i) && (j%2==1))
{
cout<<x1;
}
else
{
cout<<x2;
}
}
else
{
if( (j<n-i-1) && (j%2==1) )
{
cout<<x1;
}
else if((j>=i+1) && (j%2==1))
{
cout<<x1;
}
else
{
cout<<x2;
}
}
}
else
{
if(i<n/2)
{
if( (j<i) && (j%2==0) )
{
cout<<x2;
}
else if((j>=n-i) && (j%2==0))
{
cout<<x2;
}
else
{
cout<<x1;
}
}
else
{
if( (j<n-i-1) && (j%2==0) )
{
cout<<x2;
}
else if((j>=i+1) && (j%2==0))
{
cout<<x2;
}
else
{
cout<<x1;
}
}
}
}
cout<<"\n";
}
cout<<"\n=========================================\n";
system("pause");
return 0 ;
}
--
※ 來源: Disp BBS 看板: Programming 文章連結: http://disp.cc/b/33-Kkh
※ 作者: abc1231qa 時間: 2010-11-12 15:10:41 來自: 114-44-210-20.dynamic.hinet.net
※ 看板: Programming 文章推薦值: -1 目前人氣: 0 累積人氣: 1525
回列表(←)
分享