这里写目录标题
容器放取逻辑
一、彩虹瓶
写法一:
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int N=1005;
//stack<int> st;
//int vis[N];
int a[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,k;//颜色数量,货架容量
cin>>n>>m>>k;
// 货架数量不够,愤怒;要从货架搬到地上,愤怒
while(k--){
for(int i=1;i<=n;i++){
cin>>a[i];
}
bool f=true;
int cnt=1;
// st.clear();
stack<int> st;//woc,注意要情况栈呀!没有clear函数
for(int i=1;i<=n&&f;i++){
if(a[i]==cnt){
cnt++;
while(!st.empty()&&st.top()==cnt){
st.pop();
cnt++;
}
//每次cnt改变都先往回看,就避免了vis判断是否在前面
}
else {
st.push(a[i]);
if(st.size()>m)f=false;
}
}
if(!f||cnt!=n+1)cout<<"NO";//直接判断后面这个就好啦
else cout<<"YES";
if(k)cout<<endl;
}
return 0;
}
//7 6 1 3 2 5 4
//逻辑见鬼,遇到的相等就输出,不是就看看栈顶是否有,没有就放到货架上
//不相等可能
// 记得每轮要初始化容器
写法二:
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int N=1005;
int a[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,k;//颜色数量,货架容量
cin>>n>>m>>k;
// 货架数量不够,愤怒;要从货架搬到地上,愤怒
while(k--){
for(int i=1;i<=n;i++)cin>>a[i];
int cnt=1;
bool f=true;
stack<int> st;
for(int i=1;i<=n;i++){
st.push(a[i]);
if(st.size()>m+1){
f=false;
break;
}
while(!st.empty()&&st.top()==cnt){
// if(st.top()==cnt){//woc这两种不一样,while里面的条件决定了是否死循环
st.pop();
cnt++;
// }
}
}
if(cnt==n+1&&f)cout<<"YES";
else cout<<"NO";
if(k)cout<<endl;
}
return 0;
}
逻辑见鬼系列
只是想把情况细分,没想到TT
有时候分的太细也容易搞混
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int N=1005;
int vis[N];
int a[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,k;//颜色数量,货架容量
cin>>n>>m>>k;
// 货架数量不够,愤怒;要从货架搬到地上,愤怒
while(k--){
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)cin>>a[i];
int cnt=1;
bool f=true;
stack<int> st;
for(int i=1;i<=n&&f;i++){
vis[a[i]]=1;
if(a[i]==cnt){
cnt++;
}
else {
while(vis[cnt]){
// i--;//下次要再看a[i],因为这次是往前面找,此时a[i]入栈还是等于cnt+1仍未可知
// vis[a[i]]=0;
if(!st.empty()&&st.top()==cnt){
st.pop();
cnt++;
}
else {
cout<<"NO";
f=false;
break;
}
}
st.push(a[i]);
if(st.size()>m){
cout<<"NO";
f=false;
break;
}
}
}
if(f){
while(!st.empty()){
if(st.top()==cnt){
cnt++;
st.pop();
}
else{
cout<<"NO";
break;
}
}
if(cnt==n+1)cout<<"YES";
}
if(k)cout<<endl;
}
return 0;
}
//7 6 1 3 2 5 4
//逻辑见鬼,遇到的相等就输出,不是就看看栈顶是否有,没有就放到货架上
//不相等可能
[Error] invalid type argument of unary ‘*’ (have ‘int’)
map容器的插入
mp.insert(1,2); 就会包这样的错,记得打花括弧
mp.insert({1,2});
也许逻辑错在没搞清vis数组记录是否是否陷入死循环,仅仅在一轮操作中起作用
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int N=1e4+5;
//struct node{
// int v,num;//本身的值,依附于他幸福树的个数
//};
int st,ed;
map<int,int> mp;
int vis[N];//依附于别人的幸福树,不会计入输出
bool f=false;
void judge(int x){
int res=0;
int y=x;
for(int i=1;;i++){
res=0;
while(y){
int t=y%10;
y/=10;
res+=t*t;
}
cout<<"res:"<<" "<<res<<endl;
if(res==1){
mp.insert({x,i});
vis[x]=0;
break;
}
else if(!vis[res]){
vis[res]=1;
// if(res>=st&&res<=ed){
if(mp.count(res)){//说明res依附于x
int z=mp[res]+i;cout<<"hhhc";
mp.insert({x,z});
vis[x]=0;
mp.erase(res);
vis[res]=0;
break;
}
else y=res;
}
else break;
}
}
//幸福树,最初的存入map,中途的搜索过程中标记为1,得到了1标记为0
//别的数碰到vis[x]=1说明不幸福,vis[x]不是1可以继续得到1
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>st>>ed;
mp.clear();
for(int i=st;i<=ed;i++){
if(!mp.count(i)&&!vis[i]){
vis[i]=1;
judge(i);
cout<<i<<" "<<mp.size()<<endl;
for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)cout<<it->first<<" ";
cout<<endl;
cout<<"end"<<endl;
}
//vis过要么是判断过,要么是依附于别人的(在路上判断过)
}
cout<<mp.size();
return 0;
}
//2 4 16 37 58 89
二、特立独行的幸福
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int N=10005;
int re[N];
bool isprime(int x){
int s=(int)sqrt(x);
for(int i=2;i<=s+1;i++){
if(x%i==0)return false;
}
return true;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
// 比起在map容器里的删除插入,将vis值该为0,1不是更方便吗,还有查找
// vis[x],mp.count(x)
int m,n;
cin>>m>>n;
//对于某个数,一要记录迭代过程中产生的数,防止死循环,这是在一轮判断中的操作
// 二要标记是否是依赖于别人的数/非幸福树,用于最后的输出,全局
for(int i=m;i<=n;i++){
if(!re[i]){//不是依赖别人的幸福树、不是不幸福数
int vis[N]={0};
int x=i;
while(true){
int t=0;
while(x){
t+=(x%10)*(x%10);
x/=10;
}
if(t==1)break;//re[x]依旧为0,是求的幸福树
// 10 13 23 后者依赖于前者
if(vis[t]){//如果t是死循环的或者是依赖于别人的
re[i]=1;
re[t]=1;//迭代出来的
break;
}
//不输出的,要么死循环,要么是依赖于别人的(别人迭代出来的
vis[t]=1;//记录途径的数,防止死循环
re[t]=1;
x=t;
}
}
}
int f=1;
for(int i=m;i<=n;i++){
if(!re[i]){
f=0;
int cnt=0;
int x=i;
while(true){
int t=0;
cnt++;
while(x){
t+=(x%10)*(x%10);
x/=10;
}
if(t==1)break;
x=t;
}
cout<<i<<" ";
if(isprime(i))cout<<cnt*2;
else cout<<cnt;
cout<<endl;
}
}
if(f)cout<<"SAD"<<endl;
return 0;
}