编程基础06

枚举解题方法

  1. 确定枚举范围(优化)
  2. 通过特定条件筛选目标答案

几个C库函数

C 标准库 – | 菜鸟教程

以下函数需要加头文件math.h 在C++写cmath

  • sqrt()开根号
  • pow()幂函数
  • abs()绝对值
  • ceil()向上取整
  • floor()向下取整
  • fmod()小数取余

作业

623 斐波那契数列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

int main()
{
int n;
cin >> n;
long long a = 1, b = 1, c = 1;
for (int i = 3; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
cout << c << endl;
return 0;
}

1423 统计满足条件的4位数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

// 模块化编程
bool check(int x)
{
int a = x / 1000;
int b = x / 100 % 10;
int c = x / 10 % 10;
int d = x % 10;
if (d - c - b - a > 0) return true;
return false;
}

int main()
{
int n;
cin >> n;
int x, cnt = 0;
for (int i = 1; i <= n; i++)
{
cin >> x;
if (check(x)) cnt++;
}

cout << cnt << endl;
return 0;
}

1428 求小数的某一位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int main()
{
int a, b, n;
cin >> a >> b >> n;
for (int i = 1; i <= n; i++)
{
a %= b; // double不能直接使用%进行模运算,可以使用fmod进行模运算
a *= 10;
}
cout << a / b << endl;
return 0;
}

课堂练习

626 水仙花数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using namespace std;

// 枚举法
// 1.确定枚举范围(优化)
// 2.通过特定条件筛选目标答案

int main()
{
// 1.确定枚举范围
for (int i = 100; i <= 999; i++)
{
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;

// 2.通过特定条件筛选目标答案
if (a * a * a + b * b * b + c * c * c == i)
{
cout << i << endl;
}
}
return 0;
}

LG2001 小学数学N合一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<math.h>
//sqrt()开根号 pow()幂函数 abs()绝对值 ceil()向上取整 floor()向下取整
int main() {

int T;
scanf("%d", &T);
if (T == 1) {
// 粘贴问题 1 的主函数代码,除了 return 0
printf("I love Pointer!");
}
else if (T == 2) {
// 粘贴问题 2 的主函数代码,除了 return 0
printf("%d %d", 6, 4);
}
else if (T == 3) {
// 请自行完成问题 3 的代码
printf("%d\n%d\n%d", 14 / 4, 14 / 4 * 4, 14 % 4);
}
else if (T == 4) {
// 请自行完成问题 4 的代码
printf("%.3lf", 500.0 / 3);
}
else if (T == 5) {
// 请自行完成问题 5 的代码
printf("%d", (260 + 220) / (12 + 20));
}
else if (T == 6) {
// 请自行完成问题 6 的代码
double l = sqrt(6 * 6 + 9 * 9);//开根号
printf("%.4lf", l);
}
else if (T == 7) {
// 请自行完成问题 7 的代码
printf("%d\n%d\n%d", 110, 90, 0);
}
else if (T == 8) {
// 请自行完成问题 8 的代码
double r = 5, pi = 3.141593;
printf("%.4lf\n%.4lf\n%.3lf", 2 * pi * r, pi * r * r, 4.0 / 3 * pi * r * r * r);
}
else if (T == 9) {
// 请自行完成问题 9 的代码
printf("%d", (((1 + 1) * 2 + 1) * 2 + 1) * 2);
}
else if (T == 10) {
// 请自行完成问题 10 的代码
printf("%d", 9);
}
else if (T == 11) {
// 请自行完成问题 11 的代码
printf("%.4lf", 100.0 / (8 - 5));
}
else if (T == 12) {
// 请自行完成问题 12 的代码
//ASCII
//'A' - 65 'B' - 66 ... 'Z' - 90
//'a' - 97 'b' - 98 ... 'z' - 122
//'0' - 48 '1' - 49 ... '9' - 57
//int-%d double-%lf char-%c
printf("%d\n%c", 'M' - 'A' + 1, 'A' + 18 - 1);
}
else if (T == 13) {
// 请自行完成问题 13 的代码
double r1 = 4, r2 = 10, pi = 3.141593;
double v1 = 4.0 / 3 * pi * r1 * r1 * r1, v2 = 4.0 / 3 * pi * r2 * r2 * r2;
double v = v1 + v2;
double l = pow(v, 1.0 / 3);//pow(a,b)求a的b次方
printf("%d", (int)l);//强制类型转换
}
else if (T == 14) {
// 请自行完成问题 14 的代码
printf("%d", 50);
}

return 0;
}

输入某年某月某日,判断这一天是这一年的第几天?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
using namespace std;
int main()
{
int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31 };

// 使用:month[i](取第i个元素的值) month[0] month[1]...
int year, month, day;
cin >> year >> month >> day;
int ans = 0;
for (int i = 1; i < month; i++)
{
ans += months[i];
}
if (month > 2)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
ans += 1;
}
}

ans += day;
cout << ans << endl;
return 0;
}

编程基础06
http://yjmanman.github.io/2024/09/21/06/
作者
YuJia
发布于
2024年9月21日
许可协议