CTU Open 2008(未完工)

本文解析了CTU2008竞赛中的部分题目,包括模拟银行操作、股票交易匹配、补助分配等问题,并提供了相应的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:CTU Open 2008


【2014/05/15】今晚做了一下CTU 2008的这套题,最后的rank是3道题。基本是水题啊,我们做的是4个小时,如果完整做,我想应该还会出掉D题,主要是D题很繁琐,weikd写起都说烦。




A - Alea iacta est

B - On-Line Banking

【题意】纯模拟题,模拟银行存钱,取钱,转钱的操作。注意下细节~

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#define EPS 1e-8
using namespace std;

char str[1005];
char cmd[1005];
char name[1005],name1[1005];
map<string,double> ac;

void create(string id)
{
    printf("create: ");
    if (ac.find(id)!=ac.end())
    {
        printf("already exists\n");
    }
    else
    {
        ac[id] = 0;
        printf("ok\n");
    }
}

void deposit(string id, double x)
{
    printf("deposit %.2f: ",x);
    if (ac.find(id)==ac.end())
    {
        printf("no such account\n");
    }
    else
    {
        ac[id] = ac[id]+x;
        printf("ok\n");
    }
}

void withdraw(string id,double x)
{
    printf("withdraw %.2f: ",x);
    if (ac.find(id)==ac.end())
    {
        printf("no such account\n");
    }
    else
    {
        if (ac[id]+EPS<x)
        {
            printf("insufficient funds\n");
        }
        else
        {
            ac[id]=ac[id]-x;
            printf("ok\n");
        }
    }
}

void transfer(string ida,string idb,double x)
{
    printf("transfer %.2f: ",x);
    if (ac.find(ida)==ac.end()||ac.find(idb)==ac.end())
    {
        printf("no such account\n");
    }
    else
    {
        if (ida==idb)
        {
            printf("same account\n");
        }
        else
        {
            if (ac[ida]+EPS<x)
            {
                printf("insufficient funds\n");
            }
            else
            {
                ac[ida]=ac[ida]-x;
                ac[idb]=ac[idb]+x;
                char ia = *ida.rbegin();
                char ib = *idb.rbegin();
                if (ia==ib)
                {
                    printf("ok\n");
                }
                else
                {
                    printf("interbank\n");
                }
            }
        }
    }
}

int main()
{
    #ifdef HotWhite
    freopen("in.txt", "r", stdin);
    #endif
    int n;
    double mn;
    while(scanf("%d",&n))
    {
        if (!n) break;
        ac.clear();
        for (int i = 0; i < n; i++)
        {
            scanf("%s %lf",str,&mn);
            ac[str]=mn;
        }
        string s1,s2;
        while(scanf("%s",cmd))
        {
            if (strcmp(cmd,"end")==0) break;
            if (strcmp(cmd,"create")==0)
            {
                scanf("%s",name);
                s1=name;
                create(s1);
            }
            if (strcmp(cmd,"deposit")==0)
            {
                scanf("%s%lf",name,&mn);
                s1=name;
                deposit(s1,mn);
            }
            if (strcmp(cmd,"withdraw")==0)
            {
                scanf("%s%lf",name,&mn);
                s1=name;
                withdraw(s1,mn);
            }
            if (strcmp(cmd,"transfer")==0)
            {
                scanf("%s%s%lf",name,name1,&mn);
                s1=name;
                s2=name1;
                transfer(s1,s2,mn);
            }
        }
        printf("end\n\n");
    }
    printf("goodbye\n");
    return 0;
}


C - International Collegiate Programming Contest

D - Careful Declaration

E - Stock Exchange

【题意】每一组case给你一些买家的姓名和最高出价,还有卖家的姓名和最低售价。然后计算出所有成交信息。
【思路】水题,直接暴力模拟,比较价钱即可。代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 1010;

struct BID
{
    char name[25], type[5];
    double price;
};
BID bid[MAX], buy[MAX], sell[MAX];

int main() {

#ifdef HotWhite
    freopen("in.txt", "r", stdin);
#endif

    int N, flag;
    char issuer[12];

    while(scanf("%d%s", &N, issuer) != EOF && N)
    {
        int cntb = 0, cnts = 0;
        for(int i=0; i<N; ++i)
        {
            scanf("%s%s%lf", bid[i].name, bid[i].type, &bid[i].price);
            if(strcmp(bid[i].type, "buy") == 0)
            {
                buy[cntb++] = bid[i];
            }
            if(strcmp(bid[i].type, "sell") == 0)
            {
                sell[cnts++] = bid[i];
            }
        }
        printf("%s\n", issuer);
        for(int i=0; i<N; ++i)
        {
            flag = 0;
            printf("%s:", bid[i].name);
            if(strcmp(bid[i].type, "buy") == 0)
            {
                for(int j=0; j<cnts; ++j)
                {
                    if(sell[j].price <= bid[i].price)
                    {
                        printf(" %s", sell[j].name);
                        flag = 1;
                    }
                }
            }
            if(strcmp(bid[i].type, "sell") == 0)
            {
                for(int j=0; j<cntb; ++j)
                {
                    if(buy[j].price >= bid[i].price)
                    {
                        printf(" %s", buy[j].name);
                        flag = 1;
                    }
                }
            }
            if(!flag)
                printf(" NO-ONE");
            printf("\n");
        }
    }
    return 0;
}

F - Wooden Fence

G - Safe Gambling

H - Government Help

【题意】经济危机,政府给予bankA和bankB补助,补助是按照一包一包的算。总共有N包,为了公平起见,怎么分配才能使得最后bankA和bankB得到的补助之差最小。

【思路】贪心策略,从小到大排序,先把最小的给A。然后从最大的开始先给B,再给A,依次分配下去,直到分完。代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int a[50005];
int main() {
#ifdef HotWhite
   // freopen("in.txt", "r", stdin);
#endif
    int n;
    while(scanf("%d",&n))
    {
        if (!n) break;
        for (int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        printf("%d-A",a[0]);
        int k=0;
        for (int i = n-1; i >=1; i--,k++)
        {
            printf(" %d-%c",a[i],k&1?'A':'B');
        }
        printf("\n");
    }
    return 0;
}


I - Tree Insertions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值