Segmented function-2001 the computer research of Tsinghua University The real Problem (I set)

Source: Internet
Author: User

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


Problem Solving Code:

#include <stdio.h>intMain () {Doublex; Doubley;  while(SCANF ("%LF", &x)! =EOF) {        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-1.5; }}} printf ("%.3lf\n", y); }    return 0;}

AC

To do the topic:

Practice Feel

Algorithm Analysis:

Water problem, very simple, examines the use of basic if else nesting, while examining character input and output control.

Segmented function-2001 the computer research of Tsinghua University The real Problem (I set)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.