【基础题】【分支语句】

1.输入一个字母,如果是大写,输出其小写格式,反之,如果是小写,输出其大写格式。

#include <iostream>
using namespace std;
int main(){
	char c;
	cin>>c;
	if(c >= 'a' && c <= 'z')
		c -= 32;
	else if(c >= 'A' && c <= 'Z')
		c += 32;
	cout<<c;
    return 0;
}

2.输出两个数中最大的数

#include<iostream>
using namespace std;
int main(){
	int a,b;
	cin>>a>>b;
	if(a > b)
		cout<<"max = "<<a;
	else if(b > a)
		cout<<"max = "<<b; 
	else
		cout<<"两数相等。";
return 0;	
} 

3.输出三个数中最大的数。

#include <iostream>
using namespace std;
int main(){
	int a,b,c,max;
	cin>>a>>b>>c;
	if(a > b){
		if(a > c)
			max = a;
		else
			max = c;
	}
	else{
		if(b > c)
			max = b;
		else
			max = c;
	}
	cout<<"max = "<<max;
    return 0;
}

使用条件表达式,更简明、清晰。

#include <iostream>
using namespace std;
int main(){
	int a,b,c,max,temp;
	cin>>a>>b>>c;
	temp = a > b ?a :b;
	max = temp > c ?temp :c;
	cout<<"max = "<<max;
    return 0;
}

4.输入一个年份,判断其是否为闰年。

#include <iostream>
using namespace std;
int main(){
	int year;
	cin>>year;
	if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		cout<<year<<"年是闰年。"; 
	else
		cout<<year<<"年不是闰年。";
    return 0;
}

5.分段函数,如果x < 1,y = x; 如果1<= x < 10,y = 2x - 1;如果x >= 10,y = 3x - 1 .

#include <iostream>
using namespace std;
int main(){
	float x,y;
	cin>>x;
	if(x < 1)
		y = x;
	else if(x >= 1 && x < 10)
		y = 2*x - 1;
	else
		y = 3*x - 11;
	cout<<"x = "<<x<<",y = "<<y;
    return 0;
}

6.给出一个百分制成绩,要求输出成绩等级’A’,‘B’,‘C’,‘D’,‘E’。90分以上为’A’,80~89分为’B’, 70-79分为’C’, 60-69分为’D’, 60以下为’E’。

#include <iostream>
using namespace std;
int main(){
	float score;
	cin>>score;
	char grade;
	while(score > 100 || score < 0){
		cout<<"data error,enter data again.";
		cin>>score;
	} 
	switch((int)score / 10){
		case 10:
		case 9:
			grade = 'A';
			break;
		case 8:
			grade = 'B';
			break;
		case 7:
			grade = 'C';
			break;
		case 6:
			grade = 'D';
			break;
		default:
			grade = 'E';
	}
	cout<<"score is "<<score<<",grade is "<<grade;
    return 0;
}

7.根据给出的年份、月份、输出该月份所含的天数。

#include <iostream>
using namespace std;
int main(){
	int year,month,days;
	cin>>year>>month;
	switch(month){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 11:
			days = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 12:
			days = 30;
			break;
		case 2:
			if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
				days = 29;
			
			else 
				days = 28;
			break; 
	}
	cout<<year<<"年"<<month<<"月"<<"有"<<days<<"天。"; 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值