一个三段式函数
y=x+3(x<0)
y=-2x平方+5(0<=x<3)
y=6-3分之x(x>=3)
要求输入输出均为整数
C语言
#include<stdio.h>
int hanshu(int x)
{
int y=0;
if(x<0){
y=x+3;
}else if(x>=0 && x<3){
y=-2*x*x+5;
}else{
y=6-x/3;
}
return y;
}
int main()
{
int x,y;
while(scanf("%d",&x)!=-1)
{
y=hanshu(x);
printf("%d\n",y);
}
return 0;
}
c++
#include<iostream>
//#include<iomanip> //setw 必用头文件 ,两种方法
using namespace std;
int hanshu(int x)
{
int y=0;
if(x<0){
y=x+3;
}else if(x>=0 && x<3){
y=-2*x*x+5;
}else{
y=6-x/3;
}
return y;
}
int main()
{
int x,y;
cin>>x;
y=hanshu(x);
cout<<y;
return 0;
}
到底C还是C++?