当前位置:网站首页>Solution to the 38th weekly match of acwing
Solution to the 38th weekly match of acwing
2022-07-06 20:28:00 【Friend Su】
AcWing 4299. Delete point
Topic link :AcWing 4299. Delete point
The first question is sign in question , In fact, it is to calculate whether there is y Both sides of the shaft , Whether the number of points on one side is less than or equal to 1
The code is as follows :
#include <iostream>
using namespace std;
const int N = 110;
int n, l, r;
int main(){
cin >> n;
for(int i = 0; i < n; i++){
int x, y;
cin >> x >> y;
if(x > 0)r++;
else l++;
}
if(l <= 1 || r <= 1)puts("Yes");
else puts("No");
return 0;
}
AcWing 4300. Two kinds of operations
Topic link :AcWing 4300. Two kinds of operations
This problem is not difficult , As long as you find the right direction , He has only two operations , Immediate subtraction 1 And ride 2, The reverse is equivalent to m There are two operations , That is, by adding 1 And division 2 obtain n, In order to make m Faster approach n, Obviously, we need to divide first , When m Less than n when , We are adding , But in the process ,m May be odd , So we need to judge , When m When bits are odd , It needs to be added 1, To ensure that it can be divided , Finally get n.
Of course, there is another situation , Is that when n > m when , This is a n Only by subtracting 1 The way to get m, So in this case, calculate directly n - m that will do .
The code is as follows :
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, res;
int mid(){
int k = 0;
while(m > n){
// When m <= n Time description m It's close to n And it can be obtained by adding one n 了
if(m % 2){
// Judge m Whether it's odd or not
m++;
res++;//res Used to calculate midway +1 The number of operations
}
m /= 2;
k++;//k Calculate Division 2 The number of operations
}
return k;
}
int main(){
cin >> n >> m;
if (n == m)cout << 0 << endl;// situation 1: n == m
else if(n > m) // situation 2: n > m
cout << n - m << endl;
else{
// situation 3: n < m
int b = m;
int k = mid(); // Because besides 2 The process is a bit like dichotomy , So I named it mid
if(m == n)cout << k + res << endl; // If it is exactly equal to, there is no need to add 1 operation
else if(m < n)cout << (n - m) + k + res << endl; // Otherwise, add the last 1 The operation of
}
return 0;
}
AcWing 4301. Truncated sequence
Topic link :AcWing 4301. Truncated sequence
Although this problem is difficult , But it's not too difficult , The direction is still very easy to think of , That is, different programming methods may encounter some troubles , For example, convert it into numbers and store it in an array , In this way, we have to deal with some special situations , In fact, there is no need to convert , Directly in the form of string , They correspond to ASCLL Code value calculation is the same .
Ideas :
In fact, it is divided into two or more groups and the same size form , We can easily consider , First calculate the sum of them , Then group in the more summative factors , Because you can't divide it casually , The order is the same , So we traverse the string for each factor , If you can form this number , Then you can .
The code is as follows :
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n, sum;
char s[N];
int main(){
cin >> n >> s;
for(int i = 0; i < n; i++){
s[i] -= '0';
sum += s[i];
}
for(int i = 2; i <= n; i++){
if(sum % i == 0){
// Judge if it's a factor
int k = 0;
int p = sum / i;
bool f = true;
// Traverse , See if it can be divided into equal parts
for(int j = 0; j < n; j++){
k += s[j];
// If k Greater than p Then it means that , immediate withdrawal , Tagged false
if(k > p){
f = false;
break;
}
else if(k == p){
// If equal, then return to 0, Restart the calculation
k = 0;
}
}
if(f){
puts("YES");
return 0;
}
}
}
puts("NO");
return 0;
}
边栏推荐
- "Penalty kick" games
- B-杰哥的树(状压树形dp)
- How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
- 棋盘左上角到右下角方案数(2)
- JS implementation force deduction 71 question simplified path
- Recyclerview GridLayout bisects the middle blank area
- [weekly pit] output triangle
- Case ① | host security construction: best practice of 3 levels and 11 capabilities
- Tencent architects first, 2022 Android interview written examination summary
- RT thread I2C tutorial
猜你喜欢
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
New generation garbage collector ZGC
PowerPivot - DAX (first time)
Notes on beagleboneblack
[network planning] Chapter 3 data link layer (3) channel division medium access control
B-杰哥的树(状压树形dp)
电子游戏的核心原理
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
BeagleBoneBlack 上手记
Web security - payload
随机推荐
RT thread I2C tutorial
夏志刚介绍
Force deduction brush question - 98 Validate binary search tree
Tencent architects first, 2022 Android interview written examination summary
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
8086指令码汇总表(表格)
Wechat applet common collection
Recyclerview GridLayout bisects the middle blank area
持续测试(CT)实战经验分享
Tencent byte and other big companies interview real questions summary, Netease architects in-depth explanation of Android Development
2022 portal crane driver registration examination and portal crane driver examination materials
JVM_常见【面试题】
22-07-05 upload of qiniu cloud storage pictures and user avatars
Groovy基础语法整理
Linear distance between two points of cesium
电子游戏的核心原理
Unity makes AB package
Maximum likelihood estimation and cross entropy loss
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
使用ssh连接被拒