CCF小明种苹果(续) C++ and python

#include<iostream>
using namespace std;
 long long  a[10000];
 long long  b[10000];
 long long  c[10000];
 int main(){
     int n;
     cin>>n;
  long long x,k;
  long long sum=0;
  long long num=0,count=0,D=0;
  for(int i=0;i<n;i++){
	 cin>>x;
	 for(int j=1;j<=x;j++)
	 {
	     cin>>k;
	     a[j]=k;
	     sum=sum+a[j];
	     if(a[j]>0&&j!=1){
			
		sum=sum-a[j];
		if(sum>a[j])
		{
		  count++;
		  c[i]=count;
		  count=0;
				}//记录苹果掉落的棵树,把这个写在前面是因为,放在后面的话,苹果树的数量会改变,先记录下来
		
			}
		if(a[j]>0&&j!=1){
			
		sum=0;
		sum=sum+a[j];
				
		
			}//计算最后所剩苹果的数量
		
		 }
		
         b[i]=sum;
         num=num+b[i];
         sum=0;
         D=c[i]+D;
	 }	
	int e =0;
	for(int i=0;i<n;i++)
	{
		if(i==0){
			if((c[0] * c[1] * c[n-1]) != 0)
			e++;
		}
		else if(i==n-1){
			if((c[n-2] * c[n-1] * c[0]) != 0)
			e++;
		}else{
			if((c[i] * c[i-1] * c[i+1]) != 0)
			e++;
		}
	}//计算三棵连续的掉落苹果树的组数

	cout<<num<<" "<<D<<" "<<e;
    return 0;
}

再刷CCF的题目,这道题是小模拟,我自己刷题很少,也是做了2个小时才做出来,速度太慢,加油,希望可以考过200分

下面是我用python写的

n = list(map(int, input().split()))
M = []
Q = []
while n != 0:
    s = list(map(int, input().split()))
    h = sum(s)
    M.append(h)
    s.remove(s[0])
    p = abs(sum(s))
    Q.append(p)
    n = n - 1
h1 = sum(M)
m1 = max(Q)
d = {}
for i in range(len(Q)):
    d[i] = Q[i]
L = list(d.items())
L.sort(key=lambda x : x[1], reverse=True)
print(h1, L[0][0] + 1, m1)

代码其实是有点冗余,我这里用字典来存的树的标号,然后在把值排序,输出最后的标号





 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值