SDUTOJ 2167 Mathman Bank 第二届ACM省赛题 模拟

15 篇文章 0 订阅
10 篇文章 0 订阅

Mathman Bank

Time Limit: 1000MS Memory limit: 65536K

题目描述

 With the development of mathmen's mathematics knowlege, they have finally invented computers. Therefore, they want to use computers to manage their banks. However, mathmen's programming skills are not as good as their math- ematical skills. So they need your help to write a bank management system software.

The system must support the following operations:

1. Open account: given the customer's name and password, and an initial deposit, open an bank account for the customer.

2. Deposit: given the amount of money and the name of the customer, deposit money in the customer's account.

3. Withdraw: given the amount of money, the customer's name and password, withdraw money from the customer's account.

4. Transfer: given the amount of money, sender's name, sender's password and receiver's name, transfer money from the sender's account to the

receiver's account.

5. Check: given the customer's name and password, print the balance of the customer's account.

6. Change password: given the customer's name and old password, and the new password, replace the customer's old password with the new one.

Initially, no account exists in the bank management system .

输入

 The first line of the input contains one positive integer, n (n <= 1000), which is the number of commands. Each of the following n lines contains one of the following commands, in the following format:

1. O "customer" "password" "initial deposit" - open an account for "customer", set the password to "password" and deposit "inital deposit" money in the account. "customer" and "password" are strings, and "initial deposit" is an integer.

2. D "customer" "amount" - deposit "amount" money in "customer"'sac- count. "customer" is a string, and "amount" is an integer.

3. W"customer" "password" "amount" - withdraw "amount" money from "customer"'s account. "customer" and "password" are strings, and amount

4. T "sender" "password" "receiver" "amount" - transfer "amount" money from "sender"'s acount to "receiver"'s account. "sender", "pasword" and "receiver" are strings, and "amount" is an integer. 

5. C "customer" "password" - check "customer"'s balance. "customer" and "password" are strings.

6. X "customer" "old password" "new password" - replace "customer"'s old password with "new password". "customer", "old password" and "new password" are strings.

All of the strings appearing in the input consist of only alphebetical letters and digits, and has length at most 10. All the integers in the input are nonnegative, and at most 1000000.

Refer to the sample input for more details.

输出

For each of the commands, output one of the following results, respectively:

1. Open account: If "customer" already has an account, output "Account exists." (without quotation marks); otherwise, output "Successfully opened an account." (without quotation marks).

2. Deposite: If "customer" doesn't have an account ,output "Account does not exist."; otherwise, output "Successfully deposited money."(without quotation marks).

3. Withdraw: If "customer" doesn't have an account, output "Account does not exist."; otherwise, if "customer"'s password doesn't match "password", output "Wrong password."; otherwise, if there are less money than "amount" in "customer"'s account, output "Money not enough."; otherwise, output "Successfully withdrew money." quotation marks).

4. Transfer: If "sender"'s account or "receiver"'s account doesn't exist, output "Account does not exist."; otherwise, if "sender"'s password doesn't match "password", output "Wrong password."; otherwise, if there is less money than 'amount" in "sender"'s account, output "Money not enough."; otherwise, output "Successfully transfered money."

5. Check: If "customer"'s account doesn't exist, output "Account does not exist."; otherwise, if "customer"'s password doesn't match "password", output "Wrong password."; otherwise, output the balance of "customer"'s account.

6. Change password: If "customer" doesn't have an account, output "Account does not exist."; otherwise, if "customer"'s password doesn't match "old password", output "Wrong password."; otherwise, output 'Successfully changed password.".

示例输入

25
W Alice alice 10
O Alice alice 10
C Alice alice
D Bob 10000
O Bob bob 100
D Alice 50
C Alice alice
X Bob bob BOB
C Bob bob
O Bob bob 10
T Bob bob Alice 100000
W Alice alice 10
T Bob BOB Alice 100000
T Bob BOB Alice 100
C Alice alice
C Bob BOB
T Alice alice BOB 10
T ALICE alice Bob 10
X Jack jack JACE
X Alice ALICE alice
W Alice Alice 10
W Alice alice 200
T Alice alice Bob 80
C Alice alice
C Bob BOB

示例输出

