csp 小明种苹果

题目链接

http://118.190.20.162/view.page?gpid=T94

//小明种树
#include 
#include 
#include 
using namespace std;
const int n = 10001; //注意数据量的边界值 
const int m = 10001;
int a[n][m]; //代表输入数据即每个果树苹果总个数和疏果个数 
struct shuG //果树 
{
	int num; //疏果个数 
	int s; //s为果树编号从1开始 
};
struct shuG shug[n];
void bubble1(struct shuG shug[], int N); //比较疏果数目 
void cmp (struct shuG shug[], int N);//比较编号大小 
int main()
{
	int N, M; //N为苹果棵数M为疏果轮数 
	int T = 0;
	int k = 0;
	int p = 0;
	int i, j;
	//T为最后疏果后所有果树苹果总数,
	//k为疏果个数最多的苹果编号 
	//p为果树疏果个数 
	cin >> N >>M;
	//输入数据 
	 
	for( i = 0; i < N; i++) 
	{
		for(j = 0; j < M + 1; j++)
		{
		 	cin >> a[i][j];
		 	T += a[i][j]; 
		} 
	} 
	
	for( i = 0; i < N; i++)  //果树棵数 
	{
		for(j = 1; j < M + 1; j++)// 疏果次数 
		{
			shug[i].num += abs(a[i][j]);//每颗果树疏果的数目 
			shug[i].s = i + 1; //每颗果树编号 
		} 
	} 
	//排序 

	
	bubble1(shug, N); //排每棵树疏果数目 
	cmp (shug, N); //排疏果数目相同时的果树编号 
	k = shug[N-1].s;
	p = shug[N-1].num;    
	cout << T << " " << k << " " << p; 
	return 0;
}

void bubble1(struct shuG shug[], int N) //按num疏果数排序 
{
	int flag = 0;
	int temp1,temp2;
	for(int i = 0; i < N - 1; i++)
	{
		for(int j = 0; j < N - i - 1; j++)
		{
			if(shug[j].num > shug[j+1].num)
			{
				temp1 = shug[j].num;
				shug[j].num = shug[j+1].num;
				shug[j+1].num = temp1;
				temp2 = shug[j].s;
				shug[j].s= shug[j+1].s;
				shug[j+1].s = temp2;
				flag = 1;
			}
		}
		if(flag == 0)
			break;
	
	}	
} 

void cmp (struct shuG shug[], int N)
{
	int flag = 0;
	int temp;
	for(int i = 0; i < N - 1; i++) //趟数 
	{
		for(int j = 0; j < N - i -1; j++)
		{
			if(shug[j].num == shug[j+1].num)
			{
				//只排序s果树编号 
				if(shug[j].s < shug[j+1].s) 
				{
					temp = shug[j].s;
					shug[j].s = shug[j+1].s;
					shug[j+1].s = temp;
					flag == 1;
				}
			}
		}
		if(flag == 0)
		{
			break;
		}
	}	
} 

以上是个人代码,感觉比较臃肿复杂,水平有限只写到此。

下面是自己默写一个大佬的代码比较简洁

//自写模仿
#include
#include 
using  namespace std;
int a[1001][1001];
int main()
{
    int N,M;
    cin >> N >> M;
    //输入数据

    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M + 1; j++)
        {
            cin >> a[i][j];
        }
    }
    
    //处理数据
    int temp = 0;
    //int  sum = 0;
    int  p = 0;
    int  k = 0;
    int T = 0;;
    for(int i = 0; i < N; i++)
    {
         temp = a[i][0]; //每颗果树初始苹果数
        int sum = 0; //每次初始每颗树的疏果数为0;
        for(int j = 1; j < M + 1; j++)
        {
            sum += abs(a[i][j]); //每颗果树疏果个数
        }
        if(p < sum)
        {
            p = sum;
            k = i + 1;
        }
        else if(p == sum)
        {
            if(k > i + 1)
            {
                k = i + 1;
            }
        }
        T += (temp - sum);    
    }
    cout << T <<" " << k << " " << p;
    return 0;
}

 

 

你可能感兴趣的:(ccf)