1 条题解

  • 0
    @ 2025-4-12 21:50:59

    C :

    #include <stdio.h>
    int main ()
    {
    	double x,y=0,m;
    	scanf("%lf",&m);
    	while(m--)
    	{
    		scanf("%lf",&x);
    		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;
    		}
    		printf("y=%.1f\n",y);
    	}
    
    }
    

    C++ :

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

    Pascal :

    Program TK1999;
    var m,x,i:longint;
    
    Begin 
    	readln(m);
    	for i:=1 to m do begin 
    		readln(x); write('y=');
    		if (0<=x)and(x<2) then writeln((-x+2.5):0:1)
    			else if (2<=x)and(x<4) then writeln((2-1.5*(x-3)*(x-3)):0:1)
    				else if (4<=x)and(x<6) then writeln((x/2-1.5):0:1);
    	end;
    End.
    

    Java :

    import java.util.*;
    
    public class Main{
    	public static void main (String[] args) {
    		Scanner in=new Scanner(System.in);
    		double s;
    		int n=in.nextInt();
    		for(int i=1;i<=n;i++){
    			double x=in.nextDouble();
    			if(x>=0&&x<2){
    				s=2.5-x;
    				System.out.println ("y="+String.format("%.1f",s));
    			}
    			if(x>=2&&x<4){
    				s=2-1.5*(x-3)*(x-3);
    				System.out.println ("y="+String.format("%.1f",s));
    			}
    			if(x>=4&&x<6){
    				s=x/2-1.5;
    				System.out.println ("y="+String.format("%.1f",s));
    			}
    		}
    	}
    }
    
    
    • 1

    信息

    ID
    1880
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者