当前位置:网站首页>Solve "sub number integer", "jump happily", "turn on the light"
Solve "sub number integer", "jump happily", "turn on the light"
2022-07-02 13:29:00 【Pandaoxi】
List of articles
A whole number of children
Title Description
For a five digit number a 1 a 2 a 3 a 4 a 5 a_1a_2a_3a_4a_5 a1a2a3a4a5, It can be divided into three subnumbers :
s u b 1 = a 1 a 2 a 3 sub_1=a_1a_2a_3 sub1=a1a2a3
s u b 2 = a 2 a 3 a 4 sub_2=a_2a_3a_4 sub2=a2a3a4
s u b 3 = a 3 a 4 a 5 sub_3=a_3a_4a_5 sub3=a3a4a5
for example , Five figures 20207 20207 20207 Can be broken down into
s u b 1 = 202 sub_1=202 sub1=202
s u b 2 = 020 ( = 20 ) sub_2=020(=20) sub2=020(=20)
s u b 3 = 207 sub_3=207 sub3=207
Now let's give a positive integer K K K, Ask you to program out 10000 10000 10000 To 30000 30000 30000 All five digits that meet the following conditions , The condition is that three of these five digits s u b 1 , s u b 2 , s u b 3 sub_1,sub_2,sub_3 sub1,sub2,sub3 Can be K K K to be divisible by .
Input format
A positive integer K
Output format
A five digit number for each action , Require output from small to large . Do not repeat output or omit . If there is no solution , The output “No”.
Examples #1
The sample input #1
15
Sample output #1
22555
25555
28555
30000
Tips
0 < K < 1000 0<K<1000 0<K<1000
Answer key
// Author:PanDaoxi
#include <iostream>
using namespace std;
bool f(char s[],int k){
// Write a function to judge whether it can be divided after cutting
int sub[4]={
};
sub[0]=(s[0]-'0')*100+(s[1]-'0')*10+(s[2]-'0'),
sub[1]=(s[1]-'0')*100+(s[2]-'0')*10+(s[3]-'0'),
sub[2]=(s[2]-'0')*100+(s[3]-'0')*10+(s[4]-'0');
// cout<<sub[0]<<" "<<sub[1]<<" "<<sub[2]<<endl;
// Return results
return (sub[0]%k==0&&sub[1]%k==0&&sub[2]%k==0)?true:false;
}
int main(){
int k,a[30001],n=0;
cin>>k;
for(int i=10000;i<=30000;i++){
// It was written incorrectly , But I'm too lazy to change , Direct conversion string nested function
char s[7]={
};
s[0]=i/10000+'0',
s[1]=(i%10000)/1000+'0';
s[2]=(i%1000)/100+'0';
s[3]=(i%100)/10+'0';
s[4]=i%10+'0';
if(f(s,k)) a[n++]=i; // Stored in the array
}
if(n==0){
// unsolvable
cout<<"No";
return 0;
}
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
return 0;
}
Happy jump
Title Description
One n n n An integer array of elements , If the absolute value of the difference between two consecutive elements of the array includes [ 1 , n − 1 ] [1,n-1] [1,n−1] Between all the integers , It is called consistent with “ Happy jump ”, Such as arrays 1423 1 4 2 3 1423 accord with “ Happy jump ”, Because the absolute values of the difference are : 3 , 2 , 1 3,2,1 3,2,1.
Given an array , Your task is to determine whether the array matches “ Happy jump ”.
Input format
The first row of each set of test data is represented by an integer n ( 1 ≤ n ≤ 1000 ) n(1 \le n \le 1000) n(1≤n≤1000) Start , Next n n n A space separated in [ − 1 0 8 -10^8 −108, 1 0 8 10^8 108] Integer between .
Output format
For each group of test data , Output a line if the array matches “ Happy jump ” The output "Jolly", Otherwise output "Not jolly".
Examples #1
The sample input #1
4 1 4 2 3
Sample output #1
Jolly
Examples #2
The sample input #2
5 1 4 2 -1 6
Sample output #2
Not jolly
Tips
1 ≤ n ≤ 1000 1 \le n \le 1000 1≤n≤1000
Answer key
// Author:PanDaoxi
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n,a[1001];
bool flag=false;
cin>>n;
// Read in the data
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n-1;i++){
// According to the meaning of the title , Write the formula
int t=abs(a[i]-a[i+1]);
if(1<=t&&t<=n) flag=true;
else flag=false; // Do not conform to the
}
if(flag==true) cout<<"Jolly";
else cout<<"not Jolly";
return 0;
}
turn on the light
Title Description
On an infinitely long road , There is an endless line of street lights , The number is 1 , 2 , 3 , 4 , … 1,2,3,4,… 1,2,3,4,….
There are only two possible states for each lamp , On or off . If you press the switch of a lamp , Then the state of this lamp will change . If it turns out to be on , It's going to turn off . If it turns out to be off , It's going to turn on .
At the beginning , All the lights are off . Xiaoming can perform the following operations every time :
Specify two numbers , a , t a,t a,t( a a a It's a real number , t t t As a positive integer ). Will be numbered as [ a ] , [ 2 × a ] , [ 3 × a ] , … , [ t × a ] [a],[2 \times a],[3 \times a],…,[t \times a] [a],[2×a],[3×a],…,[t×a] Once for each of the lights . among [ k ] [k] [k] For real numbers k k k The integral part of .
In Xiaoming n n n After the first operation , Xiaoming suddenly finds out , Only one light is on at this time , Xiao Ming would like to know the number of this lamp , But this lamp is too far away from Xiaoming , Xiao Ming can't see the number .
fortunately , Xiaoming still remembers the previous n n n operations . So Xiaoming finds you , Can you help him figure out the number of the light on ?
Input format
First line a positive integer n n n, Express n n n operations .
Next there is n n n That's ok , Two numbers per line , a i , t i a_i,t_i ai,ti. among a i a_i ai Is the set of real Numbers , There must be... After the decimal point 6 6 6 position , t i t_i ti It's a positive integer. .
Output format
Just a positive integer , The number of the light on .
Examples #1
The sample input #1
3
1.618034 13
2.618034 7
1.000000 21
Sample output #1
20
Tips
remember T = t 1 + t 2 + t 3 + … + t n T=t_1+t_2+t_3+…+t_n T=t1+t2+t3+…+tn.
about 30 % 30\% 30% The data of , Satisfy T ≤ 1000 T \le 1000 T≤1000
about 80 % 80\% 80% The data of , Satisfy T ≤ 200000 T \le 200000 T≤200000
about 100 % 100\% 100% The data of , Satisfy T ≤ 2000000 T \le 2000000 T≤2000000
about 100 % 100\% 100% The data of , Satisfy n ≤ 5000 , 1 ≤ a i < 1000 , 1 ≤ t i ≤ T n \le 5000,1 \le a_i<1000,1 \le t_i \le T n≤5000,1≤ai<1000,1≤ti≤T
Data assurance , after n n n After the first operation , There is and only one lamp is on , There's no need to make a mistake . And for all the i i i Come on , t i × a i t_i\times a_i ti×ai The maximum value of is not more than 2000000.
Answer key
// Author:PanDaoxi
#include <iostream>
using namespace std;
int main(){
int n;
// Write a Boolean array to store data , And less space is used
bool flag[20000001]={
};
cin>>n;
for(int i=1;i<=n;i++){
// Enter a real number a and Integers t
double a;
int t;
cin>>a>>t;
// Core processing , Turn off the street lights and turn them on , Turn it on turn it off
for(int j=1;j<=t;j++){
// A mathematical principle
flag[int(j*a)]-=1;
flag[int(j*a)]*=-1;
}
}
// Output results
for(int i=1;i<20000001;i++){
if(flag[i]){
cout<<i;
return 0;
}
}
return 0;
}
The mathematical principles mentioned above , I mean, put 1 become 0, hold 0 And then become 1 This matter . If you write directly if
It will time out .
I first thought of using absolute value , Look, here is a 0, It decreases 1 Just change -1 了 ,-1 The absolute value of is 1;1 subtract 1 become 0,0 The absolute value of is still 0. So it changed .
Further simplification , Because subtract 1 In the future, non positive numbers , therefore , The absolute value of a negative number is its opposite , We can express it as -1 The opposite number of is 1, And it stipulates 0 The opposite number of is 0. therefore , We have :
x-=1;
// x=-x;
x*=-1;
边栏推荐
- 解答:EasyDSS视频点播时音频是否可以设置为默认开启?
- Web Foundation
- 记忆函数的性能优化
- [opencv learning] [image pyramid]
- Daily question: 1175 Prime permutation
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- 【笔耕不辍勋章活动】生命不止,写作不息
- MAC (MacOS Monterey 12.2 M1) personal use PHP development
- How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
- Ruby: how to copy variables without pointing to the same object- Ruby: how can I copy a variable without pointing to the same object?
猜你喜欢
What are eNB, EPC and PGW?
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
Unity SKFramework框架(十二)、Score 计分模块
Bridge of undirected graph
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
上海交大教授:何援军——包围盒(包容体/包围盒子)
de4000h存储安装配置
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Unity skframework framework (XV), singleton singleton
net share
随机推荐
Daily question: 1175 Prime permutation
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
[opencv learning] [contour detection]
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
[opencv learning] [moving object detection]
Unity SKFramework框架(十二)、Score 计分模块
mac(macos Monterey12.2 m1) 个人使用php开发
Ali on three sides, it's really difficult to successfully get the offer rated P7
Explanation of 34 common terms on the Internet
Web Foundation
D为何链接不了dll
[cloud native database] what to do when encountering slow SQL (Part 1)?
Unity skframework framework (XXI), texture filter map resource filtering tool
Lucky numbers in the [leetcode daily question] matrix
Everyone wants to eat a broken buffet. It's almost cold
numpy数组计算
【OpenGL】笔记二十九、高级光照(镜面高光)
[opencv] [image gradient]
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?