PAT A 1148 1149 1150 1151

1148 枚举

题目大意: 假设有2个狼人,至少有一个,但是不是全部 在说谎 ,,那岂不是只有1个在说谎。至少有2人说谎,也就是说村民也有可能说谎 ,且至少有一个村民在说谎 。

#include
#include
using namespace std;

typedef struct node{
    bool flag;
    int id;
}node;
 
int main(){
    int n;
    scanf("%d", &n); 
    vector v(n+1);
    for(int i=1;i<=n;i++){
        char s[2];
        scanf("%s",s);
        v[i].flag =  s[0]=='+' ? false : true;//是狼人就是true 
        v[i].id = s[1] - '0';
    }
    int fake_num = 0;//统计说谎人数 
    int flag = false;
    //选取两个狼人 
    for(int i = 1; i <= n; i++){
        if(flag == false){ 
            for(int j = i+1; j <= n; j++){
                bool judge[105] = {false};
                fake_num = 0;
                judge[i] = judge[j] = true; 
                //先判断两个狼人说话是否符合条件 
                if(v[i].flag != judge[ v[i].id ]) fake_num++;
                if(v[j].flag != judge[ v[j].id ]) fake_num++;
                //printf("fake_num:%d ",fake_num);
                if(fake_num != 1) continue;
                //然后判断村民说话是否符合条件 
                for(int k = 1; k <= n; k++){
                    if(k != i && k != j){
                        if(v[k].flag != judge[ v[k].id ]) fake_num++;
                    } 
                }
                //printf(" %d\n",fake_num);
                if(fake_num == 2){
                    printf("%d %d", i, j);
                    flag = true;
                    break;
                }
            }
        }else{
            break; 
        } 
    }
    if(flag == false){
        printf("No Solution\n");
    }
    return 0;
}

结果: 有一个运行时错误,我还没找出来5555555555555~

1149 STL

incompatible 不相容的

#include
#include
#include
using namespace std;
 
int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    map > map;
    for(int i=0;i>a>>b;
        map[a].push_back(b);
        map[b].push_back(a);
    }
    for(int i=0;i v(k);
        int table[100001] = {0};
        for(int j=0;j>v[j];
            table[v[j]] = 1;
        }
        bool flag = true;
        for(int j=0;j

1150 图论

combinatorial 组合的
operations research 运筹学
旅行商环路

#include
#include
using namespace std;
int main(){
    int n,m,k;
    scanf("%d %d", &n, &m);
    int e[205][205];
    for(int i=0;i s;
        for(int j=1; j sum){
                mindis = sum;
                minid = i;
            }
        }
    }
    printf("Shortest Dist(%d) = %d", minid, mindis);
    return 0;
} 

1151 树

题目大意: 知道先序中序,找出最近祖先

#include
#include
using namespace std;

int m,n;
int p_in, q_in;
int p_pre, q_pre;
int anc;
vector inorder;
vector preorder;

int find(int st_in, int en_in, int st_pre, int en_pre){
    //首先找到根节点
    int root;
    for(int i=st_in; i<=en_in; i++){
        if(inorder[i] == preorder[st_pre]){
            root = i;
            break;
        }
    }
    if(p_in < root && q_in < root){//都在左子树 
        find(st_in, root-1, st_pre+1, st_pre+(root-st_in)+1);
    }else if(p_in > root && q_in > root){//都在右子树 
        find(root+1, en_in, root+1, en_pre-(en_in-root)+1);
    }else{
        return inorder[root];
    }
} 

int main(){
    
    scanf("%d %d", &m, &n);
    
    inorder.resize(n);
    preorder.resize(n);
    
    for(int i=0;i n){
            exist_u = false;
        }
        if( v <= 0 || v > n){
            exist_v = false;
        }
        if(exist_u == false && exist_v == false){
            printf("ERROR: %d and %d are not found.\n", u, v);
            continue;
        }else if(exist_u == false){
            printf("ERROR: %d is not found.\n", u);
            continue;
        }else if(exist_v == false){
            printf("ERROR: %d is not found.\n", v);
            continue;
        }
        //首先在序列中定位
        for(int i=0;i

结果: 超时可以用map解决,还有2错没找到!!

PAT A 1148 1149 1150 1151_第1张图片
部分正确

然后我就找到了!
原来编号不一定按照顺序。细节啊!自以为是啊!于是代码变成了这样:

#include
#include
#include 
using namespace std;

int m,n;
int p_in, q_in;
int p_pre, q_pre;
int anc;
map mp;
vector inorder;
vector preorder;

int find(int st_in, int en_in, int st_pre, int en_pre){
    //首先找到根节点
    int root;
    for(int i=st_in; i<=en_in; i++){
        if(inorder[i] == preorder[st_pre]){
            root = i;
            break;
        }
    }
    if(p_in < root && q_in < root){//都在左子树 
        find(st_in, root-1, st_pre+1, st_pre+(root-st_in)+1);
    }else if(p_in > root && q_in > root){//都在右子树 
        find(root+1, en_in, root+1, en_pre-(en_in-root)+1);
    }else{
        return inorder[root];
    }
} 

int main(){
    
    scanf("%d %d", &m, &n);
    
    inorder.resize(n);
    preorder.resize(n);
    
    for(int i=0;i

就只剩超时的问题了!

改进可以参考下面这种方法:

#include
#include
#include
using namespace std;

int main(){
    int m,n;
    scanf("%d %d", &m, &n);
    vector preorder(n);
    map mp;
    for(int i=0;i=u && anc <=v ) || (anc >=v && anc <=u )){
                break;
            }
        }
        if(anc == u){
            printf("%d is an ancestor of %d.\n", u, v);
        }else if(anc == v){
            printf("%d is an ancestor of %d.\n", v, u);
        }else{
            printf("LCA of %d and %d is %d.\n", u, v, anc);
        }
    }
    return 0;
} 

解决!

你可能感兴趣的:(PAT A 1148 1149 1150 1151)