编写程序,计算下列分段函数y=f(x)的值。
当 0<= x <2,y= -x+2.5;
当 2<= x <4,y=2-1.5(x-3)(x-3);
当 4<= x <6,y=x/2-1.5;
当 2<= x <4,y=2-1.5(x-3)(x-3);
当 4<= x <6,y=x/2-1.5;
输入第一行为整数m表示样例数,接下来有m行每行一个整数x。
输出m行分别表示对应的y值,保留小数点后一位小数。
2 1 3
y=1.5 y=2.0
#include<iostream> #include<iomanip> using namespace std; int main(){ int m,x; cin>>m; while(m>0){ cin>>x; double y; if(0<=x&&x<2) y=-1.0*x+2.5; else if(2<=x&&x<4) y=2-1.5*(x-3)*(x-3); else if(4<=x&&x<6) y=1.0*x/2-1.5; cout<<"y="<<fixed<<setprecision(1)<<y<<endl; m--; } }
# include<iostream> # include<iomanip> using namespace std; int main(){ int m; cin >> m; int temp=0; for (int i=0; i<m; i++){ cin >> temp; if (0<=temp && temp<2) cout <<"y="<< setiosflags(ios::fixed) << setprecision(1)<< -1*temp+2.5 << endl; else if (2<=temp && temp<4) cout <<"y="<< setiosflags(ios::fixed) << setprecision(1)<< 2-1.5*(temp-3)*(temp-3) << endl; else cout << setiosflags(ios::fixed) <<"y="<< setprecision(1)<< float(temp)/2.0-1.5 << endl; } return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { int m; cin >> m; for(int i = 0;i < m;i++) { int x; double y; cin >> x; if(x >= 0 && x < 2) y = -1.0 * x + 2.5; else if(x >= 2 && x < 4) y = 2-1.5*(x-3)*(x-3); else if(x >= 4 && x < 6) y = 1.0 * x/2 - 1.5; printf("y=%.1f\n",y); //cout << "y=" << setiosflags(ios::fixed) << setprecision(1) << y << endl; //cout<<"y="<<fixed<<setprecision(1)<<y<<endl; } }
#include<bits/stdc++.h> using namespace std; int main(){ int a; cin>>a; while(a>0){ int x,y; cin>>x; if(x==0) cout<<"y=2.5"<<endl; if(x==1) cout<<"y=1.5"<<endl; if(x==2) cout<<"y=0.5"<<endl; if(x==3) cout<<"y=2.0"<<endl; if(x==4) cout<<"y=0.5"<<endl; if(x==5) cout<<"y=1.0"<<endl; a--; } }
#include <iostream>
#include <cstdio>
using namespace std;
float my_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
return x/2-1.5;
}
int main()
{
int m;
cin >> m;
float input[500];
for(int i = 0; i < m; i++)
cin >> input[i];
for(int i = 0; i < m; i++)
printf("y=%.1f\n", my_fun(input[i]));
return 0;
}
简单的判断和循环
#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; }
#include <cstdio> float function(int x){ if(0<=x && x<2){ return -x+2.5; }else if(2<=x && x<4){ return 2-1.5*(x-3)*(x-3); }else if(4<=x && x<6){ return 0.5*x-1.5; }else{ return 0; } } int main(){ int m; scanf("%d",&m); for(int i = 0; i < m; ++i){ int x; scanf("%d",&x); printf("y=%.1f\n",function(x)); } return 0; }
#include <iostream> #include <cstdio> using namespace std; int main() { int n; scanf("%d", &n); while(n--) { float x; scanf("%f", &x); float y = 0; if(0 <= x && x < 2) { y = 2.5 - x; } else if(x < 4) { y = 2 - 1.5 * (x - 3) * (x - 3); } else { y = x / 2 - 1.5; } printf("y=%.1f\n", y); } return 0; }
#include<iostream> #include<vector> using namespace std; double f(double x) { double y; if(x >= 0 && x < 2) y = 2.5 - x; if(x >= 2 && x < 4) y = 2 - 1.5 * (x -3) * (x - 3); if(x >= 4 && x < 6) y = x / 2 - 1.5; return y; } int main(void) { int m; while(cin >> m) { vector<double> arr; while(m--) { double x; cin >> x; arr.push_back(x); } for(vector<double>::iterator it = arr.begin();it != arr.end();it++) { double y = f(*it); cout << "y="; printf("%.1lf",y);//直接用c的格式化输出 cout << endl; } } return 0; }
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 1000000000; const int maxn = 100; int main() { int m; cin>>m; int x; while(m--) { cin>>x; if(x>=0&&x<2) printf("y=%.1f\n",-x+2.5); else if(x>=2&&x<4) printf("y=%.1f\n",2-1.5*(x-3)*(x-3)); else if(x>=4&&x<6) printf("y=%.1f\n",x*1.0/2-1.5); } return 0; }
#include<stdio.h> float cacul(int x) { float y; if(x>=0&&x<2) y=2.5-(float)x; if(x>=2&&x<4) y=2-1.5*((float)x-3)*((float)x-3); if(x>=4&&x<6) y=(float)x/2-1.5; return y; } int main() { int m,x[1000],i; scanf("%d",&m); for(i=0;i<m;i++) scanf("%d",&x[i]); for(i=0;i<m;i++) printf("y=%.1f\n",cacul(x[i])); return 0; }先强制类型转换
#include <stdio.h> #include <stdlib.h> float func(float); int main() { int m; while(~scanf("%d", &m)) { int i; for(i = 0; i < m; i++) { float data; scanf("%f", &data); printf("y=%.1f\n", func(data)); } } return 0; } float func(float x) { if(x >= 0 && x < 2) return (2.5 - x); if(x >= 2 && x < 4) return (2 - 1.5 * (x - 3) * (x - 3)); return (x / 2.0 - 1.5); }
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); for (int i = 0; i < m; i++) { double x = scanner.nextDouble(); DecimalFormat f = new DecimalFormat("0.0"); if (x>=0&&x<2) System.out.println("y="+f.format(-x+2.5)); else if (x>=2&&x<4) System.out.println("y="+f.format(2-1.5*(x-3)*(x-3))); else if (x>=4&&x<6) System.out.println("y="+f.format(x/2-1.5)); } } }
int 换成double 好用 #include <stdio.h> (737)#include <stdlib.h> #include <math.h> int main() { int m; while(scanf("%d",&m)!=EOF) { double x[m];//用int乘除时会出事 double y[m]; int i; for(i=0;i<m;i++) { scanf("%lf",&x[i]); if(x[i]>=0&&x[i]<2) { y[i]=-x[i]+2.5; } else if(x[i]>=2&&x[i]<4) { y[i]=2-1.5*(x[i]-3)*(x[i]-3); } else if(x[i]>=4&&x[i]<6) { y[i]=x[i]/2-1.5; } } for(i=0;i<m;i++) printf("y=%.1lf\n",y[i]); } return 0; }
#include<stdio.h> void func(int x) { if (0 <= x && x < 2) { printf("y=%.1f\n", -1 * x + 2.5); } else if (2 <= x && x < 4) { printf("y=%.1f\n", 2 - 1.5 * (x - 3) * (x - 3)); } else if (4 <= x && x < 6) { printf("y=%.1f\n", x / 2.0 - 1.5); } } int main() { int m, x; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &x); func(x); } return 0; }