knrt.net
当前位置:首页 >> C++试题 编程输入x的值,输出y的值 y = x ( x<1) ; y = x+5 (1≤x... >>

C++试题 编程输入x的值,输出y的值 y = x ( x<1) ; y = x+5 (1≤x...

void main(){ float x,y; scanf(%f,&x); if(x<1) printf("y=%.2f\n",x); else if(1<=x<10) printf("y=%.2f\n",x+5); else printf("y=%.2f\n",x-5); }

有一函数: x (-5<x<0) y= x-1 (x=0) x+1 (0<x<10) 编写一程序,要求用scanf函数输入x的值,输出y的值.main() { int x; scanf ("%d",&x); if (x>-5&&x<0) printf ("y=%d",x); else if (x=0) printf ("y=%d",x-1); else if (x>0&&x<10) printf ("y=%d",x+1); else printf ("input eeror"); }

第一个else改为else if(x<3)else本身就是对应if条件不成立的分支了,不能再加条件

#includeusing namespace std;int main(){ int x,y; cin>>x; y=x; cout<<y<<endl; return 0;}

#include<stdio.h> int main() { int x,y; scanf("%d",&x); switch(x) { case x<0&&x>-5 : y=x; case x==0 : y=x-1; case x<10&&x>0 : y=x+1; } printf("%d",y); return 0; }

int x = 0;cout<<"请输入一个数:";cin>>x;if(x <= 3){ y = x;}else if(x <= 5){ y = x*2;}else{y = x * 3;}cout << endl;cout << "Y =" << y << endl;

else 多加了分号#include<stdio.h> void main() { int x,y; scanf("x=:%d",&x); if(x<1) y=x; else if(1<=x<10) y=2x-1; else y=3x-11; printf("x=%d,y=%d\n",x,y); getch();}

#include<stdio.h> int main(){ float x,y; printf("please input x\n"); scanf("%f",&x); if (x<0) y=1.0; else if (x>1) y=3.0; else y = 2.0; printf("x=%g y=%g\n",x,y); return 0; }

可以用if语句来实现:#include<stdio.h> main() { double x,y;scanf("%lf",&x);if(x<0)y=3*x+2;else y=-x+1;printf("%lf\n",y);}

#include "iostream.h"void func(int &a,int b);int main(){ int x,y; cout>x>>y; func(x,y); return 0;}void func(int &a,int b){ b=a; if(b>10&&b=100)cout=100" 评论0 0 0

相关文档
网站首页 | 网站地图
All rights reserved Powered by www.knrt.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com