首页 > 试题广场 >

分段函数

[编程题]分段函数
  • 热度指数:10544 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
编写程序,计算下列分段函数y=f(x)的值。
当 0<= x <2,y= -x+2.5;

 2<= x <4y=2-1.5(x-3)(x-3)

 4<= x <6y=x/2-1.5


输入描述:
输入第一行为整数m表示样例数,接下来有m行每行一个整数x。


输出描述:
输出m行分别表示对应的y值,保留小数点后一位小数。
示例1

输入

2
1
3

输出

y=1.5
y=2.0
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
    int m,x;
    cin>>m;
    while(m>0){
        cin>>x;
        double y;
        if(0<=x&&x<2)
            y=-1.0*x+2.5;
        else if(2<=x&&x<4)
            y=2-1.5*(x-3)*(x-3);
        else if(4<=x&&x<6)
            y=1.0*x/2-1.5;
        cout<<"y="<<fixed<<setprecision(1)<<y<<endl;
        m--;
    }
}

发表于 2019-03-01 10:21:01 回复(0)
#include<stdio.h>
int main()
{
    int m,i;
    double x,y;
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        scanf("%lf",&x);
        if(x>=0&&x<2)
        {
            y=-x+2.5;
        }
        if(x>=2&&x<4)
        {
            y=2-1.5*(x-3)*(x-3);
        }
        if(x>=4&&x<6)
        {
            y=x/2-1.5;
        }
        printf("y=%.1f\n",y);
    }
    return 0;
}

发表于 2020-05-09 16:16:46 回复(0)

#include<stdio.h>
int main()
{
int m;
double x, y;
scanf("%d", &m);
while(m)
{
scanf("%lf", &x);
if( 0<=x && x<2 )
y = -x+2.5;
else if ( 2<=x && x<4 )
y = 2.0-1.5*(x-3.0) *(x-3.0);
else if ( 4<=x && x<6)
y = x/2.0-1.5;
printf("y=%.1lf\n", y);
m--;
}
}

发表于 2019-06-26 11:35:31 回复(0)
# include<iostream>
# include<iomanip>
using namespace std;

int main(){
    int m;
    cin >> m;
    int temp=0;
    for (int i=0; i<m; i++){
        cin >> temp;
        if (0<=temp && temp<2) cout <<"y="<< setiosflags(ios::fixed) << setprecision(1)<< -1*temp+2.5 << endl;
        else if (2<=temp && temp<4) cout <<"y="<< setiosflags(ios::fixed) << setprecision(1)<< 2-1.5*(temp-3)*(temp-3) << endl;
        else cout << setiosflags(ios::fixed) <<"y="<< setprecision(1)<< float(temp)/2.0-1.5 << endl;
    }
    return 0;
}

编辑于 2019-02-27 14:33:00 回复(0)
这一题对我来说主要难在计算 y=x/2-1.5 这个算式,刚开始我写的算式是y = x/2 - 1.5, 一直无法通过,后来看了榛子老哥的答案才发现这里在x除2之前应该先乘一个1.0把它转化为小数,不然一个int除以2小数部分会舍去,就无法得出正确答案。
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    int m;
    cin >> m;
    for(int i = 0;i < m;i++) {
        int x;
        double y;
        cin >> x;
        if(x >= 0 && x < 2)
            y = -1.0 * x + 2.5;
        else if(x >= 2 && x < 4)
            y = 2-1.5*(x-3)*(x-3);
        else if(x >= 4 && x < 6)
            y = 1.0 * x/2 - 1.5;
        printf("y=%.1f\n",y);
        //cout << "y=" << setiosflags(ios::fixed) << setprecision(1) << y << endl;
        //cout<<"y="<<fixed<<setprecision(1)<<y<<endl;
    }
}

编辑于 2019-03-01 17:43:28 回复(1)
偷鸡解法,其实只有六种情况🤣
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    while(a>0){
        int x,y;
        cin>>x;
        if(x==0)
            cout<<"y=2.5"<<endl;
        if(x==1)
            cout<<"y=1.5"<<endl;
        if(x==2)
            cout<<"y=0.5"<<endl;
        if(x==3)
            cout<<"y=2.0"<<endl;
        if(x==4)
            cout<<"y=0.5"<<endl;
        if(x==5)
            cout<<"y=1.0"<<endl;
    a--;
    }
}

