🎆音乐分享
盗墓笔记·十年人间(八一七稻米节主题推广曲) – 李常超 (Lao乾妈)
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
char start[N], aim[N];
void turn(int i)
{
if (start[i] == '*') start[i] = 'o';
else start[i] = '*';
}
int main()
{
cin >> start >> aim;
n = strlen(start);
int res = 0;
for (int i = 0; i < n - 1; i ++ )
if (start[i] != aim[i])
{
turn(i);//反转
turn(i + 1);
res ++ ;
}
cout << res << endl;
return 0;
}
这个题,就是不看来源我就知道是cf的题(不要问我为什么)
🚥🚥🚥
注意翻硬币的过程,是从前往后递推
就是先变第1个,然后第2个,第3个,第4个……
🚥🚥🚥
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int n;
string str;
void update(char& c)//引用
{
if (c == 'W') c = 'B';//更换颜色
else c = 'W';
}
bool check(char c)
{
vector<int> res;
string s = str;
for (int i = 0; i + 1 < n; i ++ )
{
if (s[i] != c)
{
update(s[i]);
update(s[i + 1]);
res.push_back(i);//存下这个操作,因为下面要输出
}
}
if (s.back() != c) return false;//就是没有全部变为一个颜色
cout << res.size() << endl;
for (int x: res) cout << x + 1 << ' ';//因为题目中的下标从1开始
if (res.size()) cout << endl;
return true;
}
int main()
{
int T;
cin >> T;
while (T -- )
{
cin >> n >> str;
if (!check('B') && !check('W')) puts("-1");
//判断能不能全部变为一个颜色
//注意,check的返回类型为bool类型
}
return 0;
}
P1146 硬币翻转 – 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
那么把所有的硬币都翻过来,操作次数=硬币个数
🚥🚥🚥
但是可不能这样子写,因为要输出具体的操作方法
代码
#include<iostream>
using namespace std;
const int maxn=101;
bool a[maxn];//a数组负责存储硬币的状态
int n;//n枚硬币
int main()
{
cin>>n;
cout<<n<<endl;//因为相当于只翻一枚,所以翻n次即可
for(int i=1;i<=n;i++){//i表示这是第几次翻
for(int j=1;j<=n;j++){//表示当前翻得是第几枚硬币
if(j!=i){//如果不为第i枚(因为翻的是n-1枚)
if(a[j])a[j]=0;//1变成0
else a[j]=1;//0变成1
}
cout<<a[j];//输出当前状态
}
cout<<endl;//别忘了换行
}
return 0;
}
Code over!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/131376.html