2013-C++第13周班级对抗赛题目及参考解答

来源:互联网 发布:程序员学历要求高吗 编辑:程序博客网 时间:2024/05/02 02:45

课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759


Problem  A  她胖吗?

Description
奚嘉嘉是位爱美的女孩,身高165cm,体重52.5kg,可还是为保持身材错过了好多口福。实际上,保持健康和快乐,无论胖瘦都是美。当然太胖或太瘦可能对健康不利,适当注意即可。我们要为奚嘉嘉这样的爱美女孩设计一个程序,提供身体、体重,给出建议。女性的标准体重是:身高(厘米)-100= 标准体重(公斤),超过标准体重20%以上者为肥胖,低于标准体重20%的为偏瘦,在肥胖与偏瘦之间为正常。
Input
身高(cm)与体重(kg)值,身高整数,体重是小数
Output
肥胖时输出high,正常时输出normal,偏瘦要输出low。(注意全用小写)
Sample Input
165 52.5
Sample Output
normal
参考解答:
#include <iostream>#include<cstdio>#include <iomanip>using namespace std;int main(){    cin>>height>>weight;    st = height - 100;    if(weight/st>1.2)        cout<<"high\n";    else if((weight/st<0.8))        cout<<"low\n";    else        cout<<"normal\n";    return 0;}

Problem  B   股市风云

Description
股市强烈动荡,有涨有跌。现在有一组数据表示各公司的涨跌(涨为正,跌为负,不动为零),要求统计出平均涨幅和平均跌幅。
Input
一组数,其中有正数,也有负数,还有0。输入的个数不定,另外,不会出现只有正数或只有负数的情况。
Output
第一行输出见涨的数目和遇跌的数目;
第二行输出平均涨幅(正数的平均数)和平均跌幅(负数的平均数,再取反),保留小数点后3位。
Sample Input
5 0 -1 1.5 2.3 -0.3 2.4 0 7.9 -4.3
Sample Output
5 3
3.820 1.867
HINT
(1)用于处理不定数目的输入,参考:
int main()
{
    int a,b;
    while(cin >>a)
    {
        cout << a << endl;
    }
    return 0;
}
(2)输出x的值,保留两位小数,用:
cout<<setiosflags(ios::fixed)<<setprecision(3)<<x<<endl;
参考解答:(在调试时,最后一个数输完按CTRL_Z)
#include <iostream>#include<cstdio>#include <iomanip>using namespace std;int main(){    int c_inc=0, c_dec=0;  //涨的个数和跌的个数    //freopen("input.txt","r",stdin);    double v, s_inc=0, s_dec=0;  //用于涨跌值和用于求和的变量    while(cin>>v)    {        if(v>0)        {            s_inc+=v;            c_inc++;        }        else if(v<0)        {            s_dec+=v;            c_dec++;        }    }    cout<<c_inc<<" "<<c_dec<<endl;    cout<<setiosflags(ios::fixed)<<setprecision(3)<<(s_inc/c_inc)<<" "<<(-s_dec/c_dec)<<endl;    return 0;}

Problem  C   麦克劳林用于函数求值

Description
泰勒公式是一个用函数在某点的信息描述其附近取值的公式。如果函数足够光滑的话,在已知函数在某一点的各阶导数值的情况之下,泰勒公式可以用这些导数值做系数构建一个多项式来近似函数在这一点的邻域中的值。函数的麦克劳林展开是泰勒公式的特殊形式,即泰勒公式中“某一点”取0的情况。下面是e^x的麦克劳林展开式,据此求出多组e^x的值。
 
精度要求:最后一项大于1e-7
Input
输入一个整数N,代表输入数据的组数,后随N行,每行一个小数,分别代表一个x
Output
输出N行,分别代表各行x对应的ex的值。输出精确到小数点后第7位。
Sample Input
5
0
1
1.5
0.3
2.4
Sample Output
1.0000000
2.7182818
4.4816890
1.3498588
11.0231763
HINT
在C++中,cout<<setiosflags(ios::fixed)<<setprecision(7)<<y<<endl;输出的y值保留7位小数。
参考解答:
#include <iostream>#include<cstdio>#include <iomanip>using namespace std;int main( ){    int i, n, k;    double x, ex, item;    //freopen("input.txt","r",stdin);    cin>>n;    for(i=0; i<n; i++)    {        cin>>x;        ex=1;        item=x;        k=1;        while(item>1e-7)        {            ex+=item;            k++;            item=item*x/k;            //cout<<item<<endl;        }        cout<<setiosflags(ios::fixed)<<setprecision(7)<<ex<<endl;    }    return 0;}

Problem  D   我想有套北京的房

