首页 > 试题广场 >

Mileage Bank

[编程题]Mileage Bank
  • 热度指数:1667 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
Mileage program of ACM (Airline of Charming Merlion) is really nice for the travelers flying frequently. Once you complete a flight with ACM, you can earn ACMPerk miles in your ACM Mileage Bank depended on mileage you actual fly. In addition, you can use the ACMPerk mileage in your Mileage Bank to exchange free flight ticket of ACM in future. The following table helps you calculate how many ACMPerk miles you can earn when you fly on ACM. When you fly ACM Class Code You'll earn First Class F Actual mileage + 100% mileage Bonus Business Class B Actual mileage + 50% mileage Bonus Economy Class Y 1-500 miles 500 miles 500+ miles Actual mileage It's shown that your ACMPerk mileage consists of two parts. One is your actual flight mileage (the minimum ACMPerk mileage for Economy Class for one flight is 500 miles), the other is the mileage bonus (its accuracy is up to 1 mile) when you fly in Business Class and First Class. For example, you can earn 1329 ACMPerk miles, 1994 ACMPerk miles and 2658 ACMPerk miles for Y, B or F class respectively for the fly from Beijing to Tokyo (the actual mileage between Beijing and Tokyo is 1329 miles). When you fly from Shanghai to Wuhan, you can earn ACMPerk 500 miles for economy class and ACMPerk 650 miles for business class (the actual mileage between Shanghai and Wuhan is 433 miles). Your task is to help ACM build a program for automatic calculation of ACMPerk mileage.

输入描述:
he input file contains several data cases. Each case has many flight records, each per line. The flight record is in the following format:
OriginalCity DistanceCity ActualMiles ClassCode
Each case ends with a line of one zero.


输出描述:
Output the summary of ACMPerk mileages for each test case, one per line.

When calculate bonus, be sure you rounded x.5 up to x+1
示例1

输入

Beijing Tokyo 1329 F
Shanghai Wuhan 433 Y
0
#

输出

3158
题目的意思就是积攒“里程”,F可以双倍,B可以1.5倍,Y就是1倍(但不够500可以按500算)。
需要注意的就是1.5倍那里题目所要求的四舍五入。
假设x是个整数。那么1.5x应该是(int)((double)x*1.5+0.5),这里是通过了,不过我不知道会不会出现误差舍入的问题
#include <stdio.h>
#include <string.h>
#define N 1000

int GetBonus(char str[N])
{
    int len=strlen(str);
    char c=str[len-1];
    int x=0;
    int i=0;
    while(str[i]<'0'||str[i]>'9') i++;
    while('0'<=str[i]&&str[i]<='9')
    {
        x=x*10+(str[i]-'0');
        i++;
    }
    if(c=='F') return 2*x;
    else if(c=='Y') return x<500 ? 500 : x;
    else return (int)((double)x*1.5+0.5);
}

int main()
{
    char str[N];
    int mileage;
    while(gets(str))
    {
        int len=strlen(str);
        if(str[0]=='0'&&len==1) break;
        mileage=GetBonus(str);
        while(gets(str))
        {
            len=strlen(str);
            if(str[0]=='0'&&len==1) break;
            mileage+=GetBonus(str);
        }
        printf("%d\n", mileage);
    }
    return 0;
}

发表于 2018-03-07 20:12:35 回复(0)
Java 解法
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int ACMPerk=0;
        while (scanner.hasNext()){
            String s = scanner.nextLine();
            if (!s.equals("0")){
                String[] s1 = s.split(" ");
                String level = s1[s1.length-1];
                int mile = Integer.parseInt(s1[s1.length-2]);
                switch (level){
                    case "F":
                        ACMPerk+= mile*2;
                        break;
                    case "B":
                        ACMPerk+= mile*1.5;
                        break;
                    case "Y":
                        ACMPerk += Math.max(mile, 500);
                        break;
                }
            }
        }
        System.out.println(ACMPerk);
    }
}