Account does not exist.
Successfully opened an account.
10
Account does not exist.
Successfully opened an account.
Successfully deposited money.
60
Successfully changed password.
Wrong password.
Account exists.
Wrong password.
Successfully withdrew money.
Money not enough.
Successfully transfered money.
150
0
Account does not exist.
Account does not exist.
Account does not exist.
Wrong password.
Wrong password.
Money not enough.
Successfully transfered money.
70
80

提示

 

来源

山东省第二届ACM大学生程序设计竞赛


题目看起来很长,但读起来不难,就是一个模拟银行的模拟题:


先前做的时候一直WA,一开四怀疑自己的链表有问题,用c++的vector类又写了一份,最后才找到错误的地方,原来自己还可以给自己转账。。。。。。。


C++代码一份:(STL中的string类,vector类)


#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct node
{
    string name;
    string pswd;
    int mon;
};

vector <node> ls;

void O()
{
    string name,pswd;
    int ny;
    cin>>name>>pswd>>ny;
    vector <node> :: iterator it = ls.begin();
    while(it != ls.end())
    {
        if(it->name == name)
        {
            cout<<"Account exists.\n";
            return ;
        }
        it++;
    }
    node t;
    t.name = name;
    t.pswd = pswd;
    t.mon = ny;
    ls.push_back(t);
    cout<<"Successfully opened an account."<<endl;
}

void D()
{
    string name;
    int add;
    cin>>name>>add;
    vector <node> :: iterator it = ls.begin();
    while(it != ls.end())
    {
        if(it->name == name)
        {
            it->mon += add;
            cout<<"Successfully deposited money."<<endl;
            return ;
        }
        it++;
    }
    cout<<"Account does not exist."<<endl;
}

void W()
{
    string name,pswd;
    int sub;
    vector <node> :: iterator it = ls.begin();
    cin>>name>>pswd>>sub;
    while(it != ls.end())
    {
        if(it->name == name)
        {
            if(it->pswd == pswd)
            {
                if(it->mon >= sub)
                {
                    it->mon -= sub;
                    cout<<"Successfully withdrew money."<<endl;
                }
                else
                {
                    cout<<"Money not enough.\n";
                }
            }
            else
            {
                cout<<"Wrong password.\n";
            }
            return ;
        }
        it++;
    }
    cout<<"Account does not exist.\n";
}

void T()
{
    string name,pswd,rename;
    int sub;
    vector <node> :: iterator it = ls.begin(),st = ls.end(),rt = ls.end();
    cin>>name>>pswd>>rename>>sub;
    while(it != ls.end())
    {
        if(it->name == name)
        {
            st = it;
        }
        if(it->name == rename)
        {
            rt = it;
        }
        if(st != ls.end() && rt != ls.end())
            break;
        it++;
    }
    if(st != ls.end() && rt != ls.end())
    {
        if(st->pswd == pswd)
        {
            if(st->mon >= sub)
            {
                st->mon -= sub;
                rt->mon += sub;
                cout<<"Successfully transfered money."<<endl;
            }
            else
            {
                cout<<"Money not enough.\n";
            }
        }
        else
        {
            cout<<"Wrong password."<<endl;
        }
    }
    else
    {
        cout<<"Account does not exist.\n";
    }
}

void C()
{
    string name,pswd;
    vector <node> :: iterator it = ls.begin();
    cin>>name>>pswd;
    while(it != ls.end())
    {
        if(it->name == name)
        {
            if(it->pswd == pswd)
            {
                cout<<it->mon<<endl;
            }
            else
            {
               cout<<"Wrong password.\n";
            }
            return ;
        }
        it++;
    }
    cout<<"Account does not exist.\n";
}

void X()
{
    string name,opswd,npswd;
    vector <node> :: iterator it = ls.begin();
    cin>>name>>opswd>>npswd;
    while(it != ls.end())
    {
        if(it->name == name)
        {
            if(it->pswd == opswd)
            {
                it->pswd = npswd;
                cout<<"Successfully changed password.\n";
            }
            else
            {
                cout<<"Wrong password.\n";
            }
            return ;
        }
        it++;
    }
    cout<<"Account does not exist.\n";
}



int main()
{
    int t;
    string cm;
    ls.clear();
    cin>>t;
    while(t--)
    {
        cin>>cm;
        if(cm[0] == 'O')
            O();
        else if(cm[0] == 'D')
            D();
        else if(cm[0] == 'W')
            W();
        else if(cm[0] == 'T')
            T();
        else if(cm[0] == 'C')
            C();
        else if(cm[0] == 'X')
            X();
    }
    return 0;
}


