题解 | #分段函数#
分段函数
https://www.nowcoder.com/practice/af1d874fb54d4989ae868959bdda9894
#include <cstdio> #include <iostream> using namespace std; double f(int x) { double y; 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.0 - 1.5; } return y; } int main() { int m, x; cin >> m; while (m--) { cin >> x; printf("y=%.1f\n", f(x)); } return 0; }