Topic 1062: Segmented functions
time limit:1 seconds
Memory limit:32 MB
Special question: No
submitted:2877
Resolution:1671
-
Title Description:
-
Write a program that calculates the value of the following segment function, Y=f (x).
y=-x+2.5; 0<=x<2
y=2-1.5 (x-3) (x-3); 2<=x<4
y=x/2-1.5; 4<=x<6
-
Input:
-
A floating-point number n
-
Output:
-
The test data may have multiple groups, for each set of data,
Output N corresponds to the piecewise function value: F (n). Results retain three decimal places
-
Sample input:
-
1
-
Sample output:
-
1.500
#include <stdio.h> #include <stdlib.h> #include <string.h>double y (double x) { double result; if (0<=x&&x<2.0) { result=-1*x+2.5; } else if (2<=x&&x<4) { result=2-1.5* (x-3.0) * (x-3.0); } else{ result=x/2-1.5; } return result;} int main (int argc, char *argv[]) { freopen ("1062.in", "R", stdin); Double N; while (~SCANF ("%lf", &n)) { printf ("%.3lf\n", Y (N)); } return 0;}
Nine degree OJ 1062 segment function (analog)