Description
小原是一个软件工程师,名叫原黛玛,他在北京工作。现在有一套房子,价格200万,房价每年上涨10%,每年固定能赚40万。他想知道要想买下这套房子,不贷款,不涨工资,没有其他收入,每年不吃不喝不消费,20年之内是否能攒够钱买下这套房子?
Input
房子的价格(万),房价上涨率,每年收入(万)。
Output
20年之内是否能买下房子的结论,能买下输出'Y',买不起输出'N'。
Sample Input
200 0.1 40
Sample Output
N
HINT
在调试过程中可以写一些输出语句观察计算过程,不过在最后提交前,将多余的输出删除。
参考解答:
#include <iostream>#include<cstdio>using namespace std;int main( ){    double money;  //积蓄的钱    double housePrice,rate, salary;  //房价、房价涨幅、年收入    //freopen("input.txt","r",stdin); //重定向输入,从文件中读数据    cin>>housePrice>>rate>>salary;    money=salary;    int year=1;    while(year<=20 && money<housePrice)    {        housePrice*=(1+rate);  //下一年房价        money+=salary;       //下一年积蓄        year++;    }    if(year>20)        cout<<'N'<<endl;  //调试中,可以看看到底是否已经超出20年    else        cout<<'Y'<<endl; //调试中,可以看看到底几年能买上    return 0;}

调试用的代码,加入了调试中可能需要对变量的观察(多余的输出要在OJ上提交删除):
#include <iostream>#include<cstdio>using namespace std;int main( ){    double money;  //积蓄的钱    double housePrice,rate, salary;  //房价、房价涨幅、年收入    freopen("input.txt","r",stdin); //重定向输入,从文件中读数据    cin>>housePrice>>rate>>salary;    money=salary;    int year=1;    while(year<=20 && money<housePrice)    {        cout<<"第"<<year<<"年,房子值 "<<housePrice<<"万,程序员有 "<<money<<"万元"<<endl;  //调试中输入这一行,可以看出变化趋势,当然提交时要删除或给这行语句加上注释        housePrice*=(1+rate);  //下一年房价        money+=salary;       //下一年积蓄        year++;    }    if(year>20)        cout<<year<<'N'<<endl;  //调试中,可以看看到底是否已经超出20年    else        cout<<year<<'Y'<<endl; //调试中,可以看看到底几年能买上     return 0;}

Problem  E   北京买房方案辅助决策

Description
小原是一个软件工程师,名叫原黛玛,他在北京工作。现在有一套房子,价格200万,房价每年上涨10%,每年固定能赚40万。他想知道要想买下这套房子,不贷款,不涨工资,没有其他收入,每年不吃不喝不消费,20年之内是否能攒够钱买下这套房子?他还想知道,如果房子的价格、房价上涨率、每年收入可以有其他选择的话会如何。
Input
多行数据,每一行数据包括了一组房子的价格(万),房价上涨率,每年收入(万)。
Output
20年之内是否能买下房子的结论,能买下输出'Y',买不起输出'N'。针对每一组数据对应的输出,单独占一行。
Sample Input
200 0.05 40
200 0.1 40
100 0.8 200
1000 0.01 100
Sample Output
Y
N
Y
Y
HINT
在调试过程中可以写一些输出语句观察计算过程,不过在最后提交前,将多余的输出删除。
参考解答:
#include <iostream>#include<cstdio>using namespace std;int main( ){    double money;  //积蓄的钱    double housePrice,rate, salary;  //房价、房价涨幅、年收入    //freopen("input.txt","r",stdin);    while(cin>>housePrice>>rate>>salary)    {        money=salary;        int year=1;        while(year<=20 && money<housePrice)        {            housePrice*=(1+rate);  //下一年房价            money+=salary;       //下一年积蓄            year++;         }        if(year>20)            cout<<'N'<<endl;        else            cout<<'Y'<<endl;    }    return 0;}

调试用的代码,加入了调试中可能需要对变量的观察(多余的输出要在OJ上提交删除):
#include <iostream>#include<cstdio>using namespace std;int main( ){    double money;  //积蓄的钱    double housePrice,rate, salary;  //房价、房价涨幅、年收入    //freopen("input.txt","r",stdin); //重定向输入,从文件中读数据    while(cin>>housePrice>>rate>>salary)    {        money=salary;           int year=1;        while(year<=20 && money<housePrice)        {            cout<<"第"<<year<<"年,房子值 "<<housePrice<<"万,程序员有 "<<money<<"万元"<<endl;  //调试中输入这一行,可以看出变化趋势,当然提交时要删除或给这行语句加上注释            housePrice*=(1+rate);  //下一年房价            money+=salary;       //下一年积蓄            year++;        }        if(year>20)            cout<<year<<'N'<<endl;  //调试中,可以看看到底是否已经超出20年         else            cout<<year<<'Y'<<endl; //调试中,可以看看到底几年能买上    }    return 0;}


==================== 迂者 贺利坚 CSDN博客专栏=================

|==  IT学子成长指导专栏  专栏文章分类目录(不定期更新)    ==|

|== C++ 课堂在线专栏   贺利坚课程教学链接(分课程年级)   ==|

======== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =======



原创粉丝点击