找回密码
 加入
搜索
查看: 2586|回复: 5

[效率算法] 一道c++题目:图形输出(想了半天,好蛋疼啊)【已解决】

[复制链接]
发表于 2011-11-17 15:58:52 | 显示全部楼层 |阅读模式
本帖最后由 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[i][j]==0) cout<<setw(3)<<" "; 
                else cout<<setw(3)<<a[i][j];
                cout<<endl;
        }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2011-11-17 17:45:32 | 显示全部楼层
#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[low+i][low]=j++;  
                for(i=low+1;i<hi-low;i++)
                        a[hi-1][i]=j++;  
                for(i=(hi-low)-2;i>low;i--)
                        a[low+i][i]=j++;  
        }
        for (i=0;i<n;i++){
                for ( j=0;j<n;j++)
                if (a[i][j]==0) cout<<setw(3)<<" "; 
                else cout<<setw(3)<<a[i][j];
                cout<<endl;
        }
发表于 2011-11-17 19:19:24 | 显示全部楼层
本帖最后由 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))
发表于 2011-11-17 20:49:39 | 显示全部楼层
#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))
 楼主| 发表于 2011-11-17 22:10:43 | 显示全部楼层
谢谢两位版主热心回复啊,学习了
发表于 2011-11-18 09:32:51 | 显示全部楼层
学习啊!!支持
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-22 07:38 , Processed in 0.085463 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表