PAT-乙级练习题-1058~1065-解题报告

题目链接:1058. 选择题

代码:

struct node{
    int v;
    vector<char> ans;
}p[2000];
int a[2000];
int vec[2000];
int main()
{
    int n,m;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int k;
        cin >> p[i].v >> k >> k;
        while(k--)
        {
            char ch;
            cin >> ch;
            p[i].ans.push_back(ch);
        }
        sort(p[i].ans.begin(), p[i].ans.end());
    }
    int maxnum = 0;
    for (int i = 0; i < n; i++)
    {

        for (int j = 0; j < m; j++)
        {
            vector <char> ret;
            char ch;
            int num;
            cin >> ch >> num;
            while(num--)
            {
                cin >> ch;
                ret.push_back(ch);
            }
            cin >> ch;

            sort(ret.begin(), ret.end());
            int f = 0;

            if (ret.size() ==  p[j].ans.size())
            {
                for (int l = 0; l < ret.size(); l++)
                {
                    if (ret[l] != p[j].ans[l]) f = 1;
                }
                if (!f) a[i] += p[j].v;
                else
                {
                    vec[j + 1]++;
                    maxnum = max(maxnum, vec[j + 1]);
                }
            }
            else
            {
                vec[j + 1]++;
                maxnum = max(maxnum, vec[j + 1]);
            }

        }

    }
    for (int i = 0; i < n; i++) cout << a[i] << endl;
    if (maxnum == 0)
    {
        cout << "Too simple\n";
    }
    else{
        cout << maxnum;
        for (int i = 1; i <= m; i++)
        {
            if (vec[i] == maxnum){
                cout << " " << i;
            }
        }
        cout<return 0;
}

题目链接:1059. C语言竞赛

代码:

bool isPrime(int n)
{
    if (n<= 1) return false;
    else if (n == 2) return true;
    else if (n % 2 == 0)return false;
    else
    {
        double limit = sqrt(n) + 1;
        for (int i = 3; i <= limit; i += 2)
        {
            if (n % i == 0) return false;
        }
        return true;
    }
}
int mp[100005];
int vis[100005];
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        int num;
        cin >> num;
        if (i == 1) mp[num] = 1;
        else if (isPrime(i)) mp[num] = 2;
        else mp[num] = 3;
    }
    int k;
    cin >> k;
    while(k--)
    {
        int num;
        cin >> num;
        if (mp[num] == 0)
        {
            printf("%04d: Are you kidding?\n",num);
            continue;
        }
        if (vis[num])
        {
            printf("%04d: Checked\n",num);
            continue;
        }
        if (mp[num] == 1)
        {
            printf("%04d: Mystery Award\n",num);
        }
        else if (mp[num] == 2)
        {
            printf("%04d: Minion\n",num);
        }
        else
        {
            printf("%04d: Chocolate\n",num);
        }
        vis[num] = 1;
    }
    return 0;
}

题目链接:1060. 爱丁顿数

代码:

long long num[1000005];
bool cmp(int a, int b)
{
    return a > b;
}
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
    }
    sort(num, num + n, cmp);
    int ans = 0;
    while(ans <= n && num[ans] > ans+1)
    {
        ans++;
    }

    cout << ans << endl;
    return 0;
}

题目:1061. 判断题

代码:

int a[200];
int b[200];
int c[200];
int main()
{
    int n,m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) cin >> a[i];
    for (int i = 0; i < m; i++) cin >> b[i];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            int num;
            cin >> num;
            if (num == b[j]) c[i] += a[j];
        }

    }
    for (int i = 0; i < n; i++) cout << c[i] << endl;
    return 0;
}

题目:1062. 最简分数

代码:

long long gcd(long long a, long long b)
{
    return b == 0 ? a : gcd(b, a%b);
}
int main()
{
    double a,b,c,d,k;
    char ch;
    cin >> a >> ch >> b;
    cin >> c >> ch >> d;
    cin >> k;

    double x = min(a/b,c/d);
    double y = max(a/b,c/d);

    int first = 1;

    for (double i = 1; i < k; i++)
    {
        double z = i / k;
        if (z > x && z < y && gcd(i,k) == 1)
        {
            if (first) printf("%.0f/%.0f",i,k),first = 0;
            else printf(" %.0f/%.0f",i,k);
        }
    }
    cout << endl;
    return 0;
}

题目:1063. 计算谱半径

代码:

int main()
{
    int n;
    cin >> n;
    double ans = 0;
    for (int i = 0; i < n; i++)
    {
        int u,v;
        cin >> u >> v;
        ans = max(ans, sqrt(u * u + v * v * 1.0));
    }
    printf("%.2f\n",ans);
    return 0;
}

题目:1064. 朋友数

代码:

int solve(int num)
{
    int ans = 0;
    while(num > 0)
    {
        ans += num % 10;
        num /= 10;
    }
    return ans;
}
int vec[100005];
vector <int> ans;
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int num,ret;
        cin >> num;
        ret = solve(num);
        vec[ret]++;
    }
    for (int i = 0; i < 100000; i++)
    {
        if (vec[i]) ans.push_back(i);
    }
    cout << ans.size() << endl;
    cout << ans[0];
    for(int i = 1; i < ans.size(); i++) cout << " " << ans[i];
    cout << endl;
    return 0;
}

题目:1065. 单身狗

代码:

int mp[1000005];
int num[1000005];
int vis[1000005];
vector <int> ans;
int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int u,v;
        cin >> u >> v;
        mp[u] = v + 1;
        mp[v] = u + 1;
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++)
    {
        cin >> num[i];
        vis[num[i]] = 1;
    }
    for (int i = 0; i < k; i++)
    {
        int u = mp[num[i]];
        if (u && vis[u - 1]) continue;

        ans.push_back(num[i]);
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    if (ans.size())
    {
       // cout << ans[0];
        printf("%05d",ans[0]);
        for (int i = 1; i < ans.size(); i++) printf(" %05d",ans[i]);
        cout << endl;
    }
    return 0;
}

你可能感兴趣的:(PAT)