发表于 2021-02-07 16:43:21 回复(1)
#include <iostream>
#include <cstdio>
using namespace std;
float my_fun(float x)
{
    if(x >= 0 && x < 2)
        return -x+2.5;
    else if(x >= 2 && x < 4)
        return 2-1.5*(x-3)*(x-3);
    else
        return x/2-1.5;
}

int main()
{
    int m;
    cin >> m;
    float input[500];
    for(int i = 0; i < m; i++)
        cin >> input[i];

    for(int i = 0; i < m; i++)
        printf("y=%.1f\n", my_fun(input[i]));
    return 0;
}

简单的判断和循环

编辑于 2019-03-16 10:24:28 回复(0)
#include <cstdio>
#include <iostream>
using namespace std;

double f(int x) {
    double y;
    if (x >= 0 && x < 2) {
        y = -x + 2.5;
    } else if (x >= 2 && x < 4) {
        y = 2 - 1.5 * (x - 3) * (x - 3);
    } else if (x >= 4 && x < 6) {
        y = x / 2.0 - 1.5;
    }
    return y;
}

int main() {
    int m, x;
    cin >> m;
    while (m--) {
        cin >> x;
        printf("y=%.1f\n", f(x));
    }
    return 0;
}

发表于 2024-01-28 21:18:18 回复(0)
#include <iostream>
using namespace std;

int main() {
int m;
float x;
float y;
cin >> m;
while (m--) { // 注意 while 处理多个 case
cin >> x;
if (x>=0 && x<2) printf("y=%.1f\n",-x+2.5);
else if(x>=2 && x<4) printf("y=%.1f\n",2-1.5*(x-3)*(x-3));
else if(x>=4 && x<6) printf("y=%.1f\n",x/2 -1.5);
}
return 0;
}
// 64 位输出请用 printf("%lld")
发表于 2024-01-21 17:47:07 回复(0)
#include <cstdio>

float function(int x){
    if(0<=x && x<2){
        return -x+2.5;
    }else if(2<=x && x<4){
        return 2-1.5*(x-3)*(x-3);
    }else if(4<=x && x<6){
        return 0.5*x-1.5;
    }else{
        return 0;
    }
}

int main(){
    int m;
    scanf("%d",&m);
    for(int i = 0; i < m; ++i){
        int x;
        scanf("%d",&x);
        printf("y=%.1f\n",function(x));
    }
    return 0;
}

发表于 2023-03-25 21:21:26 回复(0)
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
	int n;
	scanf("%d", &n);
	while(n--) {
		float x;
		scanf("%f", &x);
		float y = 0;
		if(0 <= x && x < 2) {
			y = 2.5 - x;
		}
		else if(x < 4) {
			y = 2 - 1.5 * (x - 3) * (x - 3);
		}
		else {
			y = x / 2 - 1.5;
		}
		printf("y=%.1f\n", y);
	}
	return 0;
} 

发表于 2022-04-04 16:51:32 回复(0)
#include<iostream>
#include<vector>
using namespace std;
double f(double x)
{
    double y;
    if(x >= 0 && x < 2)
        y = 2.5 - x;
    if(x >= 2 && x < 4)
        y = 2 - 1.5 * (x -3) * (x - 3);
    if(x >= 4 && x < 6)
        y = x / 2 - 1.5;
    return y;
}

int main(void)
{
    int m;
    while(cin >> m)
    {
        vector<double> arr;
        while(m--)
        {
            double x;
            cin >> x;
            arr.push_back(x);
        }
        
        for(vector<double>::iterator it = arr.begin();it != arr.end();it++)
        {
            double y = f(*it);
            cout << "y=";
            printf("%.1lf",y);//直接用c的格式化输出
            cout << endl;
        }
     }
    return 0;
}

发表于 2022-02-05 17:19:49 回复(0)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;

int main()
{
	int m;
	cin>>m;
	int x;
	while(m--)
	{
		cin>>x; 
		if(x>=0&&x<2)
			printf("y=%.1f\n",-x+2.5);
		else if(x>=2&&x<4)
			printf("y=%.1f\n",2-1.5*(x-3)*(x-3));
		else if(x>=4&&x<6)
			printf("y=%.1f\n",x*1.0/2-1.5);
	}
	return 0;
}