发表于 2020-03-06 16:25:08 回复(0)
读题有点难度,其他读懂了就很容易了
#include<iostream>
#include<string>
using namespace std;
int money(int n, char ch) {
    switch (ch) {
    case 'Y':
        if (n > 500) return n;
        else return 500;
    case 'B':
        return (int)(1.5*n+0.5);
    case 'F':
        return 2*n;
    }
    return 0;
}
int main() {
    string p1, p2;
    int distance;
    char rank;
    int total = 0;
    while (cin >> p1) {
        if (p1 == "0")
            break;
        cin >> p2 >> distance >> rank;
        total += money(distance, rank);
    }
    cout << total << endl;
}

发表于 2019-02-08 16:17:18 回复(1)
bool process()
{
    int sum   = 0;
    int bonus = 0;
    std::string src, dst, lv;
    while ((std::cin >> src) && src != "#") {
        if (src == "0") {
            printf("%d\n", sum);
            return true;
        }
        std::cin >> dst >> bonus >> lv;
        switch(lv[0]) {
        case 'F': bonus *= 20; break;
        case 'B': bonus *= 15; break;
        default : bonus  = std::max(bonus, 500) * 10; break;
        }
        sum += (bonus + 5) / 10;
    }
    return false;
}

编辑于 2017-03-14 19:49:07 回复(0)
读题比较困难,这里我们来翻译一下:
描述:
ACM(一个航空组织。。。)的里程计划对经常飞行的旅客是非常好的。只要你完成了一次在ACM的飞行,根据你飞行的实际里程,你将会得到ACMPerk米的奖励放到你的ACM里程银行中。此外,你可以使用ACM里程银行中的ACMPerk里程去交换以后的免费的飞机票。
根据你的ACM飞行级别,你将会 得到:
一等F级别:飞行的实际里程+100%的里程奖励
商务B级别:飞行的实际里程+50%的里程奖励
经济Y级别:
1-500里程 :500里程
500里程+  :飞行的实际里程
这表示你的ACMPerk里程考虑两部分。一个是你的实际飞行里程(ACMPerk里程对于一次经济级别飞行最少是500m,也就是说你飞了不到500里程,也会给你500里程的ACMPerk),另一部分是当你乘坐商务级别或者一等级别时候的里程奖励(准确的说1里程奖励就是1m)。
**举个例子:从北京到Tokyo的实际路程是 1329m。**
如果是经济Y级别,将会得到1329m的AMCPerk。
如果是商务B级别,将会得到1329+1329*50%=1993.5=1994 --> 这里1329*50%得到.5要进一。(还是很大方的)
如果是一等F级别,将会得到1329+1329=2658。
**再举个例子:从上海到武汉,实际路程是433m**
Y:500
B:433+433/2+0.5
F:433+433

输入是多组样例,输出要输出飞机得到奖励的总和。所以思路很简单,直接计算每次得到奖励并相加就行。

#include<iostream>
(720)#include<string>
using namespace std;
int main(){
	string from,to,level;
	int len;
	float sum=0;
	while(cin>>from){
		if(from=="0")break;
		cin>>to>>len>>level;
		if(level=="Y"){
			if(len<=500)sum+=500;
			else sum+=len;
		}else if(level=="B"){
			if(len%2==0){
				sum+=(len+len/2);
			}else{
				sum+=(len+len/2+1);
			}
		}else{
			sum+=2*len;
		}
	}
	cout<<sum<<endl;
	return 0;
}


编辑于 2020-03-04 10:43:55 回复(0)
这道题走java的话,我一开始没注意是直接一次性输入的字符串,还以为是分批次输入 地点,地点,距离,等级....实际上就是一个字符串,中间还带空格的
编辑于 2024-03-28 17:03:33 回复(0)
def Mileage(a, b):
    if b == 'F':
        return 2*a
    elif b == 'B':
        return int(a + 0.5*a + 0.5)
    elif b == 'Y' and a >= 500:
        return a
    else:
        return 500
s1 = 0
while True:

    
    s = list(input().split())
    if s[0] == '0'&nbs***bsp;s[0] == '#':
        break
    s1+=Mileage(int(s[2]), s[-1])
print(s1) 


发表于 2024-03-18 23:09:27 回复(0)
#include<stdio.h>
#include<string>
using namespace std;
typedef struct Node{
    string from;
    string to;
    float length;
    char rank;
}Node;

