求助:1. 有一函数: x (x<1) y= 2x+3 (1=<x<=10) 3x-2 (x>10) 写一程序,输入x,输出y值.
答案:4 悬赏:10
解决时间 2021-03-02 02:06
- 提问者网友:写不出迷人情诗
- 2021-03-01 18:10
多谢啦!!!!!
最佳答案
- 二级知识专家网友:情窦初殇
- 2021-03-01 19:05
//C++ Code
#include
using namespace std;
int main(int args,char* argv[])
{
float x,y;
char ch;
do
{
cout<<"Input x"<<"\t";
cin>>x;
if (x<1)
{
y = x;
}
else if(x >= 1 && x <= 10)
{
y = 2*x+3;
}
else if(x>10)
{
y = 3*x - 2;
}
cout<<"y = "<
cout<<"continue?[y/n]"<
cin>>ch;
}while(ch == 'y');
return 0;
}
如果是C语言的话,把第一行改成#include,注释掉第二行
cout行换成printf cin行换成scanf
#include
using namespace std;
int main(int args,char* argv[])
{
float x,y;
char ch;
do
{
cout<<"Input x"<<"\t";
cin>>x;
if (x<1)
{
y = x;
}
else if(x >= 1 && x <= 10)
{
y = 2*x+3;
}
else if(x>10)
{
y = 3*x - 2;
}
cout<<"y = "<
}while(ch == 'y');
return 0;
}
如果是C语言的话,把第一行改成#include
cout行换成printf cin行换成scanf
全部回答
- 1楼网友:社会水太深
- 2021-03-01 21:40
马上就好
你好像少了一条
x (x<1)
y= 2x+3
(1=10)
这里
- 2楼网友:萌萌哒小可爱
- 2021-03-01 21:20
//自己写的c语言代码,绝对精炼:
#include
int f(int x) {
if(x >= 1 && x <= 10) return 2 * x + 3 ;
if(x > 10) return 3 * x - 2 ;
return x ;
}
int main() {
int x ;
while(scanf("%d" , &x) == 1) {
printf("%d\n" , f(x)) ;
}
return 0 ;
}
- 3楼网友:而你却相形见绌
- 2021-03-01 20:06
#include
int main()
{
float x,y;
printf("input x:\n");
scanf("%f",&x);
if(x<1)
y=x;
else if(x<=10)
y=2*x+3;
else
y=3*x-2;
printf("y=%f\n",y);
return 0;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