T637642 比较数字
错误原因:题目意思分析不全面 原代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
string s;
cin>>s;
for(int i=0;i<s.size();i++){
if(s[i]=='0'){
cout<<'1';
}else{
cout<<s[i];
}
}
}
return 0;
}改正后的代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int c=0;
for(int i=0;i<s.size();i++){
if(s[i]=='0'){//当发现第一个零时
cout<<'1';
c=1;//标记,后面的全,输出一
}else if(c!=1){
cout<<s[i];
}else if(c==1){
cout<<'1';
}
}
cout<<endl;
return 0;
}T637627 张的弓
原来的代码是暴力的,还有点错误
#include<bits/stdc++.h>
using namespace std;
int main(){
int T;
cin>>T;
while(T--){
int n,m,k;
cin>>n>>m>>k;
if(n==1&&m==1){
cout<<1<<endl;
continue;
}if(m==1){
cout<<k<<endl;
}
bool jl=0;
int x=1,y=1;
while(k--){
if(jl==0&&y<m){
y++;
continue;
}
if(jl==1&&y>1){
y-=1;
continue;
}
if(y==1){
x++;
jl=0;
continue;
}
if(y==m){
x++;
jl=1;
}
}
cout<<x+y-2<<endl;
}
return 0;
}这提应该是根据题意和数据推出公式
#include<bits/stdc++.h>
using namespace std;
int main(){
long long T;
cin>>T;
while(T--){
long long n,m,k;
cin>>n>>m>>k;
long long y=k/n+1;
long long x=0;
if(y%2==0){
x=n-k%n;
}else{
x=k%n+1;
}
cout<<x+y-2<<endl;
}
return 0;
}T637602 可可口乐
原来用的是暴力,不知道使用广搜,没拿分 原代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string zs="cocacola";
string s;
cin>>s;
int ans;
for(int i=0;i<8;i++){
if(s[i]!=zs[i]){
for(int j=0;j<8;j++){
if(s[j]!=zs[j]&&s[i]==zs[j]&&s[j]==zs[i]){
swap(s[i],s[j]);
ans++;
}
}
}
}for(int i=0;i<8;i++){
if(s[i]!=zs[i]){
for(int j=0;j<8;j++){
if(s[j]!=zs[j]&&s[j]==zs[i]){
swap(s[i],s[j]);
ans++;
}
}
}
}
cout<<ans;
return 0;
}正确写法:
#include<bits/stdc++.h>
using namespace std;
string ss="cocacola";
struct node{
int c;
string s;
};
string s;
queue <node> q;
int ans;
map <string,bool> mp;//用来判断名称是否重复
void bfs(){
q.push({0,s});
mp[s]=1;
while(q.size()){
node f=q.front();//取出队首
q.pop();//对手出队
if(f.s==ss){
cout<<f.c;//输出步数
return ;
}
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
string xs=f.s;
swap(xs[i],xs[j]);
if(mp.count(xs)==0){//判断
mp[xs]=1;//标记
q.push({f.c+1,xs});//入队
}
}
}
}
return ;
}
int main(){
cin>>s;
bfs();
return 0;
}T637561 小梦的守卫
这题应该用前缀和来做
#include <bits/stdc++.h>
using namespace std;
long long n, q, a[200005], d[200005], s[200005], ss[200005];
int main() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
d[n - i + 1] = a[i];//求前缀和
s[i] = s[i - 1] + a[i];
}
for (int i = 1; i <= n; i++) {
ss[i] = ss[i - 1] + d[i];//求后缀和
}
while (q--) {
long long t;
cin >> t;
int l = upper_bound(s + 1, s + 1 + n, t) - s - 1;//二分查找找到从左到右可以坚持到t秒的士兵
int r = upper_bound(ss + 1, ss + 1 + n, t) - ss - 1;//二分查找找到从右到左可以坚持到t秒的士兵
if (l + r > n) {
cout << "Zombies ate your brain!\n";
} else {
cout << n - l - r <<endl;//输出士兵的数量
}
}
return 0;
}