int main(){
    char tmp1[100] = {0};
    char tmp2[100] = {0};
    int tmp3;
    char tmp4;
    int res = 0;
    while(scanf("%s%s%d %c",tmp1,tmp2,&tmp3,&tmp4)!=EOF){
        if(tmp1[0] =='0'){
            printf("%d\n",res);
            res = 0;
        }else if(tmp1[0]=='#') break;
        if(tmp4 == 'F'){
            res = res + tmp3*2;
        }else if(tmp4 =='Y'){
            if(tmp3>500){
                res = res +tmp3;
            }else res = res + 500;
        }else if(tmp4 == 'B'){
            res = res + tmp3*1.5;
        }

    }
}


发表于 2024-03-16 16:19:56 回复(0)
#include <stdio.h>
#include <string.h>
//构造结构体,表示每次航班
typedef struct{
    char Ocity[100];
    char Dcity[100];
    float distance;
    char clas;
}Flight;
//构造函数计算每次航班会得到多少
int ACMperk(float d,char clas){
    float result=0;
    if(clas=='F'){
        result=d*2;
    }
    else if(clas=='B'){
        result=d+d/2;
    }
    else if((clas=='Y')&&(d<=500)){
        result=500;
    }
    else{
        result=d;
    }
    int test=result;
    if(test<result){
        return test+1;
    }
    else{
        return test;
    }
}
int main() {
   Flight flight[100];
   int i=0;
   while(scanf("%s %s %f %c",flight[i].Ocity,flight[i].Dcity,&flight[i].distance,&flight[i].clas)!=EOF){
    if(strcmp(flight[i].Ocity,"0")==0){
          break;
    }
     //printf("zh%d\n",i);
    //scanf("%s %s %f %c",flight[i].Ocity,flight[i].Dcity,&flight[i].distance,&flight[i].clas);
    i++;
   }
  // printf("zhsuhs%d\n",i);
   int sum=0;
   for(int j=0;j<=i;j++){
    sum=sum+ACMperk(flight[j].distance,flight[j].clas);
   }
   printf("%d\n",sum);
   return 0;
}
发表于 2024-03-13 15:36:05 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
int main(){
    vector<string> input;
    string first;
    while((getline(cin, first)) && first != "#"){
        input.push_back(first);
        string temp;
        while(input.back()!="0"){
            getline(cin, temp);
            input.push_back(temp);
        }
        input.pop_back();
        int total = 0;
        for(int i=0; i<input.size(); ++i){
            int mile = 0;
            for(int j=input[i].size()-3, k=0; input[i][j]>='0' && input[i][j]<='9'; --j, ++k){
                mile+=(input[i][j]-'0')*pow(10, k);
            }
            if(input[i].back() == 'F'){
                total += mile * 2;
            }else if(input[i].back() == 'B'){
                total += ceil(mile * 1.5); // 向上取整
            }else if(mile<500){
                total += 500;
            }else {
                total += mile;
            }
        }
        cout << total << endl;
    }
}

编辑于 2024-03-10 20:28:08 回复(0)
/*
题目翻译:
描述:
ACM(魅力鱼尾狮航空公司)的里程计划对经常飞行的旅客来说真的很好。
使用ACM完成飞行后,您可以根据实际飞行里程在ACM里程库中赚取ACMPerk里程。
此外,您可以使用里程银行中的ACMPerk里程在未来兑换ACM的免费机票。
下表帮助您计算在ACM上飞行时可以赚取多少ACMPerk里程。
当您飞行ACM等级代码时,
您将获得头等舱F实际里程+100%里程奖金商务舱B实际里程
+50%里程奖金经济舱Y 1-500英里500英里500英里以上实际里程
据显示,您的ACMPerk里程由两部分组成。
一个是您的实际飞行里程(经济舱一次飞行的最低ACMPerk里程为500英里),
另一个是商务舱和头等舱飞行时的里程奖励(其准确性高达1英里)。
例如,从北京飞往东京的航班,
Y、B或F级可分别获得1329 ACMPerk里程、1994 ACMPerk英里和2658 ACMPerk公里
(北京和东京之间的实际里程为1329英里)。
当您从上海飞往武汉时,经济舱可获得500英里的ACMPerk里程,
商务舱可获得650英里的ACMPenk里程(上海和武汉之间的实际里程为433英里)。
您的任务是帮助ACM建立一个自动计算ACMPerk里程的程序。
输入描述:
输入文件包含几个数据案例。每个病例都有许多飞行记录,每一行都有。航班记录的格式如下:
原始城市 距离城市 实际里程 分类代码
每种情况都以一行零结束。
输出描述:
输出每个测试用例的ACMPerk里程摘要,每行一个。
计算奖金时,请确保将x.5四舍五入到x+1
*/
#include <iostream>
#include <string>
using namespace std;

