当前位置:网站首页>题解《子数整数》、《欢乐地跳》、《开灯》
题解《子数整数》、《欢乐地跳》、《开灯》
2022-07-02 10:09:00 【潘道熹】
文章目录
子数整数
题目描述
对于一个五位数 a 1 a 2 a 3 a 4 a 5 a_1a_2a_3a_4a_5 a1a2a3a4a5,可将其拆分为三个子数:
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
例如,五位数 20207 20207 20207可以拆分成
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
现在给定一个正整数 K K K,要求你编程求出 10000 10000 10000到 30000 30000 30000之间所有满足下述条件的五位数,条件是这些五位数的三个子数 s u b 1 , s u b 2 , s u b 3 sub_1,sub_2,sub_3 sub1,sub2,sub3都可被 K K K整除。
输入格式
一个正整数K
输出格式
每一行为一个满足条件的五位数,要求从小到大输出。不得重复输出或遗漏。如果无解,则输出“No”。
样例 #1
样例输入 #1
15
样例输出 #1
22555
25555
28555
30000
提示
0 < K < 1000 0<K<1000 0<K<1000
题解
// Author:PanDaoxi
#include <iostream>
using namespace std;
bool f(char s[],int k){
// 写个函数判断一下切开后是否能被整除
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 (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++){
// 当初写得不对,但是懒得改了,直接转字符串套函数
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; // 储存到数组里面
}
if(n==0){
// 无解
cout<<"No";
return 0;
}
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
return 0;
}
欢乐的跳
题目描述
一个 n n n个元素的整数数组,如果数组两个连续元素之间差的绝对值包括了 [ 1 , n − 1 ] [1,n-1] [1,n−1]之间的所有整数,则称之符合“欢乐的跳”,如数组 1423 1 4 2 3 1423符合“欢乐的跳”,因为差的绝对值分别为: 3 , 2 , 1 3,2,1 3,2,1。
给定一个数组,你的任务是判断该数组是否符合“欢乐的跳”。
输入格式
每组测试数据第一行以一个整数 n ( 1 ≤ n ≤ 1000 ) n(1 \le n \le 1000) n(1≤n≤1000)开始,接下来 n n n个空格隔开的在[ − 1 0 8 -10^8 −108, 1 0 8 10^8 108]之间的整数。
输出格式
对于每组测试数据,输出一行若该数组符合“欢乐的跳”则输出"Jolly",否则输出"Not jolly"。
样例 #1
样例输入 #1
4 1 4 2 3
样例输出 #1
Jolly
样例 #2
样例输入 #2
5 1 4 2 -1 6
样例输出 #2
Not jolly
提示
1 ≤ n ≤ 1000 1 \le n \le 1000 1≤n≤1000
题解
// Author:PanDaoxi
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n,a[1001];
bool flag=false;
cin>>n;
// 读入数据
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n-1;i++){
// 依据题意,写出式子
int t=abs(a[i]-a[i+1]);
if(1<=t&&t<=n) flag=true;
else flag=false; // 不符合
}
if(flag==true) cout<<"Jolly";
else cout<<"not Jolly";
return 0;
}
开灯
题目描述
在一条无限长的路上,有一排无限长的路灯,编号为 1 , 2 , 3 , 4 , … 1,2,3,4,… 1,2,3,4,…。
每一盏灯只有两种可能的状态,开或者关。如果按一下某一盏灯的开关,那么这盏灯的状态将发生改变。如果原来是开,将变成关。如果原来是关,将变成开。
在刚开始的时候,所有的灯都是关的。小明每次可以进行如下的操作:
指定两个数, a , t a,t a,t( a a a为实数, t t t为正整数)。将编号为 [ a ] , [ 2 × a ] , [ 3 × a ] , … , [ t × a ] [a],[2 \times a],[3 \times a],…,[t \times a] [a],[2×a],[3×a],…,[t×a]的灯的开关各按一次。其中 [ k ] [k] [k]表示实数 k k k的整数部分。
在小明进行了 n n n次操作后,小明突然发现,这个时候只有一盏灯是开的,小明很想知道这盏灯的编号,可是这盏灯离小明太远了,小明看不清编号是多少。
幸好,小明还记得之前的 n n n次操作。于是小明找到了你,你能帮他计算出这盏开着的灯的编号吗?
输入格式
第一行一个正整数 n n n,表示 n n n次操作。
接下来有 n n n行,每行两个数, a i , t i a_i,t_i ai,ti。其中 a i a_i ai是实数,小数点后一定有 6 6 6位, t i t_i ti是正整数。
输出格式
仅一个正整数,那盏开着的灯的编号。
样例 #1
样例输入 #1
3
1.618034 13
2.618034 7
1.000000 21
样例输出 #1
20
提示
记 T = t 1 + t 2 + t 3 + … + t n T=t_1+t_2+t_3+…+t_n T=t1+t2+t3+…+tn。
对于 30 % 30\% 30%的数据,满足 T ≤ 1000 T \le 1000 T≤1000
对于 80 % 80\% 80%的数据,满足 T ≤ 200000 T \le 200000 T≤200000
对于 100 % 100\% 100%的数据,满足 T ≤ 2000000 T \le 2000000 T≤2000000
对于 100 % 100\% 100%的数据,满足 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
数据保证,在经过 n n n次操作后,有且只有一盏灯是开的,不必判错。而且对于所有的 i i i 来说, t i × a i t_i\times a_i ti×ai 的最大值不超过 2000000。
题解
// Author:PanDaoxi
#include <iostream>
using namespace std;
int main(){
int n;
// 写个布尔数组储存数据,而且空间用得少
bool flag[20000001]={
};
cin>>n;
for(int i=1;i<=n;i++){
// 输入实数a 和 整数t
double a;
int t;
cin>>a>>t;
// 核心处理,把路灯关的打开,开的关掉
for(int j=1;j<=t;j++){
// 一个数学原理
flag[int(j*a)]-=1;
flag[int(j*a)]*=-1;
}
}
// 输出结果
for(int i=1;i<20000001;i++){
if(flag[i]){
cout<<i;
return 0;
}
}
return 0;
}
上面提到的数学原理,是说把1变成0,把0再变成1这件事。如果直接写if
的话会超时。
我最开始想到的是利用绝对值嘛,你看这里有个0,它减1就变-1了,-1的绝对值是1;1减去1变成0,0的绝对值还是0。这样就变化了。
再进一步简化,因为减去1以后非正数了,所以,负数的绝对值是它的相反数,我们就可以表示为-1的相反数为1,而且规定了0的相反数是0。所以,我们有:
x-=1;
// x=-x;
x*=-1;
边栏推荐
- Web Foundation
- Answer: can the audio be set to on by default during easydss video on demand?
- numpy数组计算
- Jerry's watch stops ringing [article]
- Ali on three sides, it's really difficult to successfully get the offer rated P7
- Everyone wants to eat a broken buffet. It's almost cold
- Fundamentals of face recognition (facenet)
- VIM super practical guide collection of this one is enough
- [200 opencv routines] 100 Adaptive local noise reduction filter
- Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
猜你喜欢
Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
MAC (MacOS Monterey 12.2 M1) personal use PHP development
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
Jerry's watch ringtone audition [article]
Jerry's watch delete alarm clock [chapter]
OpenApi-Generator:简化RESTful API开发流程
Unity skframework framework (XVI), package manager development kit Manager
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
随机推荐
[Unity]使用GB2312,打包后程序不正常解决方案
Jerry's watch time synchronization [chapter]
Bridge of undirected graph
[opencv learning] [template matching]
Security RememberMe原理分析
自主可控三维云CAD:CrownCAD赋能企业创新设计
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
(6) Web security | penetration test | network security encryption and decryption ciphertext related features, with super encryption and decryption software
Unity skframework framework (XXI), texture filter map resource filtering tool
[opencv learning] [image histogram and equalization]
2022 zero code / low code development white paper [produced by partner cloud] with download
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
Sensor adxl335bcpz-rl7 3-axis accelerometer complies with rohs/weee
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
【youcans 的图像处理学习课】总目录
net share
日本赌国运:Web3.0 ,反正也不是第一次失败了!
Unity SKFramework框架(十五)、Singleton 单例
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
Unity SKFramework框架(十三)、Question 问题模块