Topic 1057: Level 2 C language-piecewise function

There is a function as follows. Write a program that inputs x and outputs y value.

Level 2 C language - piecewise function

Keep to two decimal places

Sample input

1

Sample output

1.00

The idea of ​​​​this question is very simple. I directly use if to determine the interval of the function Y corresponding to the input X , substitute the corresponding function, and obtain the result. Remember to use floating point type for variables (to preserve two decimal places).

#include<iostream>
#include<iomanip>  //专门保留小数的头文件
using namespace std;


//温度转换
int main() {
	double X, Y;
	cin >> X;
	
	if (X < 1) {   //分三段求出不同的结果
		Y = X;
	}
	else if (X >= 1 && X < 10) {
		Y = 2 * X - 1;
	}
	else if (X>=10) {
		Y = 3 * X - 11;
	}

	cout << fixed <<setprecision(2) << Y << endl;
	//printf("%.2f",C);

	return 0;
}

Guess you like

Origin blog.csdn.net/qq_63999224/article/details/132934447