int money(int n, char ch) {
    switch (ch) {
        case 'Y':
            if (n > 500) return n;
            else return 500;
        case 'B':
            return (int)(1.5 * n + 0.5);
        case 'F':
            return 2 * n;
    }
    return 0;
}

int main() {
    string p1, p2;
    int distance;
    char rank;
    int total = 0;
    while (cin >> p1) {
        if (p1 == "0")
            break;
        cin >> p2 >> distance >> rank;
        total += money(distance, rank);
    }
    cout << total << endl;
    return 0;
}

发表于 2024-01-31 21:01:23 回复(0)
#include "stdio.h"
#include "string"
using namespace std;

int main(){
    int sum = 0;
    char city1[20],city2[20];
    int mile;
    char Class;
    while (true){
        scanf("%s",city1);
        if (city1[0] == '0')
            break;
        scanf("%s",city2);
        scanf("%d ",&mile);scanf("%c",&Class);
        if (Class == 'Y'){//经济舱
            if (mile < 500)
                sum += 500;
            else
                sum += mile;
        } else if (Class == 'B'){
            if (int(mile)%2 != 0)
                sum += mile + float (mile)/2 +0.5;
            else
                sum += mile + mile/2;
        } else if (Class == 'F'){
            sum += mile*2;
        }
    }
    printf("%d\n",sum);
}

发表于 2023-03-09 10:46:35 回复(0)
#include <stdio.h>
int money(int n,char x)
{
    switch(x){
        case 'Y':
        {if(n>500)
            {
            return n;
            }
         else
             {
             return 500;}
            }
        case 'B':
            {
                return (int)(1.5*n+0.5);
            }
        case 'F':
            {return 2*n;}
    }
}
int main()
{
    char str1[9999],str2[9999],str3[9999];
    char p;
    int n;
    int sum;
    sum=0;
    while(scanf("%s",str1))
    {
        if(str1[0]=='0')
        {
            break;
        }
        scanf("%s",str2);
        scanf("%d",&n);
        scanf("%s",str3);
        sum+=money(n,*str3);
    }
    printf("%d",sum);
    return 0;
}
函数指针好烦呀QAQ
发表于 2020-03-03 21:05:38 回复(0)
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int Mile(int x)
 {return x>500?x:500;
 }
int main()
{string x,y,z;int a;int sum=0;
while(cin>>x)
{if(x=="0"){break;}
  cin>>y>>a>>z;
if(z=="F"){sum+=Mile(a)*2;}
else if(z=="B"){sum+=ceil(Mile(a)*1.5);}
else if(z=="Y"){sum+=Mile(a);}   
  }
cout<<sum<<endl;
	return 0;
}

发表于 2020-02-15 10:22:58 回复(0)
#include <iostream>
#include <string>
#include <math.h>
#include <map>
using namespace std;

map<char,int> Class;
double calculate(int dis,int Rank){
    if(Rank==0){
        if(dis<500)return 500;
        else return dis;
    }
    else if(Rank==1){
        return ceil(dis*1.5);
    }
    else if(Rank==2){
        return ceil(dis*2.0);
    }
}
int main()
{
    string info;
    Class['Y']=0,Class['B']=1,Class['F']=2;
    int ans=0;
    while(getline(cin,info)){
        if(info=="0")break;
        int secondBlank = info.find(" ",info.find(" ",0)+1);
        int thirdBlank = info.find(" ",info.find(" ",info.find(" ",0)+1)+1);
        string dis = info.substr(secondBlank+1,thirdBlank-secondBlank-1);
        string Rank = info.substr(info.size()-1,1);
        ans+=calculate(stoi(dis),Class[Rank[0]]);
    }
    cout<<ans<<endl;
    return 0;
}

