试题编号: 201909-2
试题名称: 小明种苹果(续)
时间限制: 1.0s
内存限制: 512.0MB
题目解析:
明确一点,他可能在中间查看,不止查看一次,所以使用整型t的话,值不一定是1 。
代码:
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int main(){
int n , m , total , method;
vector<int> vc;
while(cin >> n){
int sum = 0;
for(int i = 0 ; i < n ; i++){
int t = 0;
cin >> m >> total; //本行数目 ,开始的总数
int rest = total;
int temp = 0;
for(int j = 0 ; j < m - 1 ; j++){
cin >> method;
if(method <= 0){
rest += method;
}else{
if(rest > method){
rest = method; //重新查看的总数
t++; //是否掉落,掉落肯定 t>0
}
}
}
vc.push_back(t); //为了进行比较,找到e,e是连续三棵掉落
sum += rest; //总数
}
int e = 0;
int t = 0; //与上面的t不同,这个是掉落棵树,上面是一棵树,在查看中掉落几次
for(int i = 0; i < vc.size(); i++){
if(vc[i] != 0){
t++;
}
int two = (i + 1) % n;
int three = (i + 2) % n;
if(vc[i] != 0 && vc[two] != 0 && vc[three] != 0){
e++;
}
}
cout << sum << " " << t << " " << e << endl;
vc.clear();
}
return 0;
}
/*
5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0
4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
*/
/* 第二次
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<fstream>
//#include<regex>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n , m , total , number;
vector<int> vc;
while(cin >> n){
int rst = 0;
while(n--){
cin >> m >> total;
bool flag = false;
for(int i = 1 ; i < m ; i++){
cin >> number;
if(number <= 0){
total += number;
}else{
if(total > number){
flag = true;
}
total = number;
}
}
rst += total;
if(flag){
vc.push_back(1);
}else{
vc.push_back(0);
}
}
int d = 0 , e = 0 , count = vc.size();
for(int i = 0 ; i < vc.size() ; i++){
if(vc[i] == 1){
d++;
}
if(vc[i % count] == 1 && vc[(i+1) % count] == 1 && vc[(i+2) % count] == 1){
e++;
}
}
cout << rst << " " << d << " " << e << endl;
vc.clear();
}
return 0;
}
*/