第七届福建省大学生程序设计竞赛

Problem 2264 Card Game (First Edition)

题意:

  有2×n张牌,Fat brother 和 Maze两个人轮流取一张牌,不放回, 谁取的牌大谁就得一分。问Fat brother 得分的期望。

题解:

  每个人赢的概率都是0.5。

代码:

#include<stdio.h>
using namespace std;
int main(){
    int T;
    scanf("%d",&T);
    for(int t = 1; t <= T; t++) {
        int n,x;
        scanf("%d",&n);
        for (int i = 0; i < n*2; i++){
            scanf("%d",&x);
        }
        printf("Case %d: %.2f\n",t,n/2.0);
    }
    return 0;
}
View Code

 

Problem 2265 Card Game (Second Edition)

题意:

  Fat brother 和 Maze两个人分别有n张牌,每轮两个人都在自己的牌里面随便选一张,不放回, 谁取的牌大谁就得一分。问Fat brother 得分的期望。

题解:

  将Fat brother每张牌能赢的次数加起来除以n。排个序在找就行了。

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = 10000 + 10;
int a[N],b[N];
int main(){
    int T,n;
    scanf("%d",&T);
    for(int t = 1; t <= T; t++) {
        scanf("%d",&n);
        for (int i = 0; i < n; i++){
            scanf("%d",&a[i]);
        }
        for (int i = 0; i < n; i++){
            scanf("%d",&b[i]);
        }
        sort(a,a+n);
        sort(b,b+n);
        int cnt = n-1,sum = 0;
        for (int i = n-1; i >=0; i--){
            while(a[i]<=b[cnt] && cnt>=0)
                cnt--;
            if (cnt<0) break;
            if (a[i] > b[cnt]) sum+=cnt+1;
        }
        printf("Case %d: %.2f\n",t,(double)sum/(double)n);
    }
    return 0;
}
View Code

 

Problem 2266 Card Game (Third Edition)

题意:

  Fat brother 和 Maze两个人分别有n张牌,每轮两个人都在自己的牌里面按顺序取一张,如果两人取的牌的和大于10就得一分,求得分。

题解:

  遍历一下两个数组就行了。

代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = 10000 + 10;
int a[N],b[N];
int main(){
    int T,n;
    scanf("%d",&T);
    for(int t = 1; t <= T; t++) {
        scanf("%d",&n);
        for (int i = 0; i < n; i++){
            scanf("%d",&a[i]);
        }
        for (int i = 0; i < n; i++){
            scanf("%d",&b[i]);
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (a[i]+b[i] > 10) {
                ans++;
            }
        }
        printf("Case %d: %d\n",t,ans);
    }
    return 0;
}
View Code

 

 

Problem 2267 The Bigger the Better

题意:

  给你一个长度为n的数字a和一个长度为m的数组b,每一个元素都是1~9,你每次可以从a或b中选最前面一个元素移动到c数组末尾,输出c数组字典序最大的结果。c数组最开始为空。

题解:

   然后两个数组最前面那个元素不一样直接比较之后加到c数组末尾即可,当两个元素相同的时候,我们可以通过比较这两个数组的字典序来决定取哪个数组中的元素。我们可以直接用字符串存这两个数组,通过字符串比较函数来比较。不过要注意两个字符串长度不相同的情况。

代码:

#include <stdio.h>
#include <string.h>
const int N = 100000 + 10;
char a[N],b[N],c[N*2];
int main() {
    int T,n,m,x;
    while (~scanf("%d",&T)) {
        for (int t = 1; t <= T; ++t) {
            scanf("%d%d",&n,&m);
            for (int i = 0; i < n; ++i) {
                scanf("%d",&x);
                a[i] = x+'0';
            }
            for (int i = 0; i < m; ++i) {
                scanf("%d",&x);
                b[i] = x+'0';
            }
            a[n] = b[m] = '0';
            a[n+1] = b[m+1] = '\0';
            int cnt1=0,cnt2=0,cnt=0;
            while (cnt1<n&&cnt2<m) {
                if (a[cnt1] < b[cnt2]) {
                    c[cnt++] = b[cnt2++];
                }else if (a[cnt1] > b[cnt2]) {
                    c[cnt++] = a[cnt1++];
                }else {
                    int fg = strcmp(a+cnt1,b+cnt2);
                    if (fg<0) {
                        while (a[cnt1] == b[cnt2] && cnt2<m)
                        c[cnt++] = b[cnt2++];
                    }
                    else {
                        while (a[cnt1] == b[cnt2] && cnt1<n)
                        c[cnt++] = a[cnt1++];
                    }
                }
            }
            while (cnt1<n) c[cnt++] = a[cnt1++];
            while (cnt2<m) c[cnt++] = b[cnt2++];
            c[cnt] = '\0';
            printf("Case %d: %s\n",t,c);
        }
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/l999q/p/10893357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值