小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

基于visual Studio2013解决C语言竞赛题之1050矩阵反斜线求和






题目


解决代码及点评

/************************************************************************/
/* 
50.	求N阶方阵(即N×N数组)中各条反斜线上的元素之和。如4×4数组共有7条反斜线:
25	   1	   8	  12
7	  100	   3	  15
2	   5	   7	   9
8	  11	  22	   6
     
注:求和时,请按斜线编号顺序显示求出的和。


*/
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>



void main()
{
	const int N=5;
	int arr[N][N]={0};
	int arrnum[N*2-1]={0};
	for (int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)
		{
			arr[i][j]=rand()%10;
			printf("%5d",arr[i][j]);
			
		}
		printf("\n");
	}
	int m=N-1;int n=0;int cen=1;
	int sum=0; int temp=1;
	for (int i=0;i<N*N;i++)
	{
		sum+=arr[m][n];
	
		if (n+1>N-1)
		{
			m=0;
			n=cen-N+1;
			cen++;
			printf("和为:%d\n",sum);
			sum=0;
		}
		else if (m+1>N-1)
		 {
			 m=N-cen-1;
			 n=0;
			 cen++;
			 printf("和为:%d\n",sum);
			 sum=0;
		 }
		else
		{
			m++;
			n++;
		}
		 
	}
	system("pause");
}


代码编译以及运行

由于资源上传太多,资源频道经常被锁定无法上传资源,同学们可以打开VS2013自己创建工程,步骤如下:

1)新建工程

2)选择工程

3)创建完工程如下图:

4)增加文件,右键点击项目

5)在弹出菜单里做以下选择

6)添加文件

7)拷贝代码与运行


程序运行结果


代码下载

http://download.csdn.net/detail/yincheng01/6681845

解压密码:c.itcast.cn








posted on 2013-12-09 22:34  牛栏山1  阅读(84)  评论(0编辑  收藏  举报

导航