2024.1.24笔记

P9516 color

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;

int main(){
    int a,b,c,d,e;
    cin>>a>>b>>c>>d>>e;
    int s=a+b+c+d+e;
    if(s<=99) cout<<"Gray"; 
    else if(s<=119) cout<<"Blue";
    else if(s<=169) cout<<"Green";
    else if(s<=229) cout<<"Orange";
    else cout<<"Red";
    return 0;
}

P7588 双重素数(2021 CoE-II A)

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10, INF=0x3f3f3f3f;
int n,m,a[N];
// 素数:[2, n-1] --- [2, sqrt(n)] = [2, n/i]
bool isp(int n){
    for(int i=2; i<=n/i; i++) if(n%i==0) return 0;
    return n > 1;
}
int s(int n){
    int res=0;
    while(n) res += n%10, n/=10;
    return res;
}
int main(){
//    file re- open 文件重定向   read  write
//    freopen("data.in", "r", stdin);
//    freopen("data.out", "w", stdout);
    int t,l,r; cin>>t;
    while(t--){
        cin>>l>>r;   int ans=0;
        for(int i=l; i<=r; i++) 
            if(isp(i) && isp( s(i) )) ans ++;
        cout<<ans<<endl;
    }
    return 0;
}
  • 优化
#include<bits/stdc++.h>
using namespace std;
const int N=5761455+10,M=1e8+10, INF=0x3f3f3f3f;
// 素数:[2, n-1] --- [2, sqrt(n)] = [2, n/i]
bool isp(int n){
    for(int i=2; i<=n/i; i++) if(n%i==0) return 0;
    return n > 1;
}
int s(int n){
    int res=0;
    while(n) res += n%10, n/=10;
    return res;
}
int main1(){
//    file re- open 文件重定向   read  write
//    freopen("data.in", "r", stdin);
//    freopen("data.out", "w", stdout);
    int t,l,r; cin>>t;
    while(t--){
        cin>>l>>r;   int ans=0;
        for(int i=l; i<=r; i++) if(isp(i) && isp( s(i) )) ans ++;
        cout<<ans<<endl;
    }
    return 0;
}
// -------------------------------------------
int pri[N], pn;
//bool st[M];   // 8bit *M
bitset<M> st;   // 1bit *M
void get_pri(int n){ // 欧拉筛法  = 线性筛法 O(n)
    st[0] = st[1] = 1;
    for(int i=2; i<=n; i++){
        if(!st[i]) pri[++pn] = i;
        for(int j=1; pri[j]<=n/i; j++){
            st[i*pri[j]] = 1;
            if(i%pri[j] == 0) break;
        }
    }
}
int main2(){
//    freopen("data.in", "r", stdin);
//    cout<<(sizeof(st) + sizeof(pri) )*1.0/1024/1024<<"MB"<<endl;
    get_pri(1e8);
//    cout<<pn<<endl;
    int t,l,r; cin>>t;
    while(t--){
        cin>>l>>r;   int ans=0;
        for(int i=l; i<=r; i++) 
            if(!st[i] && !st[ s(i) ]) ans ++;
        cout<<ans<<endl;
    }
    return 0;
}
//-------------------------
void ss(){
    int n = pn;
    pn=0;
    for(int i=1; i<=n; i++)
        if(!st[ s(pri[i]) ]) pri[++pn] = pri[i];
}
int main(){
    freopen("data.in", "r", stdin);
//    cout<<(sizeof(st) + sizeof(pri) )*1.0/1024/1024<<"MB"<<endl;
    get_pri(1e8);
//    cout<<pn<<endl;
    int t, l, r; cin >> t;
    while (t--) {
        cin >> l >> r;
        int x = lower_bound(pri + 1, pri + pn + 1, l) - pri;      // >= l
        int y = upper_bound(pri + 1, pri + pn + 1, r) - pri - 1;  // > r
        int res = y - x + 1;
        cout << res << endl;
    }
    return 0;
}

P1449 后缀表达式

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10, INF=0x3f3f3f3f;
int n,m,st[N], hh;

int main(){
    string s; cin>>s;
    for(int i=0, t=0; i<s.size(); i++){
        if(s[i]>='0' && s[i]<='9'){
            t = t*10 + s[i]-'0';
        }
        else if(s[i]=='.'){
            st[++hh] = t, t=0;
        }
        else{// +-*/
            int b=st[hh--], a=st[hh--], c;
            if(s[i] =='+') c=a+b;
            if(s[i] =='-') c=a-b;
            if(s[i] =='*') c=a*b;
            if(s[i] =='/') c=a/b;
            st[++hh] = c;
        }
    }
    cout<<st[hh];
    return 0;
}

B3642 二叉树的遍历

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n;
struct T{
    int lch,rch;
}tree[N];

void pre(int rt){
    if(rt==0) return ;
    cout<<rt<<" "; // 根 
    pre(tree[rt].lch); // 左 
    pre(tree[rt].rch); // 右 
}
void in(int rt){
    if(rt==0) return ;
    in(tree[rt].lch);
    cout<<rt<<" ";
    in(tree[rt].rch);
}
void post(int rt){
    if(rt==0) return ;
    post(tree[rt].lch);
    post(tree[rt].rch);
    cout<<rt<<" ";
}
int main(){
    cin>>n;
    int l,r;
    for(int i=1; i<=n; i++){
        cin>>l>>r;
        tree[i] = {l,r};
    }
    pre(1); cout<<endl;
    in(1);cout<<endl;
    post(1);cout<<endl;
    return 0;
}
posted @ 2024-01-24 15:41  HelloHeBin  阅读(63)  评论(0)    收藏  举报