当前位置:首页计算机类软件水平考试初级程序员->阅读以下说明和C程序,将应填入(n)处的字句写在对应栏内。[

阅读以下说明和C程序,将应填入 (n) 处的字句写在对应栏内。[说明]

下面的程序用Dole Rob算法生成N阶(N为奇数)魔方阵(各行、列、对角线数字之和相等)。该算法的过程为:从1开始,按如下方法依次插入各自然数,直到N2为止。

①在第一行的正中插入1。

②新位置应当处于最近插入位置的右上方,若该位置已超出方阵的上边界,则新位置取应选列的最下一个位置;若超出右边界,则新位置取应选行的最左一个位置。

③若最近插入的元素是N的整数倍,则选同列的下一行位置为新位置。

例如,3阶魔方阵如下所示:

8 1 6

3 5 7

4 9 2

[C程序]

include<stdio.h>

include<stdlib.h>

define SIZE 50

main()

{

int row, col, n, value;

int a[SIZE+1][SIZE+1]; /*不使用下标为0的元素*/

printf("请输入要输出魔方阵的阶数n(奇数, <%d):n=", SIZE);

scanf("%d", &n);

if(!(n%2) || n<1 || (1) ){

printf("输入数据有误!\n");

exit(0);

}

row=1; col=(n+1)/2; value=1;

while(value<= (2) ) {

a[row][col]=value;

/*计算下一位置*/

if(value%n!=0){

row--; (3) ;

if(row<1)row=n;

if(col>n) (4) ;

}

else row++;

value= (5) ;

}

printf("\n%d阶魔方阵如下所示:\n\n", n);

for(row=1; row<=n; row++){

for(col=1; col<=n; col++)

printf("%5d", a[row][col]);

printf("\n");

}

}

答案:
本题解析:

(1)n>SIZE,或其等价表示 (2)n*n (3)col++,或++col,或col=col+1,或其等价表示 (4)col-=n,或col=1,或其等价表示 (5)value+l,或其等价表示

【解析】

本题考查根据算法编写程序的基本能力。 N阶魔方阵定义为各行、列、对角线的数字之和相等。DoleRob给出了奇数阶魔方阵的算法,算法中方阵的行号和列号从1至N取值。程序中空(1)处判断n的合法性, n需为奇数,矩阵规模应不超过SIZE2。 根据题中的算法描述,由于要按次序将数值1~n2放入方阵中(在程序中以value表示每次要存入的数值),因此从1开始填入。将数值填入方阵的语句为“a[row][col]= value;”,该语句在循环中,因此循环条件为“value<=n*n”,因此,空(2)处填入“n*n”。 程序中,本次填入的数值为value的值,下一次要填入的数值为value加1,因此,空(5)处应填入“value+l”。 数值1的位置在第一行的正中间,即行号为1、列号为(n+1)/2,下一个位置即2的位置在1的右上方,即1所在位置的行号减1、列号加1。当新位置超出方阵的上边界,则新位置取应选列的最下一个位置:若超出右边界,则新位置取应选行的最左一个位置,因此对于3阶魔方阵,1填入第1行第2列,2填入第3行第3列,3填入第2行第1列,其余位置按照算法步骤b类推。因此,空(3)处填入“col++”或其等价形式,空(4)处填入“col=1或“col-=n”。需要考虑的特殊情况是本次填入的value值为N的倍数时,下一个插入位置为本次插入位置的正下方,即对于3阶矩阵,4填入3的正下方,7填入6的正下方等。

更新时间:2022-10-11 07:51
纠错

你可能感兴趣的试题

单选题

在几种不同类型的软件维护中,通常情况下()所占工作量最大。

  • A.更正性维护
  • B.适应性维护
  • C.完善性维护
  • D.预防性维护
查看答案
单选题

在()中,项目经理的权力是最小的。

  • A.强矩阵型组织
  • B.平衡矩阵组织
  • C.弱矩阵型组织
  • D.项目型组织
查看答案
单选题

在项目实施的过程中,项目经理通过项目周报中的项目进度分析图表发现机房施工进度有延期风险。项目经理立即组织相关人员进行分析,下达了关于改进措施的书面指令。该指令属于( )

  • A.检查措施
  • B.缺陷补救措施
  • C.预防措施
  • D.纠正措施
查看答案
单选题

中级数据库系统工程师,章节精选,中级数据库系统工程师

中级数据库系统工程师,章节精选,中级数据库系统工程师

中级数据库系统工程师,章节精选,中级数据库系统工程师

  • A.见图A
  • B.见图B
  • C.见图C
  • D.见图D
查看答案
单选题

中级数据库系统工程师,章节精选,中级数据库系统工程师

中级数据库系统工程师,章节精选,中级数据库系统工程师

中级数据库系统工程师,章节精选,中级数据库系统工程师

  • A.见图A
  • B.见图B
  • C.见图C
  • D.见图D
查看答案
单选题

中级数据库系统工程师,章节精选,中级数据库系统工程师

中级数据库系统工程师,章节精选,中级数据库系统工程师

  • A.u[1]<v[2]
  • B.u[1]>v[2]
  • C.u[1]<v[5]
  • D.u[1]>v[5]
查看答案
单选题

The IT service manager resigns from a project that meets the scheduleand budget. After hiring an alternativenew manager, the team is opposed to the comments from the new manager. The team is at () developmentstages。

  • A.Forming
  • B.Storming
  • C.Norming
  • D.Performing
查看答案
单选题

()is closet to Deming's definition of Quality。

  • A.Conformance to requirements
  • B.Fitness for use
  • C.Continuousimprovement of products and services
  • D.Customer focus
查看答案
单选题

The IT service manager has learned that a software canimprove the efficiency of current and future project tasks. Because the software is fresh to the Company. Theengineer is not familiar with the software. The lT service manager decides to send the highest level engineero attend the external training course. The proiect manager uses () risk strategies。

  • A.mitigation
  • B.enhancement
  • C.development
  • D.sharing
查看答案
单选题

The process control charts are used ()。

  • A.to graph what typically focuses on the prevention of a problem
  • B.to detect the problem
  • C.to reject the problem
  • D.to determine the acceptability of the sample
查看答案