Grisson's .net

源码之前,了无秘密

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1.输入一个n ,然后在屏幕上打印出NxN 的矩阵!

例如,输入一个3,则
1 2 3
8 9 4
7 6 5
输入一个4,则
1   2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

解:

#include
#include

using namespace std;

const int N=10;

void print(int a[][N],int n)
{
 int round=0;
 int i,j;
 int m=1;

 while(m<=n*n)
 {
   for(i=round;i     a[round][i]=m++;
   for(i=round+1;i     a[i][n-round-1]=m++;
   for(i=n-round-2;i>=round;i--)
    a[n-round-1][i]=m++;
   for(i=n-round-2;i>=round+1;i--)
    a[i][round]=m++;
  round++;
 }
 for(i=0;i {
   for(j=0;j   {
     cout <   }
   cout << endl;
 }
}

void main()
{
 int a[N][N];
 int round=0;
 int n,m=1;
 cout << "Please input n:";
 cin>>n;
 print(a,n);
}
2.如何打印出当前源文件的文件名以及源文件的当前行号?

 cout <<"FILE:"<<__FILE__<<endl
   <<"Line:"<<__LINE__<<endl
   <<"Time:"<<__TIME__<<endl
   <<"Date:"<<__DATE__<<endl;
3.如何判断一段程序是由C编译程序还是由C++编译程序编译的?

c++编译定义了: __cplusplus
c编译定义了: __STDC__

posted on 2005-08-21 20:26  海盗  阅读(2382)  评论(0编辑  收藏  举报