笔记——week1_c++

week1_C++

知识点:

浮点数精度、域宽、填充

#include<iomanip>
更多操作操作符功能
setprecision(int n)设置以n表示数值精度
setw(int n)设置以n表示的域宽
setfill(char c)设置以c表示的填充字符

水仙花数:

一个三位数,它的各位数的3次幂之和等于它本身。

平年和闰年:

润年:非整百年:能被4整除 整百年: 能被400整除

平年:其他年

季节归属:

3~5
6~8
9~11
12~2

练习 题

1.输入圆的半径r,输出其周长和面积。

#include<iostream>
using namespace std;
int main() {
	const double pi = 3.14159;
	double r, c, s;
	cout << "输入圆的半径:";
	cin >> r;
	c = 2.0 * pi * r;
	s = pi * r * r;
	cout << "圆的周长:" << c << endl;
	cout << "圆的面积:" << s << endl;
	return 0;
}
输入圆的半径:2
圆的周长:12.5664
圆的面积:12.5664

2.将2.0开平方后设置不同的精度和宽度输出。

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
	double d = sqrt(2.0);
	cout << "精度设置:" << endl;
	for (int i = 0; i < 5; i++) {
		cout << setprecision(i) << d << endl;//设置不同的精度
	}
	cout << "当前的精度为:" << cout.precision() << endl;
	cout << "当前域宽:" << cout.width() << endl;
	cout << setw(6) << d << endl;//默认右对齐
	cout << "当前填充字符:" << endl;
	cout << setfill('*') << setw(10) << d << endl;//通过setfill()函数可以直接插入流

	return 0;
}
精度设置:
1
1
1.4
1.41
1.414
当前的精度为:4
当前域宽:0
 1.414
当前填充字符:
*****1.414

3.输入一个三位数,输出其个位、十位、百位上的数字。

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
	int n;
	int ge, shi, bai;
	cin >> n;
	ge = n % 10;
	shi = (n / 10) % 10;
	bai = (n / 100) % 10;
	cout << ge << setw(2) << shi << setw(2) << bai << endl;

	return 0;
}
123
3 2 1

4.输入三个整数,分别输出其增加1、扩大10倍、缩小10倍的结果。

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
	int n, a, b, c;
	cin >> n >> b >> c;
	a = ++n;
	b *= 10;
	c /= 10;
	cout << a << setw(3) << b << setw(3) << c;
	return 0;
}
1 2 90
2 20  9

5.判断学生成绩是否及格

#include<iostream>
using namespace std;
int main() {
	float score;
	cin >> score;
	if (score >= 60)
		cout << "及格!" << endl;
	else
		cout << "不及格!" << endl;
	return 0;
}
21
不及格!

6.判断学生成绩分段式。

#include<iostream>
using namespace std;
int main() {
	float score;
	cin >> score;
	if (score >= 70)
		if (score < 80)
			cout << "中等!" << endl;
		else if (score < 90)
			cout << "良好!" << endl;
		else
			cout << "优秀!" << endl;
	else if (score < 60)
		cout << "不及格!" << endl;
	else
		cout << "及格!" << endl;
	return 0;
}
85
良好!

7.闰年和平年的判断

#include<iostream>
using namespace std;
int main() {
	int year;
	cin >> year;
	// 非整百年:能被4整除
	// 整百年: 能被400整除
	if ((year % 100 != 0 && year % 4 == 0) || year % 400 == 0)
		cout << "闰年!" << endl;
	else
		cout << "平年!" << endl;
	return 0;
}
1500
平年!

8.水仙花数的判断。

#include<iostream>
using namespace std;
int main() {
	int n, ge, shi, bai;
	cin >> n;
	ge = n % 10;
	shi = (n / 10) % 10;
	bai = (n / 100) % 10;
	if (n == ge * ge * ge + shi * shi * shi + bai * bai * bai)
		cout << n << "是水仙花数!" << endl;
	else
		cout << n << "不是水仙花数!" << endl;
	return 0;
}
153
153是水仙花数!

9.判断月份输入什么季节。

