9.26课测题目解析+总结
题目解析+总结
P8598
分析题干 + 步骤书写
这道题可以使用桶,它可以记录每一个数字出现的次数。
在输入的时候,标记每一个数字出现的次数,记录出现次数大于的数字。
接着开始遍历整个桶。若出现上一个数比遍历到的数的差大于时,则出现了漏号的情况,记录下来,并打断程序。
最后输出记录下来的两个数。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int DH,CH,n,id = -1;
map<int,int>vis;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i){
int x;
while(cin>>x){
if(vis[x] >= 1) CH = x;
vis[x]++;
}
}
for(auto x:vis){
if(x.first - id > 1 && id != -1){
DH = x.first - 1;
break;
}
id = x.first;
}
cout<<DH<<' '<<CH<<endl;
return 0;
}题目小结
桶
P12156
分析题干 + 步骤书写
若可以将这个数组分成两段且每段的异或和相等时,说明整个数组的异或和等于。所以,直接判断这个数组的异或和是否等于就可以了。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[1003];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin>>t;
while(t--){
cin>>n;
int sum = 0;
while(n--){
int x; cin>>x;
sum = sum ^ x;
}
if(sum == 0) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}题目小结
位运算
P8738
分析题干 + 步骤书写
根据计算,得出公元年是甲子年。因为天干地支每60年轮回一次,所以得出公元年也是甲子年。
接着以甲子年(年)为基准,计算出每一年的天干地支是什么。
注意:还要考虑减为负数的情况!!!
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
string TG[10] = {"jia","yi","bing","ding","wu","ji","geng","xin","ren","gui"};
string DZ[12] = {"zi","chou","yin","mao","chen","si","wu","wei","shen","you","xu","hai"};
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
n += 56;
cout<<TG[n%10]<<DZ[n%12]<<endl;
return 0;
}题目小结
数学
P3856
分析题干 + 步骤书写
在运行之前,提前将每一个小时、每一个分钟都各有多少秒,便于后面的计算。
在输入之后,先计算出一天有多少秒,再计算出每一个时刻对应着第几秒,最后求出两个时刻的差。
若出现终点时刻比起点时刻要早一些,则加上一天的时间,在进行计算。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int m[100005],h[100005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
for(int i=0;i<=100000;++i) m[i] = i*10+1;
h[0] = 1;
for(int i=1;i<=100000;++i) h[i] = h[i-1] + m[i];
int t; cin>>t;
while(t--){
int n,a,b,c,e,f,g,tot = 0;
cin>>n>>a>>b>>c>>e>>f>>g;
for(int i=0;i<n;++i) tot += h[i];
int sum1 = 0,sum2 = 0;
for(int i=0;i<a;++i) sum1 += h[i];
for(int i=0;i<b;++i) sum1 += m[i];
sum1 += c+1;
for(int i=0;i<e;++i) sum2 += h[i];
for(int i=0;i<f;++i) sum2 += m[i];
sum2 += g+1;
if(sum2 > sum1) cout<<sum2-sum1<<endl;
else cout<<tot-sum1+sum2<<endl;
}
return 0;
}题目小结
数学
P7036
分析题干 + 步骤书写
要将一个长宽为、的长方形折叠成一个长宽为、的小长方形,我们可以直接考虑最好情况:
- 若,则只需一步完成;
- 否则
注意:正方形纸是可以旋转的,所以我们要考虑两种情况。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int ans = INT_MAX;
int cls(int W,int H,int w,int h){
int cnt = 0;
if(W < w || H < h){
return INT_MAX;
}
while(W > w){
W = (W+1)/2;
cnt++;
}
while(H > h){
H = (H+1)/2;
cnt++;
}
return cnt;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int W,H,w,h;
cin>>W>>H>>w>>h;
ans = min(ans,cls(W,H,w,h));
ans = min(ans,cls(W,H,h,w));
if(ans == INT_MAX) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}题目小结
数学 + 模拟
P9632
分析题干 + 步骤书写
要制造一个长度为且仅有~的数字,且有个数满足,我们可以这样想:相邻的两个数互质。
我们将下标为~向右移动一位,即:
这样,我们就制造出了一个满足以上条件的数组,其他几个数不用满足上面的条件了,直接原地不动。
注:若,则无解。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[1000005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,k;
cin>>n>>k;
if(k == 0){
cout<<-1<<endl;
return 0;
}
for(int i=2;i<=k;++i) a[i] = i-1;
a[1] = k;
for(int i=k+1;i<=n;++i) a[i] = i;
for(int i=1;i<=n;++i) cout<<a[i]<<' ';
return 0;
}题目小结
模拟