zoukankan      html  css  js  c++  java
  • 7-3 Safari Park (25 分)

    A safari park(野生动物园)has K species of animals, and is divided into N regions. The managers hope to spread the animals to all the regions, but not the same animals in the two neighboring regions. Of course, they also realize that this is an NP complete problem, you are not expected to solve it. Instead, they have designed several distribution plans. Your job is to write a program to help them tell if a plan is feasible.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives 3 integers: N (0), the number of regions; R (≥), the number of neighboring relations, and K (0), the number of species of animals. The regions and the species are both indexed from 1 to N.

    Then R lines follow, each gives the indices of a pair of neighboring regions, separated by a space.

    Finally there is a positive M (≤) followed by M lines of distribution plans. Each plan gives N indices of species in a line (the i-th index is the animal in the i-th rigion), separated by spaces. It is guaranteed that any pair of neighboring regions must be different, and there is no duplicated neighboring relations.

    Output Specification:

    For each plan, print in a line Yes if no animals in the two neighboring regions are the same, or No otherwise. However, if the number of species given in a plan is not K, you must print Error: Too many species. or Error: Too few species. according to the case.

    Sample Input:

    6 8 3
    2 1
    1 3
    4 6
    2 5
    2 4
    5 4
    5 6
    3 6
    5
    1 2 3 3 1 2
    1 2 3 4 5 6
    4 5 6 6 4 5
    2 3 4 2 3 4
    2 2 2 2 2 2
     

    Sample Output:

    Yes
    Error: Too many species.
    Yes
    No
    Error: Too few species.
     
     
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int toInt(string st){
        int sum=0;
        for(int i=0;i<st.size();i++){
            sum=sum*10+(st[i]-'0');
        }
        return sum;
    }
    bool isP(int n){
        if(n<=1){
            return false;
        }
        for(int i=2;i<=sqrt(n);i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    
    int main(){
        string s,str;
        cin>>s;
        bool flag=true;;
        int len=s.size();
        for(int i=0;i<len;i++){
            str=s.substr(i,len);
            int sum=toInt(str);
            if(isP(sum)){
                printf("%s Yes
    ",str.c_str());
            }
            else{
                printf("%s No
    ",str.c_str());
                flag=false;
            }
        }
        if(flag==true){
            printf("All Prime!
    ");
        }
        return 0;
    }
  • 相关阅读:
    树形结构菜单,递归实现
    基于Vue的日历组件,可以标注重要日子
    关于element-ui级联菜单(城市三级联动菜单)和回显问题
    继承(面试问到)
    vue监听浏览器刷新
    Popover 弹出框,里面的表格点击后关闭弹窗
    el-table表格合并单元格
    对角线
    ElementUI中el-radio再次点击取消选中
    保留文本框换行和空格
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14508829.html
Copyright © 2011-2022 走看看