当前位置:网站首页>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
边栏推荐
- Online and offline problems
- Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
- Remember the implementation of a relatively complex addition, deletion and modification function based on jeecg-boot
- 基于JEECG-BOOT制作“左树右表”交互页面
- MySQL is sorted alphabetically
- 这些年用Keil遇到的坑
- Mise en œuvre d’une fonction complexe d’ajout, de suppression et de modification basée sur jeecg - boot
- 黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
- Isam2 operation process
- 模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
猜你喜欢
Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
数据库-当前读与快照读
oscp raven2靶机渗透过程
[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development
B - The Suspects
The latest 2022 review of "graph classification research"
记一个基于JEECG-BOOT的比较复杂的增删改功能的实现
Remember the implementation of a relatively complex addition, deletion and modification function based on jeecg-boot
使用Nacos管理配置
LeetCode 1200. 最小绝对差
随机推荐
模拟卷Leetcode【普通】1249. 移除无效的括号
Resttemplate and feign realize token transmission
D - How Many Answers Are Wrong
Black cat takes you to learn EMMC Protocol Part 10: EMMC read and write operation details (read & write)
(中)苹果有开源,但又怎样呢?
Win10 cannot operate (delete, cut) files
Understanding of processes and threads
MFC dynamically creates dialog boxes and changes the size and position of controls
[no app push general test plan
org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 ‘outgoing‘ 开头的无效内容
Mise en œuvre d’une fonction complexe d’ajout, de suppression et de modification basée sur jeecg - boot
JWT-JSON WEB TOKEN
Isam2 operation process
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
黑猫带你学UFS协议第4篇:UFS协议栈详解
Avtiviti创建表时报错:Error getting a new connection. Cause: org.apache.commons.dbcp.SQLNestedException
php使用redis实现分布式锁
Left matching principle of joint index
The pit encountered by keil over the years
org. activiti. bpmn. exceptions. XMLException: cvc-complex-type. 2.4. a: Invalid content beginning with element 'outgoing' was found