5.29总结
5.29总结
课堂总结
{\color{Red}{P16465}} [GKS 2013 Practice] Moist
题目传送门(比赛)
考察知识点
- 模拟
- 字符串
错误原因
-
### 错误思路 用双重循环进行比较,统计答案 ### 错误代码${\color{Red}{(0分)}}$: :::error[错误代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int t,n,ans; string s[105]; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>t; for(int x=1;x<=t;x++){ ans=0; cin>>n; for(int i=1;i<=n+1;i++){ getline(cin,s[i]); } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n+1;j++){ if(s[j]<s[i]){ swap(s[i],s[j]); ans++; } } } cout<<"Case #"<<x<<": "<<ans<<endl; } return 0; } ``` ::: ### 正确思路 维护当前已扫描的最大字符串,从第二张牌开始,若当前牌小于最大字符串则花费$+1$,否则更新最大字符串为当前牌 ### 正确代码${\color{Red}{(10分)}}$: :::success[正确代码] ```cpp #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int t,n,ans; string s,st; signed main(){ cin>>t; for(int x=1;x<=t;x++){ ans=0; cin>>n; getchar();//吸收换行符 for(int i=1;i<=n;i++){ getline(cin,s); if(i==1)st=s;//第一张牌作为初始最大值 else{ if(s<st)ans++;//比当前最大值小:移动(花费+1) else st=s;//更新最大值 } } cout<<"Case #"<<x<<": "<<ans<<endl; } return 0; } ``` ::: ### ${\color{Orange}{P16220}}$ [ECUSTPC 2025] 浮点 [题目传送门(比赛)](https://www.luogu.com.cn/problem/P16220?contestId=329544) ### 考察知识点 - 位运算
- 构造
错误原因
- 没有解题思路
正确思路
对于每个
n ,找到大于n 的最小2 的幂作为起始值,然后输出连续的n 个整数。这样构造出的序列的相邻两数的最高二进制位相同,从而满足条件正确代码
{\color{Green}{(AC)}} ::::success[正确代码]
#include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int t,n; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>t; while(t--){ cin>>n; int m=n; int res=1; //求出大于n的最小2的幂 while(m){ m/=2; res*=2; } //输出答案 for(int i=res;i<res+n;i++){ cout<<i<<" "; } cout<<endl; } return 0; }:::