原题链接:[编程入门]分段函数求值
解题思路:
声明两个变量x,y,其中x 获取用户输入,y根据函数获取赋值。
注意事项:
1,函数中“1<=x<10"在if 条件语句中要写成”x>=1&&x<10“。
2,”2x-1“”3x-11“两个式子写成代码要将乘号写出,即"2*x-1"与"3*x-11"。
参考代码:
#include<stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
if(x<1)
{y=x;}
if(x>=1&&x<10)
{y=2*x-1;}
if(x>=10)
{y=3*x-11;}
printf("%d",y);
return 0;
}
0.0分
12 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
#include<stdio.h> int main() { int x,y; scanf("%d",&x); if(x<1) y=x; else if(x>=1&&x<10) y=2*x-1; else y=3*x-11; printf("%d\n",y); return 0; }#include <stdio.h> int fun(int x){ if(x<1) { return x; } if(x<=10 && x>=1) { return 2*x-1; } if(x>=10) { return 3*x-11; } } int main(){ int x; scanf("%d",&x); printf("%d",fun(x)); return 0; }为什么我的程序只有一半对 求大佬帮我看看 #include<stdio.h> int main(void) { int x,y; scanf("%d",&x); if(x<1) { y=x; printf("%d",y); } if(x>=1&&x<10) { y=2*x-1; printf("%d",y); } else { y=3*x-11; printf("%d",y); } return 0; }#include<stdio.h> int main() { int x,y; scanf("%d",&x); if(x<1) {y=x;} if(x>=1&&x<10) {y=2*x-1;} if(x>=10) {y=3*x-11;} printf("%d",y); return 0; }@蒙mz {y=2*x-1; } {y=3*x-11;}#include<stdio.h> int main(){ int x,y; scanf("%d",&x); if(x<1) {y=x;} if(x>=1&&x<10); {2*x-1;} if(x>=10) (3*x-11;) pintf("%d",y); return 0; } 哪错了