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

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

[复制链接]
发表于 2011-11-17 15:58:52 | 显示全部楼层 |阅读模式
本帖最后由 kenan 于 2011-11-17 17:46 编辑



先建立一个NXN的二维数组,对数组赋值,最后输出
可是怎么赋值啊,求思路
  1. #include<iostream>
  2. #include<vector>
  3. #include<iomanip>
  4. using namespace std;
  5. void main()   
  6. {
  7.         int n;
  8.         int num=1;
  9.         cin>>n;
  10.         vector <vector <int> >   a(n,   vector <int> (n));
  11. //赋值
  12. //怎么赋啊


  13. //输出
  14.         for (i=0;i<n;i++){
  15.                 for (int j=0;j<n;j++)
  16.                 if (a[i][j]==0) cout<<setw(3)<<" ";
  17.                 else cout<<setw(3)<<a[i][j];
  18.                 cout<<endl;
  19.         }
  20. }
复制代码

本帖子中包含更多资源

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

×
 楼主| 发表于 2011-11-17 17:45:32 | 显示全部楼层
  1. #include<iostream>
  2. #include<vector>
  3. #include<iomanip>
  4. using namespace std;
  5. void main()   {
  6.         int n;
  7.         cin>>n;
  8.         vector <vector <int> >   a(n,   vector <int> (n));
  9.         int low=0;  
  10.         int j,i;
  11.         int hi=n;  
  12.         for(j=1;j<=n*(n+1)/2;low++,hi--)  {
  13.                 for(i=low;i<hi-low;i++)
  14.                         a[low+i][low]=j++;  
  15.                 for(i=low+1;i<hi-low;i++)
  16.                         a[hi-1][i]=j++;  
  17.                 for(i=(hi-low)-2;i>low;i--)
  18.                         a[low+i][i]=j++;  
  19.         }
  20.         for (i=0;i<n;i++){
  21.                 for ( j=0;j<n;j++)
  22.                 if (a[i][j]==0) cout<<setw(3)<<" ";
  23.                 else cout<<setw(3)<<a[i][j];
  24.                 cout<<endl;
  25.         }
复制代码
发表于 2011-11-17 19:19:24 | 显示全部楼层
本帖最后由 pusofalse 于 2011-11-17 19:20 编辑

[au3]#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))[/au3]
发表于 2011-11-17 20:49:39 | 显示全部楼层
  1. #include <Array.au3>

  2. Local $Num = 300
  3. Local $Ts = TimerInit()

  4. Local $a[$Num][$Num], $ns = 0, $xx = 0, $max = $Num - 1, $m = 0

  5. For $x = 0 To ($Num / 1.5) Step 2
  6.         For $i = $x To $max
  7.                 $ns += 1
  8.                 $a[$i][$xx] = $ns
  9.         Next
  10.         For $j = $xx + 1 To $max - $xx
  11.                 $ns += 1
  12.                 $a[$max][$j] = $ns
  13.         Next
  14.         For $k = $max - 1 To $x + 1 Step -1
  15.                 $ns += 1
  16.                 $a[$k][$k - $m] = $ns
  17.         Next
  18.         $max -= 1
  19.         $xx += 1
  20.         $m += 1
  21. Next
  22. ;Msgbox(0, '', $ns)

  23. _ArrayDisplay($a, TimerDiff($Ts))
复制代码
 楼主| 发表于 2011-11-17 22:10:43 | 显示全部楼层
谢谢两位版主热心回复啊,学习了
发表于 2011-11-18 09:32:51 | 显示全部楼层
学习啊!!支持
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2026-9-22 10:02 , Processed in 0.076225 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2026 Discuz! Team.

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