#include<iostream>
using namespace std;
int main() {
	int mouth, season = 0;
	cin >> mouth;
	if (3 <= mouth && mouth <= 5)
		season = 1;
	else if (6 <= mouth && mouth <= 8)
		season = 2;
	else if (9 <= mouth && mouth <= 11)
		season = 3;
	else if ((1 <= mouth && mouth <= 2) || (mouth == 12))
		season = 4;
	switch (season) {
	case 1:
		cout << "春季!" << endl;
		break;
	case 2:
		cout << "夏季!" << endl;
		break;
	case 3:
		cout << "秋季!" << endl;
		break;
	case 4:
		cout << "冬季!" << endl;
		break;
	default:
		cout << "输入有误!" << endl;
	}
	return 0;
}
13
输入有误!

10.输入一个整数n,输出1~n的所有整数。

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cout << i;
	return 0;
}
10
12345678910

11.输入n,输出1~n的所有整数,遇到5时停止。

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		if (i == 5)
			break;
		cout << i << endl;
	}
	cout << "This is a break test.";
	return 0;
}
12
1
2
3
4
This is a break test.

12.输入一个整数,输出n行1~n的整数(输出1~n的整数时遇到5时停止)。

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cout << "i = " << i << endl;
		for (int j = 1; j <= i; j++) {
			if (j == 5)
				break;
			cout << j << " ";
		}
		cout << "\n";
	}
	cout << "This is a break test.";
	return 0;
}
6
i = 1
1
i = 2
1 2
i = 3
1 2 3
i = 4
1 2 3 4
i = 5
1 2 3 4
i = 6
1 2 3 4
This is a break test.

12.输入一个整数n,输出1~n的所有整数,遇到整数时不输出。

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		if (i % 2 == 0)
			continue;
		cout << i << " ";
	}
	cout << "This is a continue test.";
	return 0;
}
9
1 3 5 7 9 This is a continue test.

12.输入一个整数(0<n<10),输出n!.

#include<iostream>
using namespace std;
int main() {
	int n, fac = 1;
	cin >> n;
	for (int i = 1; i <= n; i++)
		fac *= i;
	cout << "fac(" << n << ") = " << fac;
	return 0;
}
4
fac(4) = 24

13.输出斐波那契数列第100项 ( F ( 1 ) = F ( 2 ) = 1 : F ( n ) = F ( n − 1 ) + F ( n − 2 ) ) (F(1)=F(2)=1:F(n)=F(n-1)+F(n-2)) (F(1)=F(2)=1:F(n)=F(n1)+F(n2))

#include<iostream>
using namespace std;
long long f[100 + 5];
int main() {
	f[1] = 1, f[2] = 1;
	for (int i = 3; i <= 100; i++) {
		f[i] = f[i - 1] + f[i - 2];
	}
	cout << "fac(100) = " << f[100];
	return 0;
}
fac(100) = 3736710778780434371

13.输入一个整数,输出1~n的所有整数(while)。

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	int i = 1;
	while (i <= n) {
		cout << i << endl;
		i++;
	}
	return 0;
}
5
1
2
3
4
5

13.输入一个整数,输出1~n的所有整数(do while)。

#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	int i = 1;
	do {
		cout << i << endl;
		i++;
	} while (i <= n);
	return 0;
}
5
1
2
3
4
5

14.输入一个整数n,若n为奇数, n = 3 n + 1 n = 3n + 1 n=3n+1,否则, n = n / 2 n = n/2 n=n/2,输出变换次数。

#include<iostream>
using namespace std;
int main() {
	int n, count = 0;
	cin >> n;
	while (n > 1) {
		if (n % 2 == 1)
			n = 3 * n + 1;
		else
			n /= 2;
		cout << n << endl;
		count++;
	}
	cout << "count = " << count;
	return 0;
}
12
6
3
10
5
16
8
4
2
1
count = 9

nt n;
cin >> n;
int i = 1;
do {
cout << i << endl;
i++;
} while (i <= n);
return 0;
}


```css
5
1
2
3
4
5

14.输入一个整数n,若n为奇数, n = 3 n + 1 n = 3n + 1 n=3n+1,否则, n = n / 2 n = n/2 n=n/2,输出变换次数。

#include<iostream>
using namespace std;
int main() {
	int n, count = 0;
	cin >> n;
	while (n > 1) {
		if (n % 2 == 1)
			n = 3 * n + 1;
		else
			n /= 2;
		cout << n << endl;
		count++;
	}
	cout << "count = " << count;
	return 0;
}
12
6
3
10
5
16
8
4
2
1
count = 9
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值