当前位置:网站首页>Luogu p2141 abacus mental arithmetic test
Luogu p2141 abacus mental arithmetic test
2022-07-06 06:24:00 【zjsru_ Beginner】
Title Description
Abacus mental calculation is a kind of calculation technology that can complete fast calculation by simulating the change of abacus in the brain . Abacus mental arithmetic training , Can develop intelligence , It can bring a lot of convenience to our daily life , So it's popularized in many schools .
Some school's abacus mental arithmetic teacher uses a kind of quick examination abacus mental arithmetic addition ability test method . He randomly generates a set of positive integers , The numbers in the set are different , Then ask the students to answer : How many of them , Exactly equal to the other two in the set ( Different ) Sum of the numbers ?
Recently, the teacher gave some test questions , Please help me find out .
Input format
There are two lines , The first line contains an integer n, The number of positive integers given in the test .
The second line has n A positive integer , Every two positive integers are separated by a space , A positive integer given in a test .
Output format
An integer , The answer to a test question .
I/o sample
Input #1 Copy
4 1 2 3 4
Output #1 Copy
2
explain / Tips
【 Sample explanation 】
from 1+2=3,1+3=4, So the answer to meet the test requirements is 2.
Be careful , The addend and the addend must be two different numbers in the set .
Their thinking :
First sort all numbers , Then calculate the sum of each logarithm . Then use binary search to find whether there is this and... In the sorted array , And judge whether it is the first time , If so, count it as one .
The code is as follows :
#include <iostream>
#include <algorithm>
using namespace std;
int n, a[110],b[110];
int find(int x)
{
int low = 1, high = n,mid;
while (low <= high)
{
mid = (low + high) / 2;
if (a[mid] == x)
{
if (b[mid] == 0)
{
b[mid]++;
return 1;
}
else return 0;
}
else if (a[mid] < x) low = mid + 1;
else if (a[mid] > x) high = mid - 1;
}
return 0;
}
int main()
{
int sum = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n-1; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (find(a[i] + a[j])) sum++;
}
}
cout << sum << endl;
return 0;
}
zbh
边栏推荐
猜你喜欢
Mise en œuvre d’une fonction complexe d’ajout, de suppression et de modification basée sur jeecg - boot
使用Nacos管理配置
D - How Many Answers Are Wrong
win10无法操作(删除、剪切)文件
Delete the variables added to watch1 in keil MDK
全链路压测:构建三大模型
oscp raven2靶机渗透过程
On weak network test of special test
Left matching principle of joint index
JDBC requset corresponding content and function introduction
随机推荐
oscp raven2靶机渗透过程
記一個基於JEECG-BOOT的比較複雜的增删改功能的實現
Database isolation level
LeetCode 1200. 最小绝对差
基于JEECG-BOOT制作“左树右表”交互页面
leaflet 地图
Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
模拟卷Leetcode【普通】1143. 最长公共子序列
sourceInsight中文乱码
Simulation volume leetcode [general] 1062 Longest repeating substring
Isam2 operation process
(中)苹果有开源,但又怎样呢?
二维码的前世今生 与 六大测试点梳理
MFC关于长字符串unsigned char与CString转换及显示问题
Simulation volume leetcode [general] 1447 Simplest fraction
E - food chain
Leaflet map
Construction and integration of Zipkin and sleuth for call chain monitoring
Simulation volume leetcode [general] 1405 Longest happy string
Manhattan distance sum - print diamond