- 杜昊阳 的博客
寒假思维训练day3
- @ 2025-2-6 11:21:00
错题1章节三
正确思路:应把点燃和熄灭的蜡烛分开来统计并循环计算.
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,r=0,sum=0;
cin>>a>>b;
while(a!=0){
sum+=a;
r+=a;
a=r/b;
r%=b;
}
cout<<sum;
return 0;
}
错题2章节三
错因:少思考了一种情况
正确思路:先思考再下手,分三种情况,全部考虑清楚再动手(1全普通2全特殊3特普混)2、3情况要判断.
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,n,m,r;
cin>>n>>m>>a>>b;
if(a*n<=n/m*b){
cout<<a*n;
}else{
r=n%m*a;
if(b>r){
cout<<n/m*b+r;
}else{
cout<<n/m*b+b;
}
}
return 0;
}
错题3章节三
错因:没有仔细看题目意思.
正确思路:统计问题的数量,碰到一个回答就减少一个问题. tip:文中指A回答的是前面的Q哦.
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string ans[505];
int t,n,q=0;
char v;
cin>>t;
for(int i=0;i<t;i++){
cin>>n;
q=0;
for(int j=0;j<n;j++){
cin>>v;
if(v=='Q'){
q++;
}else if(q!=0){
q--;
}
}
if(q!=0){
ans[i]="No";
}else{
ans[i]="Yes";
}
}
for(int j=0;j<t;j++){
cout<<ans[j]<<endl;
}
return 0;
}
错题4章节三
错因:没有中找到题目规律.
正确思路:应先找到题目规律,我们可以先统计xy的数量,x大就输出x-y,反之如此.
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int x=0,y=0;
string s;
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]=='x'){
x++;
}else{
y++;
}
}
if(x>y){
for(int i=0;i<x-y;i++){
cout<<'x';
}
}else{
for(int i=0;i<y-x;i++){
cout<<'y';
}
}
}
错题5章节三
正确思路:如果在循环过程中发现有问号,就根据字符串长度平替,如果字符串长度大或没有问号,则“NO”,剩余字符随便输出什么英文小写字母都可以.
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,c;
string s,u;
cin>>t;
for(int i=0;i<t;i++){
c=0;
cin>>s;
cin>>u;
for(int j=0;j<s.size();j++){
if(s[j]==u[c]){
c++;
}else if(s[j]=='?'){
s[j]=u[c];
c++;
}
if(c==u.size()){
break;
}
}
if(c==u.size()){
cout<<"YES"<<endl;
for(int j=0;j<s.size();j++){
if(s[j]!='?'){
cout<<s[j];
}
else{
cout<<'a';
}
}
cout<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}