2014-1 Popular Zhenti Abacus Mental Arithmetic Test

2014-1 Abacus Mental Arithmetic Test
Problem Description
Abacus Mental Arithmetic is a kind of calculation technology that completes fast calculations by simulating changes in the abacus in the brain. Abacus mental arithmetic training can not only develop intelligence, but also bring a lot of convenience to daily life, so it has been popularized in many schools.
An abacus and mental arithmetic teacher in a school uses a test method to quickly examine the addition ability of abacus and mental arithmetic. He randomly generated a set of positive integers with different numbers in the set, and then asked the students to answer: How many numbers in the set are exactly equal to the sum of the other two (different) numbers in the set?
Recently the teacheraskedsome test questions, Please help find the answer.
There are
two lines of input . The first line contains an integer n, which represents the number of positive integers given in the test question.
There are n positive integers in the second line, and every two positive integers are separated by a space to indicate the positive integers given in the test questions.
The Output
output consists of one line and contains an integer, which represents the answer to the test question.
case:

enter
4 1 2 3 4
Output 2

Hint

[Sample description] 1+2=3, 1+3=4, so the answer that meets the test requirements is 2. Note that the addend and the addend must be two different numbers in the set.

【the data shows】

For 100% of the data, 3≤n≤100, and the size of the positive integer given in the test question does not exceed 10,000.
Analysis of Thinking
This question should pay attention to the following details, a+b=c, a!=b, for example, 1+4=5, 2+3=5, these two answers can only be regarded as one method

  1. First define two arrays, a[10001],gve[10001]={0}, the array gov[] is used to mark the sum of the addend and the addend, so as to avoid the situation where one result corresponds to two values ​​( 1+4=5, 2+3=5,)
    code analysis
	scanf("%d",&n);
	for(i=1;i<=n;i++){
    
    
		scanf("%d",&a[i]);
	}

Loop control input (simple)
full code

#include<stdio.h>
int main (){
    
    
	int a[10001],n,covd=0,gve[10001]={
    
    0},j,i,sum;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
    
    
		scanf("%d",&a[i]);
	}
	//三重循环暴力演算法
	for(int i=1;i<=n;i++){
    
    
	
		for(j=i+1;j<=n;j++){
    
    
		
			for(int k=1;k<=n;k++){
    
    
				//核心判断语句,判断下标为k的数组元素是否等于下标为i,j数组元素之和,并判断a[i],a[j]是否相等
				//且数组gov[k]==0,因为当gov!=0时,证明该值已经存在了。
				if(a[k]==a[i]+a[j]&&a[i]!=a[j]&&gve[k]==0){
    
    
					covd++;//covd计数器
					gve[k]=1;//在数组gve中标记k下标下的元素,令它等于1.避免重复
				}
			}
		}
	}
	printf("%d",covd);
	return 0;
}
4
1 2 3 4
2
--------------------------------
Process exited with return value 0
Press any key to continue . . .v

For this question, I used the triple-cycle violent calculus. I think this is very cool, so the time complexity must be too large, so I can continue to optimize.
The question type is not difficult, the important thing is the thought.

Guess you like

Origin blog.csdn.net/qq_52044923/article/details/110319831