2313 Problem B 魂之輓歌

1. 分情況討論,求出乘上之後能得到的最大值。排序後加前m個。


#include <stdio.h>

#define MAXM 50

int main()
{
	int n;
	int m;
	int k;
	int mo[MAXM+1];
	int i;
	int j;
	int t;
	int sum;
	while (~scanf("%d%d%d", &n, &m, &k))
	{
		sum = 0;
		for (i = 0;i < n; i++)
		{
			scanf("%d", &mo[i]);
		}
		for (i = 0;i < n-1;i++)
		{
			for (j = 0;j < n-1; j++)
			{
				if( mo[j] < mo[j+1])
				{
					t = mo[j];
					mo[j] = mo[j+1];
					mo[j+1] = t;
				}
			}
		}
		if(k > 0&&mo[0] > 0)
		{
			mo[0]*=k;
		}
		else
		{
			mo[n-1]*=k;
		}
		for (i = 0;i < n-1;i++)
		{
			for (j = 0;j < n-1; j++)
			{
				if( mo[j] < mo[j+1])
				{
					t = mo[j];
					mo[j] = mo[j+1];
					mo[j+1] = t;
				}
			}
		}
		for (i = 0; i < m; i++)
		{
			sum+=mo[i];
		}
		printf("%d\n", sum);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章