LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: magang2

发一个简章的编程题,看谁写的最好。

[复制链接]
发表于 2006-12-29 22:01:00 | 显示全部楼层
看错题目了。
回复 支持 反对

使用道具 举报

发表于 2006-12-30 13:39:07 | 显示全部楼层
Post by magang2
好了,还是贴出来标程吧。
  1. #include<iostream>
  2. #include<iomanip>
  3. #define N 10
  4. using namespace std;
  5. int main()
  6. {
  7.    int A[N*N];
  8.    int B[N][N];
  9.    int n=0;
  10.    for(int i=0;i<N*N;i++) { // set A[N*N]
  11.        A[i]=i;
  12.    }
  13.    for(int i=0;i<N;i++)   
  14.       for(int j=i;j>=0;j--)   { //fill B with A
  15.         B[(i%2==0)?j:(i-j)][(i%2!=0)?j:(i-j)]=A[n];
  16.         B[(i%2==0)?(N-1-j):(N-1-i+j)][(i%2!=0)?(N-1-j):(N-1-i+j)]=A[N*N-1-n];
  17.         n++;
  18.       }
  19.    for(int i=0;i<N;i++)   {  //print B
  20.      for(int j=0;j<N;j++)   {
  21.          cout<<setw(4)<<" "<<B[i][j];
  22.      }   
  23.      cout<<endl<<endl;
  24.    }
  25.    return 0;   
  26. }
复制代码

这个程序果然是最好的.
回复 支持 反对

使用道具 举报

发表于 2006-12-31 19:27:41 | 显示全部楼层
我来贴一个白痴级算法。
  1. #include <stdio.h>
  2. #define N 4
  3. int main()
  4. {
  5.         int flag = -1;
  6.         int a[N * N] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  7.         int b[N][N] = {0};
  8.         int x = 0, y = 0, t = 0;
  9.         int has_done = 0;
  10.         for (t = 0; t < N * N; t++) {
  11.                 printf ("x=%d,y=%d,t=%d,flag=%d\n",x,y,a[t],has_done);
  12.                 b[x][y] = a[t];
  13.                 if (!has_done && (y == 0 || x == 0 || x >= N - 1 || y >= N - 1)) {
  14.                         if (x >= N - 1) y++;
  15.                         else if (y >= N - 1) x++;
  16.                         else if (x == 0) y++;
  17.                         else if (y == 0) x++;
  18.                         flag = -1 * flag;
  19.                         has_done = 1;
  20.                 } else {
  21.                         x = x + flag;
  22.                         y = y - flag;
  23.                         has_done = 0;
  24.                 }
  25.         }
  26.         for (x = 0,y = 0; y < N; y ++) {
  27.                 for (x = 0; x < N; x++)
  28.                         fprintf (stdout, "%d ", b[x][y]);
  29.                 fprintf (stdout, "\n");
  30.         }
  31. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-12-31 23:28:20 | 显示全部楼层
我也来一个:
/****************************************************************
*   
*    方阵输出
*    当参数num=4时,输出:
*        1   2   6   7
*        3   5   5  13
*        4   9  12  14
*       10  11  15  16
*
*   参数option是用来控制方阵的输出方式,当option!=0时,如上;
*   否则则按下面的方式输出:
*        1   3   4  10
*        2   5   9  11
*        6   8  12  15
*        7  13  14  16
*
*                  2006-12-27  author:iawen
*
*************************************************************/

#include<iostream>
#include<iomanip>
using std::cout;

int main(int argc,char** argv)
{
        if(argc<3){
                cout<<"Usage: "<<argv[0]<<" [num] [option]\n"
                    <<"\tnum\tnum is the array'row and col\n"
                    <<"\toption\toption is the output direct(0 or 1)\n";
                return -1;
        }
        const int n=atoi(argv[1]);
        int flag=atoi(argv[2]);

        int i,j,k,tmp;

        int num[n][n];
        int count=1;
        for(k=0;k<=2*n-2;k++){
                if(flag)
                        tmp=k;
                else
                        tmp=k+1;

                if(tmp%2==0){
                        if(k<n)
                                for(i=k,j=0;i>=0 && j<=k;j++,i--){
                                        num[j]=count;
                                        count++;
                                }
                        else
                                for(i=n-1,j=k-n+1;i>=k-n+1 && j<=n-1;j++,i--){
                                        num[j]=count;
                                        count++;
                                }
                }
                else{
                        if(k<n)
                                for(j=k,i=0;j>=0 && i<=k;i++,j--){
                                        num[j]=count;
                                        count++;
                                }
                        else
                                for(i=k-n+1,j=n-1;i<=n-1 && j>=k-n+1;i++,j--){
                                        num[j]=count;
                                        count++;
                        }
                }
        }

        for(i=0;i<n;i++){
                for(j=0;j<n;j++)
                        cout<<std::setw(5)<<num[j];
                cout<<"\n\n";
        }
        return 0;
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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