回复 16楼 小孩不懂爱
这个结果是对的。如果你指的是数组位置的偶数的话。如果你是指我们看的位置偶数的话,应该吧a%2==0和b%2==0改成a%2==1和b%2==1才行
回复 21楼 lcsama
非常感谢,修改以后第二个求积的结果运行正确了,可是第一个求和的结果依旧是错误的,但第二个求积计算得出不就表明对角线的算法没有错误吗?为什么求和依旧求不出来?
程序代码:
/*
http://bbs.bccn.net/thread-439898-1-1.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
const size_t Rows = 5;
const size_t Cols = 5;
const int Data[Rows][Cols] = {
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15 },
{ 16, 17, 18, 19, 20 },
{ 21, 22, 23, 24, 25 }
};
/*
const int Data[Rows][Cols] = {
{ 12, 56, 34, 56, 34 },
{ 13, 45, 67, 45, 67 },
{ 34, 67, 54, 67, 78 },
{ 34, 56, 78, 89, 9 },
{ 34, 56, 78, 9, 56 }
};
*/
void Pause(void);
void ShowData(void);
void GetResult(int* result1, int* result2);
int main(void)
{
ShowData();
putchar('\n');
int result1, result2;
GetResult(&result1, &result2);
printf_s("两条对角线上各元素之和 = %d\n", result1);
printf_s("两条对角线上行、列下标均为偶数的各元素之积 = %d\n", result2);
Pause();
return EXIT_SUCCESS;
}
void Pause(void)
{
printf_s("\nPress any key to continue...");
_getch();
}
void ShowData(void)
{
for (size_t row = 0; row < Rows; ++row)
{
for (size_t col = 0; col < Cols; ++col)
{
printf_s("%02d", Data[row][col]);
if (col < Cols - 1)
{
putchar(' ');
}
}
putchar('\n');
}
}
void GetResult(int* result1, int* result2)
{
*result1 = 0;
*result2 = 1;
for (size_t row =0; row < Rows; ++row)
{
for (size_t col = 0; col < Cols; ++col)
{
if (row == col)
{
//printf_s("%02d ", Data[row][col]);
*result1 += Data[row][col];
if ((row % 2 == 0) && (col % 2 == 0))
{
//printf_s("%02d ", Data[row][col]);
*result2 *= Data[row][col];
}
}
if (col == Cols - row - 1)
{
//printf_s("%02d ", Data[row][col]);
*result1 += Data[row][col];
if ((row % 2 == 0) && (col % 2 == 0))
{
//printf_s("%02d ", Data[row][col]);
*result2 *= Data[row][col];
}
}
}
}
}
