列表

详情


NC212617. EatGrapes

描述

The party began, the greasy uncle was playing cards, the fat otaku was eating, and the little beauty was drawing.

After eating steaks and tasting desserts, the party is coming to an end. The host has prepared many fruits for everyone. SYH tempts to eating grapes, but now another person is eating.

The basic shape of a bunch of grapes is a long chain composed of N small grapes. On this chain, each grape can be regarded as a node. In addition to being connected to the next node, each node may have several grapes attached on it, and the node at the end of the long chain always have a grape connected on. This grape is excluded the number of grapes at the end node.

The two participants take turns to take grapes, SYH takes first. During one move, the participant can take away any positive number of grapes connected to the end node. When all the grapes connected to the end node are taken, the last node is considered to be the grape connected on the penultimate(second last) one, and the total number of nodes is reduced by 1.

For the same reason as in Dessert Time, neither of them want to take the last grape. However, SYH is very smart; he observed the shape of the grapes in advance. If SYH found out that he would lose, he would say "these are sour grapes" and leave early. Otherwise, he would say "these are sweet grapes" and then eat deliciously. Do you know what SYH said?

Given a number N and an array a, the meaning is described as above, you should print the sentence which SYH said.

输入描述

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 10), indicating the number of test cases. For each test case:

The first line contains an integer N (1 ≤ N ≤ ), indicating the number of nodes.

The second line contains N integers, a_1 , a_2 ,……, a_n (0 ≤ a_i), indicating the number of grapes connected to the i-th node.

输出描述

For each test case, each line contains a string, indicating the sentence which SYH said.

示例1

输入:

2
5
2 1 0 0 1
5
1 1 0 1 0

输出:

these are sweet grapes
these are sour grapes

说明:

For the first sample, the figure below is the case of N=5 and a={2,1,0,0,1}.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C(clang11) 解法, 执行用时: 118ms, 内存消耗: 4892K, 提交时间: 2021-01-15 13:36:54

#include<stdio.h>
#include<string.h>
#define maxn 102020
int a[maxn];
int main(){
	int n,m;
	int t;
	int sum;
	scanf("%d",&t);
	while(t--){
		sum=1;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		a[n]++;
		for(int i=n;i>=1;i--){
			if(a[i]>1)break;
			else if(i!=1)sum=1-sum;
		}
		if(sum!=0)printf("these are sweet grapes\n");
		else printf("these are sour grapes\n");
	}
	return 0;
} 

C++14(g++5.4) 解法, 执行用时: 65ms, 内存消耗: 372K, 提交时间: 2020-09-27 20:50:03

#include<stdio.h>
int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		int flag = 0, n , x;
		scanf("%d",&n);
		for(int i = 1; i <= n; i++){
			scanf("%d", &x);
			if(x == 0)
				flag = 1 - flag;
			else
				flag = 1;
		}
		if(flag)
			printf("these are sweet grapes\n");
		else
			printf("these are sour grapes\n");
	}
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 146ms, 内存消耗: 388K, 提交时间: 2020-09-26 16:15:10

#include<bits/stdc++.h>
using namespace std;
int ans,T,n,x;
int main(){
	cin>>T;
	while(T--){
		cin>>n>>x;
		if(x==0)ans=0;
		else ans=1;
		for(int i=1;i<n;i++){
			cin>>x;
			if(x>0)ans=1;
			else ans=1-ans;
		}
		if(ans) cout<<"these are sweet grapes\n";
		else cout<<"these are sour grapes\n";
	}
	return 0;
}

上一题