C++基础复习之二 表达式

题目:
[quote]
1、根据以下函数关系,对输入的每个x值,求y值。
y = x * (x + 2) 2 < x <= 10
y = 2 * x -1 < x <= 2
y = x - 1 x <= -1

2、编程实现输入一个整数,判断其能否被3、5、7整除兵输出以下信息之一:
①能同时被3,5,7整除
②能被其中两数(要指出哪2个)整除;
③能被其中一个数(要指出那一个)整除;
④不能被3、5、7任何一个整除。


3、编程实现输入一个整数,输出相应的五分制成绩。
设90-100为“A”,80-89为“B”,70-79为“C” 60-69为“D”,0-59为“E”。[/quote]


提示:
[quote]
1、if
2、switch
3、if - else if
[/quote]


参考代码:

#include <iostream>

using namespace std;

int main()
{
int x;
cout << "please input x:";
cin >> x;
cout << endl;

if(x <= -1)
cout << (x - 1) <<endl;
if(x>-1 && x<=2)
cout << (2 * x) << endl;
if(2<x && x<=10)
cout << x * (x + 2) << endl;
if(x > 10)
cout << x << endl;

return 0;
}



#include <iostream>

using namespace std;

int main()
{
int a;
cout <<"please input a number:\n";
cin >>a;

int c1 = a%3 == 0;
int c2 = a%5 == 0;
int c3 = a%7 == 0;

cout << c1 << " " << c2 << " " << c3 << endl;

switch((c1<<2) + (c2<<1) + c3)//相当c1*4 +c2*2 + c3
{
case 0:
cout <<"不能被3,5,7整除.\n";
break;
case 1:
cout <<"只能被7整除.\n";
break;
case 2:
cout <<"只能被5整除.\n";
break;
case 3:
cout <<"可以被5,7整除.\n";
break;
case 4:
cout <<"只能被3整除.\n";
break;
case 5:
cout <<"可以被3,7整除.\n";
break;
case 6:
cout <<"可以被3,5整除.\n";
break;
case 7:
cout <<"可以被3,5,7整除.\n";
break;
}

return 0;
}




#include <iostream>

using namespace std;

int main()
{
int grade;
cout << "please input a number(0-100):\n";
cin >> grade;

if(grade>100||grade<0)
cout << "错误的成绩.\n";
else if(grade>=90)
cout << "A.\n";
else if(grade>=80)
cout << "B.\n";
else if(grade>=70)
cout << "C.\n";
else if(grade>=60)
cout << "D.\n";
else
cout << "E.\n";

return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值