第一次作业

好久不写代码了,6月27日,从基础题开始写,锻炼自己写代码的速度。

1.输入2个整数,求两数的平方和并输出。

1
2
3
4
5
6
7
8
9
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<< a*a <<" "<< b*b <<endl;
return 0;
}

2.输入一个圆半径(r)当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#define PI 3.14
using namespace std;
int main(){
int r;
cin>>r;
if(r>0){
cout<<PI*r*r<<endl;
cout<<2*PI*r<<endl;
}else{
cout<<"r必须大于等于0"<<endl;
}
return 0;
}

3.函数y=f(x)可表示为:
2x+1 (x<0)
y= 0 (x=0)
2x-1 (x>0)
编程实现输入一个x值,输出值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int main(){
int x;
cin>>x;
if(x<0){
cout<<2*x+1<<endl;
}else if(x=0){
cout<<'0'<<endl;
}else if(x>0){
cout<<2*x-1;
}
return 0;
}

4、编写一个程序,从4个整数中找出最小的数,并显示此数。

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,e,f,g;
cin>>a>>b>>c>>d;
e=min(a,b);
f=min(e,c);
g=min(d,f);
cout<<g<<endl;
return 0;
}

5.有一函数当x<0时y=1,当x>0时,y=3,当x=0时y=5,编程,从键盘输入一个x值,输出y值。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;
int main(){
int x;
cin>>x;
if(x<0){
cout<<1;
}else if(x==0){
cout<<5;
}else{
cout<<3;
}
}

6.判断闰年

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main(){
int year;
cin>>year;
if(year%4==0&&year%100!=0||year%400==0){
cout<<year<<"是闰年"<<endl;
}else{
cout<<year<<"不是闰年"<<endl;
}
return 0;
}

7.判断输入字符是否为字母

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main(){
char a;
cin>>a;
if(a>=65&&a<=90||a>=97&&a<=122){
cout<<a<<"是字母";
}else{
cout<<a<<"不是字母";
}
}

12.从键盘上输入一个百分制成绩score,按下列原则输出其等级: score>=90等级为A; 80<=score<90, 等级为B;70≤score<80,等级为C; 60<=score<70, 等级为D; score<60, 等级为E。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main(){
int score;
cin>>score;
switch(score/10){
case 10:
case 9:cout<<'A'<<endl;break;
case 8:cout<<'B'<<endl;break;
case 7:cout<<'C'<<endl;break;
case 6:cout<<'D'<<endl;break;
default:
cout<<'E'<<endl;break;
}
}