当前位置:网站首页>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
边栏推荐
- 通过修改style设置打印页样式
- Réflexions sur la sécurité des données (réimpression)
- 模拟卷Leetcode【普通】1219. 黄金矿工
- Avtiviti创建表时报错:Error getting a new connection. Cause: org.apache.commons.dbcp.SQLNestedException
- [C language] qsort function
- 数据库隔离级别
- 基于JEECG-BOOT制作“左树右表”交互页面
- [mqtt from getting started to improving series | 01] quickly build an mqtt test environment from 0 to 1
- selenium源码通读·9 |DesiredCapabilities类分析
- Detailed explanation of P problem, NP problem, NPC problem and NP hard problem
猜你喜欢
联合索引的左匹配原则
Data type of MySQL
【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
LeetCode 732. 我的日程安排表 III
MFC关于长字符串unsigned char与CString转换及显示问题
Redis core technology and basic architecture of actual combat: what does a key value database contain?
黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
JWT-JSON WEB TOKEN
Caused by:org. gradle. api. internal. plugins . PluginApplicationException: Failed to apply plugin
mysql按照首字母排序
随机推荐
联合索引的左匹配原则
Technology sharing | common interface protocol analysis
MFC on the conversion and display of long string unsigned char and CString
模拟卷Leetcode【普通】1109. 航班预订统计
Database - current read and snapshot read
D - How Many Answers Are Wrong
JMeter做接口测试,如何提取登录Cookie
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
LeetCode 739. 每日温度
Black cat takes you to learn UFS protocol Chapter 18: how UFS configures logical units (Lu Management)
基于JEECG-BOOT制作“左树右表”交互页面
Career advancement Guide: recommended books for people in big factories
使用Nacos管理配置
[eolink] PC client installation
Database isolation level
[mqtt from getting started to improving series | 01] quickly build an mqtt test environment from 0 to 1
[postman] collections - run the imported data file of the configuration
B - The Suspects
Mise en œuvre d’une fonction complexe d’ajout, de suppression et de modification basée sur jeecg - boot
浅谈专项测试之弱网络测试