Piecewise function evaluation
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
There are the following piecewise functions
F(x) = x^2 + 1 When x> 0 Time ;
F(x) = -x When x<0 Time ;
F(x) = 100.0 When x=0 Time ;
Programming depends on the input x(x Is a real number ), Output its corresponding function value
Input
Multi group input , Each group has one real number x. Process to the end of the file .
Output
For each group of inputs x, Output its corresponding F(x), One line per group , Results reserved 1 Decimal place .
Sample Input
8.00
-5.0
Sample Output
65.0
5.0
Hint
import java.text.DecimalFormat; import java.util.Scanner; public class Main {
public static void main(String[] args) { Scanner str = new Scanner(System.in);
DecimalFormat df =new DecimalFormat("0.0"); double x, y; while(str.hasNext()) {
x = str.nextDouble();if(x > 0) { y = x * x + 1; } else if(x < 0) { y = - x; }
else y = 100.0; System.out.println(df.format(y)); } str.close(); } }
Technology