一道c++题目:图形输出(想了半天,好蛋疼啊)【已解决】
本帖最后由 kenan 于 2011-11-17 17:46 编辑先建立一个NXN的二维数组,对数组赋值,最后输出
可是怎么赋值啊,求思路#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
void main()
{
int n;
int num=1;
cin>>n;
vector <vector <int> > a(n, vector <int> (n));
//赋值
//怎么赋啊
//输出
for (i=0;i<n;i++){
for (int j=0;j<n;j++)
if (a==0) cout<<setw(3)<<" ";
else cout<<setw(3)<<a;
cout<<endl;
}
} #include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
void main() {
int n;
cin>>n;
vector <vector <int> > a(n, vector <int> (n));
int low=0;
int j,i;
int hi=n;
for(j=1;j<=n*(n+1)/2;low++,hi--){
for(i=low;i<hi-low;i++)
a=j++;
for(i=low+1;i<hi-low;i++)
a=j++;
for(i=(hi-low)-2;i>low;i--)
a=j++;
}
for (i=0;i<n;i++){
for ( j=0;j<n;j++)
if (a==0) cout<<setw(3)<<" ";
else cout<<setw(3)<<a;
cout<<endl;
}
本帖最后由 pusofalse 于 2011-11-17 19:20 编辑
#include <Array.au3>
Const $COUNT = 5
Local $iTimerInit = TimerInit()
Local $iDirect, $aMatrix[$COUNT][$COUNT], $iX = -1, $iY = 0
For $i = 1 To ($COUNT ^ 2 + $COUNT) / 2
Switch $iDirect
Case 0
If $iX + 1 = $COUNT Or $aMatrix[$iX + 1][$iY] Then
$iY += 1
$iDirect = 1
Else
$iX += 1
EndIf
Case 1
If $iY + 1 = $COUNT Or $aMatrix[$iX][$iY + 1] Then
$iDirect = 2
$iX -= 1
$iY -= 1
Else
$iY += 1
EndIf
Case 2
If $aMatrix[$iX - 1][$iY - 1] Then
$iDirect = 0
$iX += 1
Else
$iX -= 1
$iY -= 1
EndIf
EndSwitch
$aMatrix[$iX][$iY] = $i
Next
_ArrayDisplay($aMatrix, TimerDiff($iTimerInit)) #include <Array.au3>
Local $Num = 300
Local $Ts = TimerInit()
Local $a[$Num][$Num], $ns = 0, $xx = 0, $max = $Num - 1, $m = 0
For $x = 0 To ($Num / 1.5) Step 2
For $i = $x To $max
$ns += 1
$a[$i][$xx] = $ns
Next
For $j = $xx + 1 To $max - $xx
$ns += 1
$a[$max][$j] = $ns
Next
For $k = $max - 1 To $x + 1 Step -1
$ns += 1
$a[$k][$k - $m] = $ns
Next
$max -= 1
$xx += 1
$m += 1
Next
;Msgbox(0, '', $ns)
_ArrayDisplay($a, TimerDiff($Ts)) 谢谢两位版主热心回复啊,学习了 学习啊!!支持
页:
[1]