编辑于 2019-03-23 21:21:36 回复(0)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
 
using namespace std;
int main()
{
    string s;
    while(cin >> s)
    {
        double ans = 0.0;
        string t; int x; string tmp;
        cin >> t >> x >> tmp;
        if(tmp[0] == 'F')
        {
            ans += 2.0*x;
        }
        else if(tmp[0] == 'B')
        {
            ans += 1.5*x;
        }
        else if(tmp[0] == 'Y')
        {
            if(x>=500)
            ans += 1.0*x;
            else ans += 500.0;
        }
        while(cin >> s)
        {
            if(s == "0")
            break;
            cin >> t >> x >> tmp;
            if(tmp[0] == 'F')
            {
                ans += 2.0*x;
            }
            else if(tmp[0] == 'B')
            {
                ans += 1.5*x;
            }
            else if(tmp[0] == 'Y')
            {
                if(x>=500)
                ans += 1.0*x;
                else ans += 500.0;
            }
        }
        if(ans - (1.0*((int)ans)) > 0)
        cout << (((int)ans)+1) <<endl;
        else
        cout <<(int)ans << endl;            
    }    
} 

发表于 2019-03-01 22:25:30 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str1,str2;
    char ch;
    int sum = 0,n;
    cin >> str1;
    while(str1 != "0")//判断是否结束输入
    {
        cin >> str2 >> n >> ch;
        switch(ch)//对ch进行判断选择不同的计算方式
        {
            case 'F': sum += (2 * n);break;
            case 'B':sum += (int)(n * 1.5 + 0.5);break;//
            case 'Y':if (n > 500)
                        sum += n;
                    else
                        sum += 500;
                    break;
        }
        cin >> str1;
    }
    cout << sum <<endl;
      return 0;
}

发表于 2018-09-22 08:57:00 回复(0)
一道水题,思路没什么好说的,唯一有问题的地方是这道题和POJ1326 Mileage Bank不大一样的地方是没有以#判断是否结束,所以不能判断是否为#来跳出循环(话说题目格式各种乱,还是从网上搜的POJ1326叙述比较清楚,虽然因为结束条件改了一下导致我找了挺久的BUG...)
#include<iostream>
#include<cstring>
using namespace std;
int mile, sum, flag = 1;
string c1, c2;
char cl;

void cal(int mile, char Class)
{
    switch (Class){
    case 'F':
        sum += 2 * mile;
        break;
    case 'B':
        sum += mile * 1.5;
        if (mile % 2 != 0)
            sum++;
        break;
    case 'Y':
        if (mile > 500)
            sum += mile;
        else
            sum += 500;
        break;
    }
}

int main()
{
    while (cin >> c1){
        if (c1 == "0"){
            cout << sum << endl;
            sum = 0;
            continue;
        }
        cin >> c2 >> mile >> cl;
        cal(mile,cl);
    }
    return 0;
}

发表于 2018-06-06 20:31:11 回复(0)
啥头像
#!/usr/bin/python
#-*- coding:utf-8 -*-

#Author: Ben

import math

def getACMPerk(num, sym):
    rlt = 0
    if sym == 'Y':
        if num < 500:
            rlt = 500
        else:
            rlt = num
    elif sym == 'B':
        rlt = num + int(math.ceil(num/2.0))
    else:
        rlt = num*2
    return rlt

first = True
while first:
    rlt = 0
    while True:
        line = raw_input().strip()
        if line == '#':
            first = False
            break
        if line == '0':
            print rlt
            break
        c1, c2, num, sym = line.split()
        rlt += getACMPerk(int(num), sym) 


发表于 2016-04-03 20:09:02 回复(0)

问题信息

难度:
19条回答 3930浏览

热门推荐

通过挑战的用户

查看代码