2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 6 D. Number of strokes

D. Number of strokes

Title link: https://ac.nowcoder.com/acm/contest/9986/D

Title description:

An intelligent robot writes n numbers on the blackboard. It crosses out any two numbers each time, and writes the value of the sum of these two numbers modulo 11 at the end of the number sequence.

Example: 5 6 7 8 9, after crossing out 5 6, the number sequence becomes 7 8 9 0.

What's interesting is that the robot suddenly "strikes" when there are two numbers left. Knowing one of the numbers cnt (cnt >= 11), find the value of the other number.

Enter a description:

Multiple sets of test data, ending with EOF.

For each group of data, enter n and cnt in the first line.

Enter n numbers in the second line, and the i-th number represents num[i].

Output description:

For each group of data, output the value of another number.

Example 1:

Input
6 11
6 7 8 9 10 11
Output
7
Remarks:
2 <= n <= 150000, 1 <= num[i] <= 1e6.

The number of data groups t <= 10, to ensure that the data is legal.

Problem-solving ideas:

The value of the sum %11 is unchanged. After the operation, the new number must be between 0 and 11. If n=2, the remaining number (sum-cnt) is what you want. When n>2, it indicates that the operation is performed, and because the number after the operation is between 0 and 11, it is known from the meaning of the question (cnt >= 11) that cnt has not been operated, and the remaining number %11 must be the same as ( sum-cnt) congruence , so when n>2, the only number that is congruent with (sum-cnt) is what you want.

code show as below:

#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
int main()
{
    
    
	int n,cnt,a;
	while(scanf("%d%d",&n,&cnt)!=EOF)
	{
    
    
		ll sum=0;
		for(int i=0;i<n;i++)
		{
    
    
			scanf("%d",&a);
			sum+=a;
		}
		if(n==2) 
			printf("%lld\n",sum-cnt);
		else 
			printf("%lld\n",(sum-cnt)%11);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/114038152