C语言链表代码一份:


#include <stdio.h>
#include <string.h>

struct node
{
    char name[21];
    char pswd[21];
    int mon;
    node *next;
};

void O(node *head)
{
    char name[21],pswd[21];
    int sr;
    scanf("%s%s%d",name,pswd,&sr);
    node *p = head;
    while(p->next)
    {
        if(!strcmp(p->next->name,name))
        {
            printf("Account exists.\n");
            break;
        }
        p = p->next;
    }
    if(!p->next)
    {
        node *r = new node;
        strcpy(r->name,name);
        strcpy(r->pswd,pswd);
        r->mon = sr;
        p->next = r;
        r->next = NULL;
        printf("Successfully opened an account.\n");
    }
}

void D(node *head)
{
    char name[21];
    int sr;
    scanf("%s%d",name,&sr);
    node *p = head;
    while(p->next)
    {
        if(!strcmp(name,p->next->name))
        {
            p->next->mon += sr;
            printf("Successfully deposited money.\n");
            break;
        }
        p = p->next;
    }
    if(!p->next)
    {
        printf("Account does not exist.\n");
    }
}

void W(node *head)
{
    char name[21],pswd[21];
    int sr;
    scanf("%s%s%d",name,pswd,&sr);
    node *p = head;
    while(p->next)
    {
        if(!strcmp(p->next->name,name))
        {
            if(!strcmp(p->next->pswd,pswd))
            {
                if(sr <= p->next->mon)
                {
                    p->next->mon -= sr;
                    printf("Successfully withdrew money.\n");
                }
                else
                {
                    printf("Money not enough.\n");
                }
            }
            else
            {
                printf("Wrong password.\n");
            }
            break;
        }
        p = p->next;
    }
    if(!p->next)
    {
        printf("Account does not exist.\n");
    }
}

void T(node *head)
{
    char name[21],pswd[21],rename[21];
    int sr;
    scanf("%s%s%s%d",name,pswd,rename,&sr);
    node *p = head;
    node *q1 = NULL,*q2 = NULL;
    while(p->next)
    {
        if(!strcmp(p->next->name,name))
            q1 = p->next;
        if(!strcmp(p->next->name,rename))
            q2 = p->next;
        if(q1 && q2)
            break;
        p = p->next;
    }
    if(q1 && q2)
    {
        if(!strcmp(q1->pswd,pswd))
        {
            if(q1->mon >= sr)
            {
                q1->mon -= sr;
                q2->mon += sr;
                printf("Successfully transfered money.\n");
            }
            else
            {
                printf("Money not enough.\n");
            }
        }
        else
            printf("Wrong password.\n");
    }
    else
    {
        printf("Account does not exist.\n");
    }

}

void C(node *head)
{
    char name[21],pswd[21];
    scanf("%s%s",name,pswd);
    node *p = head;
    while(p->next)
    {
        if(!strcmp(p->next->name,name))
        {
            if(!strcmp(p->next->pswd,pswd))
            {
                printf("%d\n",p->next->mon);
            }
            else
            {
                printf("Wrong password.\n");
            }
            break;
        }
        p = p->next;
    }
    if(!p->next)
        printf("Account does not exist.\n");
}

void X(node *head)
{
    char name[21],opswd[21],npswd[21];
    scanf("%s%s%s",name,opswd,npswd);
    node *p = head;
    while(p->next)
    {
        if(!strcmp(p->next->name,name))
        {
            if(!strcmp(p->next->pswd,opswd))
            {
                strcpy(p->next->pswd,npswd);
                printf("Successfully changed password.\n");
            }
            else
            {
                printf("Wrong password.\n");
            }
            break;
        }
        p = p->next;
    }
    if(!p->next)
        printf("Account does not exist.\n");
}

int main()
{
    node *head = new node;
    head->next = NULL;
    int t;
    char cm[11];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",cm);
        if(cm[0] == 'O')
            O(head);
        else if(cm[0] == 'D')
            D(head);
        else if(cm[0] == 'W')
            W(head);
        else if(cm[0] == 'T')
            T(head);
        else if(cm[0] == 'C')
            C(head);
        else if(cm[0] == 'X')
            X(head);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值