C++新手问题

2020-11-30 23:57发布

问题:

A city offers the following prices for monthly bus passes:
· Age 0 – 5: $0
· Age 6 – 17: $80
· Age 18+ and less than $15,000 income: $30
· Age 65+ and less than $30,000 income: $70
· City employee: $50
· General population: $100
Write a program that finds the best bus price for a given user.

回答1:

#include <iostream>
using namespace std;

int main()
{
    char c;
    int age,income;
    cout<<"Please input your age: ";
    cin>>age;
    cout<<"Please input your income: ";
    cin>>income;
    cout<<"Are you a City employee? (input Y or N) : ";
    cin>>c;

    // 输出用户的信息,以便用户自查
    cout<<"Age = "<<age<<endl;
    cout<<"Imcome = "<<income<<endl;
    if(c=='Y')  cout<<"You are a City employee."<<endl;
    else if(c=='N') cout<<"You are a General population."<<endl;

    // 判断最低价格
    if(age>=0 && age<=5)
    {
        cout<<"The best bus price for you is $0."<<endl;
    }
    else if(age>=18 && income<15000)
    {
        cout<<"The best bus price for you is $30."<<endl;
    }
    else if(c=='Y')
    {
        cout<<"The best bus price for you is $50."<<endl;
    }
    else if(age>=65 && income<30000)
    {
        cout<<"The best bus price for you is $70."<<endl;
    }
    else if(age>=6 && age<=17)
    {
        cout<<"The best bus price for you is $80."<<endl;
    }
    else
    {
        cout<<"The best bus price for you is $100."<<endl;
    }
}
展开代码内容


标签: c++