Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
输入一个正整数n(1<=n<=10),再输入n*n的矩阵,要求求该矩阵的下三角元素之和。
Input
输入包括n+1行。
第一行为整数n;
接下来的n行为矩阵数据。
Output
矩阵的下三角元素之和。
Sample Input
5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Sample Output
75
Hint
Source
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int i,j;
int a[11][11],sum = 0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",(*(a+i)+j)); //用指针间接访问数组a的各个元素的地址并赋值;
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++) //用j<=i,便可以实现只累加下三角的各数之和;
{
sum += *(*(a+i)+j); //其中a为数组a的首地址;
}
}
printf("%d\n",sum);
return 0;
}
5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
75
Process returned 0 (0x0) execution time : 3.403 s
Press any key to continue.