HDU 10453 分段函数

分段函数
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 328, Accepted users: 261
Problem 10453 : No special judgement
Problem description
  有一函数

 

           x      (x<1)

   y=     2x-1   (1<=x<10)

          3x-11  (x>=10)

   编写程序根据x的值输出y的值。

Input
  输入将包含若干组数据,第一行为一个整数n,代表下面共有n组数据。接下来的每一行为一个x(共n行),代表一组数据。

Output
  对于每组输入的数据x,输出其y值,每组数据输出一行。计算结果如果为小数,小数点后保留1位有效数字,四舍五入。

Sample Input
2
5
30
Sample Output
9
79

 

 

#include<iostream>
#include<stdlib.h>
#include <iomanip>


using namespace std;
int main()
{
    int n;
    double x;
    cin>>n;
    while(n--)
    {
        cin>>x;
        if(x<1)
        {
                        cout<<x<<endl;
        }
        else if(x>=10)
        {
                cout<<(3*x-11)<<endl;
        }
        else
        {
                cout<<(2*x-1)<<endl;
        }
    }
    return 0;
}


 

 

理论上应该是这样的:

#include<iostream>
#include<stdlib.h>
#include <iomanip>


using namespace std;
int main()
{
    int n;
    double x;
    cin>>n;
    while(n--)
    {
        cin>>x;
        if(x<1)
        {
            if((int)x==x)
                cout<<fixed<<setprecision(0)<<x<<endl;
            else
                cout<<fixed<<setprecision(1)<<x<<endl;
        }
        else if(x>=10)
        {
            if((int)x==x)
                cout<<fixed<<setprecision(0)<<(3*x-11)<<endl;
            else
                cout<<fixed<<setprecision(1)<<(3*x-11)<<endl;
        }
        else
        {
            if((int)x==x)
                cout<<fixed<<setprecision(0)<<(2*x-1)<<endl;
            else
                cout<<fixed<<setprecision(1)<<(2*x-1)<<endl;
        }
    }
    system("pause");
    return 0;
}

 

posted @ 2013-11-23 17:06  褪色的狼  阅读(315)  评论(0编辑  收藏  举报