当前位置:网站首页>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
边栏推荐
- Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete
- 模拟卷Leetcode【普通】1109. 航班预订统计
- 数据库-当前读与快照读
- 把el-tree选中的数组转换为数组对象
- Isam2 and incrementalfixedlagsmooth instructions in gtsam
- keil MDK中删除添加到watch1中的变量
- [wechat applet] build a development tool environment
- Win10 cannot operate (delete, cut) files
- [postman] collections - run the imported data file of the configuration
- 黑猫带你学UFS协议第18篇:UFS如何配置逻辑单元(LU Management)
猜你喜欢
[no app push general test plan
[mqtt from getting started to improving series | 01] quickly build an mqtt test environment from 0 to 1
[postman] the monitors monitoring API can run periodically
mysql按照首字母排序
Delete the variables added to watch1 in keil MDK
(中)苹果有开源,但又怎样呢?
LeetCode 729. 我的日程安排表 I
Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
[postman] collections configuration running process
随机推荐
org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 ‘outgoing‘ 开头的无效内容
QT: the program input point xxxxx cannot be located in the dynamic link library.
Career advancement Guide: recommended books for people in big factories
SourceInsight Chinese garbled
Private cloud disk deployment
模拟卷Leetcode【普通】1414. 和为 K 的最少斐波那契数字数目
基于JEECG-BOOT的list页面的地址栏参数传递
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
Simulation volume leetcode [general] 1447 Simplest fraction
模拟卷Leetcode【普通】1249. 移除无效的括号
[eolink] PC client installation
全链路压测:构建三大模型
Black cat takes you to learn UFS protocol Chapter 4: detailed explanation of UFS protocol stack
Pat (Grade B) 2022 summer exam
模拟卷Leetcode【普通】1143. 最长公共子序列
JDBC requset corresponding content and function introduction
使用Nacos管理配置
【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境
Manage configuration using Nacos
【无App Push 通用测试方案