TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/*
- 功能:编程产生如下形式的方阵。
- 1 2 2 2 2 2 1
- 3 1 2 2 2 1 4
- 3 3 1 2 1 4 4
- 3 3 3 1 4 4 4
- 3 3 1 5 1 4 4
- 3 1 5 5 5 1 4
- 1 5 5 5 5 5 1
- 要求:不允许使用键盘输入语句和静态赋值语句,尽量少用循环。
-
-
- 时间:12:28 2013/10/26
- */
-
- #include<stdio.h>
- #include <stdlib.h>
-
- void main()
- {
- const int N=7;
- int a[N][N]={0};
- for(int i=0;i<N;i++) //横坐标遍历
- {
- if(i<=N/2) //小与N/2的部分按规则放3
- {
- for (int j=i+1;j<N-i-1;j++)
- {
- a[j]=3;
- }
- }
- if(i>=N-N/2) //大于的部分按规则放5
- {
- for (int j=N-i;j<i;j++)
- {
- a[j]=4;
- }
- }
- }
-
- for(int i=0;i<N;i++) //纵坐标遍历
- {
- if(i<=N/2) //符合条件的安规则赋2
- {
- for (int j=i+1;j<N-i-1;j++)
- {
- a[j]=2;
- }
- }
- if(i>=N-N/2) //赋5
- {
- for (int j=N-i;j<i;j++)
- {
- a[j]=5;
- }
- }
- }
-
- for(int i=0;i<N;i++) //对角线赋值
- {
- for(int j=0;j<N;j++)
- {
- if(i==j)a[j]=1; //j==i
- if(i+j==6)a[j]=1; //i+j=N-1
- }
- }
-
- for (int i=0;i<N;i++) //打印数组
- {
- for (int j=0;j<N;j++)
- {
- printf("%2d",a[j]);
- }
- printf("\n");
- }
- system("pause");
- }</pre><br><br></pre>
|
|