有一个N×N的数组求其右上三角含主对角线的元素之和。#include stdioh#define N 3int funint aNN int ijs; s=0; fori=0;iN;i++ forj=0;jN;j++ if【1】 s+=aij;【2】;main int aNNijs; fori=0;iN;i++ forj=0;
修改后的程序:
#include "stdio.h" #define N 3 int fun(int a[N][N]) { int i,j,s; s=0; for(i=0;i<N;i++) for(j=i;j<N;j++) // 只计算右上三角 s+=a[i][j]; return s; } int main() { int a[N][N],i,j,s; for(i=0;i<N;i++) for(j=0;j<N;j++) scanf("%d",&a[i][j]); s = fun(a); printf("%d\n",s); return 0; }
原文地址: https://www.cveoy.top/t/topic/fe0H 著作权归作者所有。请勿转载和采集!