天梯训练赛

暴力模拟大法好

正整数A+B

好好的题目坑这么多....
看上去挺麻烦的 但只要注意第一个空格 左边为a 右边为b 所有的操作都围绕空格

#include<bits/stdc++.h>

using namespace std;

int main() {
    string s;
    getline(cin, s);
    int len = (int) s.length(), now = 1;
    int a = 0, b = 0;
    int fa = 1, fb = 1, pos;
    for (int i = 0; i < len; ++i) {
        if (s[i] < '0' || s[i] > '9') {
            if (now == 1) {
                fa = 0;
            } else if (now == 2) {
                fb = 0;
                break;
            }
            if (s[i] == ' ' && s[i + 1] <= '9' && s[i + 1] >= '0') {
                pos=i+1;
                now=2;
            }
            if (s[i] == ' ' && (s[i + 1] > '9' || s[i + 1] < '0')) {
                fb=0;
                break;
            }
        } else if (s[i + 1] == ' '&&now==1) {
            if (fa == 1) {
                for (int j = 0; j <= i; ++j) {
                    a = a * 10 + s[j] - '0';
                }
            }
            now = 2;
            if (s[i + 2] < '0' || s[i + 2] > '9') {
                fb = 0;
                break;
            } else {
                pos = i + 2;
            }
            ++i;
        }
    }
    if(fb){
        for(int i=pos;i<len;++i){
            b=b*10+s[i]-'0';
        }
    }
    if(a<1||a>1000) fa=0;
    if(b<1||b>1000) fb=0;
    if(fa&&fb){
        printf("%d + %d = %d",a,b,a+b);
    }else{
        if(!fa){
            printf("? + ");
        }else{
            printf("%d + ",a);
        }
        if(!fb){
            printf("? = ?\n");
        }else{
            printf("%d = ?\n",b);
        }
    }
    return 0;
}

愿天下有情人都是失散多年的兄妹

做到不想说话....十行代码九个坑

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e4+5;
map<string,int>mp;
int Sex[maxn*3],n;
vector<int>G[maxn*3];
bool val[maxn*3];

void dfs(int pos,int u){
    val[u]=1;
  
    for(int v:G[u]){
        if(pos+1<=5) dfs(pos+1,v);
    }
}
bool dfs1(int pos,int u){
    if(val[u]==1) return true;
    val[u]=1;
    for(int v:G[u]){
        if(pos+1<=5) {
            if(dfs1(pos + 1, v)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    string s,a,b;
    char ch;
    int cnt=0;
    for(int i=1;i<=n;++i){
        cin>>s;
        mp[s]=++cnt;
        cin>>ch;
        if(ch=='M'){
            Sex[cnt]=1;
        }
        cin>>a>>b;
        if(a!="-1"&&!mp[a]){
            mp[a]=++cnt;
            Sex[cnt]=1;//隐藏条件
        }
        if(a!="-1")
        G[mp[s]].push_back(mp[a]);
        if(b!="-1"&&!mp[b]){
            mp[b]=++cnt;
        }
        if(b!="-1")
        G[mp[s]].push_back(mp[b]);
    }
    int k;cin>>k;
    while(k--){
        cin>>a>>b;
        if(Sex[mp[a]]==Sex[mp[b]]){
            puts("Never Mind");
        }else{
            memset(val,0,sizeof(val));
            dfs(1,mp[a]);
            if(dfs1(1,mp[b])){
                puts("No");
            }else{
                puts("Yes");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/smallocean/p/10527527.html