题解 | #分段函数#
分段函数
https://www.nowcoder.com/practice/af1d874fb54d4989ae868959bdda9894
#include <iostream> using namespace std; float fun (float x){ if (x >= 0 && x < 2){ return -x + 2.5; }else if (x >=2 && x < 4){ return 2 - 1.5 * (x - 3) * (x - 3); }else if (x >= 4 && x < 6){ return x / 2 - 1.5; } return 0; } int main () { int m; while (cin >> m){ for (int i = 0; i < m; i++){ int x = 0; cin >> x; printf ("y=%.1lf\n", fun(x)); } } return 0; }