一:问题
二:理解
用一个二维数组来存储N行M+1列的数;N行代表苹果树的数量,第一列是蔬果前树上的苹果数,M是蔬果的轮数;
num[i][j]代表的是第 i+1棵树,第j轮蔬果的数量;
输出的三个数,也比较好理解,一个是蔬果后果林总共的苹果数;一个是蔬果最多的那棵树是第几棵树;一个是蔬果最多的那棵树一共蔬了多少果;
三:代码
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int num[10005][10005];
int main()
{
int N, M; //N苹果树的颗数,M蔬果轮数;
cin >> N >> M;
int maxnum = 0, count = 0;
int temp;
for(int i=0; i<N; i++)
for(int j=0; j<=M; j++)
cin >> num[i][j];
for(int i=0; i<N; i++)
{
num[i][M+1] = 0;
for(int j=1; j<=M; j++)
num[i][M+1] += num[i][j];
num[i][M+1] = abs(num[i][M+1]);
if(num[i][M+1] > maxnum)
{
temp = i+1;
maxnum = num[i][M+1];
}
count += (num[i][0] - num[i][M+1]);
}
cout << count << " " << temp << " " << maxnum << endl;
return 0;
}
做第二次的代码:
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int num[1005][1005];
int main()
{
int N, M;
cin >> N >> M;
int count1 = 0;
int maxnum = 0;
int tempi;
for(int i=0; i<N; i++)
{
int tempcount = 0;
cin >> num[i][0];
count1 += num[i][0];
for(int j=1; j<M+1; j++)
{
cin >> num[i][j];
tempcount += num[i][j];
}
count1 += tempcount;
if(abs(tempcount) > maxnum)
{
tempi = i;
maxnum = abs(tempcount);
}
}
cout << count1 << " " << tempi+1 << " " << maxnum;
return 0;
}
做这么多题的感受就是,确实能够想的够全面,再回来做没有这么多的问题了。而且对照两次的代码也可以看出来,精简了不少。
在第一次的时候,读题目都比较费劲,做多了题了,确实感觉这个很简单。