有关三角形的c语言编程题,有趣的三角形最大和问题(ACM题)

经过我的不懈努力,放弃了上边用二维数组实现的想法,终于用一维数组搞定实现了。

下边是源代码,实验过好几遍了,结果都没错。

13a2f4bd84d8af35e7645442bb7a26a3.gif

#include

#include

struct triangle

{

int element;

int sum;

}*array;

int Count( struct triangle *arr, int m, int s )

{

int i, j, temp;

int result = 0;

int n = 0;

for( i = 0; i <= (m - 1) * (m - 2) / 2; i = i + n )

{

for( j = 0; j <= n; j ++ )

{

temp = arr[i + j].sum + arr[i + j + n + 1].element;

if( temp > arr[i + j + n + 1].sum )

arr[i + j + n + 1].sum = temp;

temp = arr[i + j].sum + arr[i + j + n + 1 + 1].element;

if( temp > arr[i + j + n + 1 + 1].sum )

arr[i + j + n + 1 + 1].sum = temp;

}

n++;

}

for( i = m * (m - 1) / 2; i < s; i ++ )

{

if( arr[i].sum > result )

result = arr[i].sum;

}

return result;

}

void main()

{

int i;

int degree, size;

int result;

printf( "Please input the degree of triangle:" );

while(1)

{

scanf( "%d", &degree );

if( degree > 1 && degree <= 100 )

break;

printf( "Error input!Please try again.\n" );

printf( "Please input the degree of triangle:" );

}

size = degree * (1 + degree ) / 2;

array = (struct triangle *)malloc( size * sizeof(struct triangle) );

printf( "Please input the elements of triangle:\n" );

for( i = 0; i < size; i ++ )

{

scanf( "%d", &array[i].element );

array[i].sum = array[i].element;

}

result = Count( array , degree , size );

free(array);

printf( "The maximum is %d\n", result );

}

[此贴子已经被作者于2007-6-14 20:03:03编辑过]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值