Log
题库目录
2000–2099
2000(ASCII码排序)
- 题解
- 反思
- 误区
- 新知
2001(计算两点间的距离)
- 题解
- 反思
- 新知
2002(计算球体积)
- 题解
- 反思
- 新知
2003(求绝对值)
- 题解
2004(成绩转换)
- 题解
2005(第几天?)
- 题解
- 反思
- 新知
2006(求奇数的乘积)
- 题解
- 反思
题解+感悟
2000(ASCII码排序)
题解
// 2000_ASCII_code_sorting.cpp
// 2024-11-19 第十次提交
#include <stdio.h>
void sorting(char *a, char *b);
int main()
{
char a, b, c;
while (scanf("%c%c%c", &a, &b, &c) != EOF)
{
getchar();
sorting(&a, &b);
sorting(&a, &c);
sorting(&b, &c);
printf("%c %c %c\n", a, b, c);
}
return 0;
}
void sorting(char *a, char *b)
{
char t;
if (*a > *b)
{
t = *a;
*a = *b;
*b = t;
}
}
反思
第一次在编程练习中使用函数模块化解答习题,虽然出现了混淆全局变量和局部变量的问题,但在一定程度上实践了C语言模块化解决问题的思想,十分有意义。
误区
局部变量和全局变量的混淆:
在前几次提交中,我没有利用指针对a和b的地址进行操作,直接交换。发现没有效果,后来发现没有返回值,与Github Copilot对话后得知交换的只是局部变量,对全局变量没有影响,应采用指针对地址进行操作或利用结构体实现两个交换的值的返回。
新知
HDU网站的多行流输入处理:
1.while (scanf("%c%c%c", &a, &b, &c) != EOF):
要点:
EOF是文件结束符,通常用于指示输入流的结束。在 C 语言中,scanf
函数在读取到文件末尾时会返回。EOF常用于循环读取文件或输入流,直到没有更多数据可读为止。
语句解释:
等待读取三个输入值,分别赋值给三个变量,若读取的值不到三个且读取到了EOF字符时结束。
(实际仍不清楚内部判断原理(悲),涉及编译吗?有佬看到希望评论一下)
2.getchar():
解释:
仍是对于HDU平台判题的需求,与编译器有关,需要利用getchar()
函数读取"\n"
换行符并丢弃。
Github Copilot:
网站:
输入数据是一次性提供的。用户在提交代码时,平台会将所有输入数据作为标准输入传递给程序。程序可以一次性读取所有输入数据;
输入数据通常在一次性提供完毕后结束,程序可以通过读取到 EOF(文件结束符)来判断输入结束。
本地终端:
输入数据通常是逐行输入的。用户需要手动输入每一行数据,程序会逐行读取输入;
用户可以通过特定的操作(如按下 Ctrl+D 或 Ctrl+Z)来手动结束输入,程序同样可以通过读取到 EOF 来判断输入结束。
补充:
利用重定向输入文件的方式在本地模拟网站上的输入方式:编写.txt文件确定好输入格式,利用终端输入,例如:./your_program < input.txt
。单个小于号 < 来重定向输入文件,这个符号表示将文件 input.txt 的内容作为标准输入传递给程序 your_program。
2001(计算两点间的距离)
题解
// 2001_calculate_the_distance_between_two_points.cpp
// 2024-11-20 第二次提交
#include <stdio.h>
#include <math.h>
float calculate(float a, float b, float c, float d);
int main(int argc, char *argv[])
{
float a, b, c, d;
float distance;
while (scanf("%f %f %f %f", &a, &b, &c, &d) != EOF)
{
getchar();
distance = calculate(a, b, c, d);
printf("%.2f\n", distance);
}
return 0;
}
float calculate(float a, float b, float c, float d)
{
return sqrt(pow(c - a, 2) + pow(d - b, 2));
}
反思
对于main主函数的传参还是不熟悉。
新知
<math.h>
的学习:
Github copilot:
math.h 是 C 标准库中的一个头文件,提供了一些常用的数学函数。以下是 math.h 中一些主要的函数(要有思考的记)
基本数学函数:
double sqrt(double x);:计算平方根。
double pow(double base, double exponent);:计算 base 的 exponent 次幂。
double exp(double x);:计算 e 的 x 次幂。
double log(double x);:计算自然对数(以 e 为底)。
double log10(double x);:计算常用对数(以 10 为底)。
三角函数:
double sin(double x);:计算正弦值。
double cos(double x);:计算余弦值。
double tan(double x);:计算正切值。
double asin(double x);:计算反正弦值。
double acos(double x);:计算反余弦值。
double atan(double x);:计算反正切值。
double atan2(double y, double x);:计算 y/x 的反正切值。
双曲函数:
double sinh(double x);:计算双曲正弦值。
double cosh(double x);:计算双曲余弦值。
double tanh(double x);:计算双曲正切值。
幂和指数函数
double exp2(double x);:计算 2 的 x 次幂。
double expm1(double x);:计算 e^x - 1。
double log1p(double x);:计算 log(1 + x)。
double log2(double x);:计算以 2 为底的对数。
取整和取余函数:
double ceil(double x);:向上取整。
double floor(double x);:向下取整。
double fmod(double x, double y);:计算 x 除以 y 的余数。
double trunc(double x);:截断小数部分。
double round(double x);:四舍五入。
其他函数:
double fabs(double x);:计算绝对值。
double fmin(double x, double y);:返回较小值。
double fmax(double x, double y);:返回较大值。
double hypot(double x, double y);:计算 sqrt(xx + yy),即欧几里得距离。
2002(计算球体积)
题解
// 2002_calculate_the_ball_volume.cpp
// 2024-11-24 第四次提交
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
#define PI 3.1415927 // 无分号
// const float PI = 3.1415927; 注意有分号
int main(int argc, char *argv[])
{
double r, V;
while (cin >> r)
{
V = PI * pow(r, 3) / 3 * 4;
printf("%.3f\n", V);
}
return 0;
}
反思
注意数值范围不能越界(蓝桥杯中也很重要)
新知
全局变量声明的两种方式:见上
2003(求绝对值)
题解
// 2003_find_the_absolute_value.cpp
// 2024-11-24 第一次提交
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int main(int argc, char *argv[])
{
double a;
while(cin>>a)
{
printf("%.2f\n", fabs(a));
}
return 0;
}
2004(成绩转换)
题解
// 2004_grade_conversion.cpp
// 2024-11-24 第一次提交
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int t;
while(cin>>t)
{
if(t<0||t>100)
{
cout << "Score is error!" << endl;
}
else
{
if(t>=90)
{
cout << 'A' << endl;
}
else
{
if(t>=80)
{
cout << 'B' << endl;
}
else
{
if(t>=70)
{
cout << 'C' << endl;
}
else
{
if(t>=60)
{
cout << 'D' << endl;
}
else
{
cout << 'E' << endl;
}
}
}
}
}
}
return 0;
}
2005(第几天?)
题解
// 2005_what_day_it_is.cpp
// 2024-11-24 第三次提交
#include <iostream>
using namespace std;
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int is_leap_year(int y);
int main(int argc, char *argv[])
{
int y, m, d, sum;
int i;
char a, b;
while (cin >> y >> a >> m >> b >> d)
{
sum = 0;
if (is_leap_year(y))
{
for (i = 0; i < m - 1; i++)
{
sum = sum + days[i];
}
if (m > 2)
{
sum = sum + 1;
}
sum = sum + d;
cout << sum << endl;
}
else
{
for (i = 0; i < m - 1; i++)
{
sum = sum + days[i];
}
sum = sum + d;
cout << sum << endl;
}
}
return 0;
}
int is_leap_year(int y)
{
if ((y % 4 == 0 && y % 100!=0) || y % 400 == 0)
{
return 1;
}
else
{
return 0;
}
}
反思
在做题过程中有些过度依赖AI进行代码纠错,不利于自己代码能力的提升。
新知
cin
的强大能力:
1.读取不同类型的数据
可以使用 cin 读取不同类型的数据并将其存储到对应变量中,如整数、浮点数、字符串等。如输入:1985/1/20,可以用cin >> y >> a >> m >> b >> d
进行处理。
2.读取整行输入
使用getline
函数读取整行输入,包括空格。如:getline(cin, line)
。
3.忽略输入中的特定字符
使用cin.ignore
忽略输入流中的特定字符。如忽略输入流中的前 10 个字符或直到遇到换行符cin.ignore(10, '\n')
。
4.检查输入是否成功
使用cin.fail()
检查输入操作是否成功。
5.清空输入缓冲区
使用cin.clear
清除输入流的错误状态,并使用cin.ignore
清空输入缓冲区。
6.使用流操纵器
使用流操纵器(如setw
、setprecision
等)格式化输入输出。如:#include <iomanip>int x;cin >> setw(5) >> x; // 设置输入宽度为 5
7.清空输入缓冲区
使用cin.clear
清除输入流的错误状态,并使用cin.ignore
清空输入缓冲区。cin.clear();cin.ignore(numeric_limits<streamsize>::max(), '\n');
这里的numeric_limits<streamsize>::max()
表示忽略所有遇到的换行符。
2006(求奇数的乘积)
题解
// 2006_find_the_product_of_odd_numbers.cpp
// 2024-11-24 第二次提交
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int s[100], d, i;
while (cin >> d)
{
int sum = 1;
for (i = 0; i < d; i++)
{
cin >> s[i];
}
for (i = 0; i < d; i++)
{
if (s[i] % 2 != 0)
{
sum = sum * s[i];
}
}
cout << sum << endl;
}
return 0;
}
反思
对于逐行输入要注意特定值的更新。
2007(平方和与立方和)
题解
// 2007_sum_of_squares_and_sum_of_cubes.cpp
// 2024-12-11 第二次提交
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, temp, i;
int sum1=0, sum2=0;
while (cin >> a >> b)
{
if (a > b)
{
temp = a;
a = b;
b = temp;
}
sum1=0;
sum2=0;
for (i = a; i <= b;i++)
{
if(i%2==0)
{
sum2 = sum2 + pow(i, 2);
}
else
{
sum1 = sum1 + pow(i, 3);
}
}
cout << sum2 << " " << sum1 << endl;
}
return 0;
}
其他
杭电OJ平台提交后403
若代码中出现id一词会触发此情况,原因可能是防止SQL注入。
解决方法:添加注释(任意)
Github账户及主页链接
账号:NeoA
链接:https://github.com/NeoA666
希望以后我的contribution map能绿一些