编辑于 2021-01-02 15:10:58 回复(0)
#include<stdio.h>
float cacul(int x)
{
	float y;
	if(x>=0&&x<2)
		y=2.5-(float)x;
	if(x>=2&&x<4)
		y=2-1.5*((float)x-3)*((float)x-3);
	if(x>=4&&x<6)
		y=(float)x/2-1.5;
	return y;
}
int main()
{
	int m,x[1000],i;
	scanf("%d",&m);
	for(i=0;i<m;i++)
	scanf("%d",&x[i]);
	for(i=0;i<m;i++)
	printf("y=%.1f\n",cacul(x[i]));
	return 0;	
 }
先强制类型转换
发表于 2020-06-06 17:50:42 回复(0)
    int m,x;
    float y;
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&x);
        if(x>=0&&x<2)
            y=-x+2.5;
        else if(x>=2&&x<4)
            y=2-1.5*(x-3)*(x-3);
        else if(x>=4&&x<6)
            y=(float)x/2-1.5;
        printf("y=%.1f\n",y);
    }

发表于 2020-04-17 19:22:03 回复(0)
#include <stdio.h>
#include <stdlib.h>

float func(float);

int main()
{
    int m;
    while(~scanf("%d", &m))
    {
        int i;
        for(i = 0; i < m; i++)
        {
            float data;
            scanf("%f", &data);
            printf("y=%.1f\n", func(data));
        }
    }

    return 0;
}

float func(float x)
{
    if(x >= 0 && x < 2)
        return (2.5 - x);
    if(x >= 2 && x < 4)
        return (2 - 1.5 * (x - 3) * (x - 3));
    return (x / 2.0 - 1.5);
}

编辑于 2020-04-02 22:43:59 回复(0)
Java
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        for (int i = 0; i < m; i++) {
            double x = scanner.nextDouble();
            DecimalFormat f = new DecimalFormat("0.0");
            if (x>=0&&x<2) System.out.println("y="+f.format(-x+2.5));
            else if (x>=2&&x<4) System.out.println("y="+f.format(2-1.5*(x-3)*(x-3)));
            else if (x>=4&&x<6) System.out.println("y="+f.format(x/2-1.5));
        }
    }
}


发表于 2020-03-20 09:09:00 回复(0)
int 换成double 好用
#include <stdio.h>
(737)#include <stdlib.h>
#include <math.h>
int main()
{
   int m;
   while(scanf("%d",&m)!=EOF)
   {
       double x[m];//用int乘除时会出事
       double y[m];
       int i;
       for(i=0;i<m;i++)
       {
           scanf("%lf",&x[i]);
       if(x[i]>=0&&x[i]<2)
       {
           y[i]=-x[i]+2.5;
       }
       else if(x[i]>=2&&x[i]<4)
       {
           y[i]=2-1.5*(x[i]-3)*(x[i]-3);
       }
       else if(x[i]>=4&&x[i]<6)
       {
           y[i]=x[i]/2-1.5;
       }
       }
       for(i=0;i<m;i++)
        printf("y=%.1lf\n",y[i]);
   }
    return 0;
}

发表于 2020-03-17 10:33:13 回复(0)
#include<stdio.h>
double f(double x)
{
    double y;
    if((x>=0)&&(x<2))
         {
          y=-1.0*x+2.5;
         }
        else if((x>=2)&&(x<4))
         {
          y=2-1.5*(x-3)*(x-3);
         }
        else if((x>=4)&&(x<6))
        {
            y=1.0*x/2-1.5;
        }
         printf("y=%.1f\n",y);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        double x,y;
        scanf("%lf",&x);
        y=f(x);
    }
    
}
发表于 2020-01-18 19:56:45 回复(0)
#include<stdio.h>

void func(int x) {
    if (0 <= x && x < 2) {
        printf("y=%.1f\n", -1 * x + 2.5);
    } else if (2 <= x && x < 4) {
        printf("y=%.1f\n", 2 - 1.5 * (x - 3) * (x - 3));
    } else if (4 <= x && x < 6) {
        printf("y=%.1f\n", x / 2.0 - 1.5);
    }
}

int main() {
    int m, x;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d", &x);
        func(x);
    }
    return 0;
}

发表于 2019-09-18 18:47:20 回复(0)

问题信息

上传者:小小
难度:
35条回答 4225浏览

热门推荐

通过挑战的用户

查看代码