编程常见函数应用和技巧
一、输入输出函数详解
1. scanf 格式符号大全
| 格式符号 | 含义 | 示例 |
|---|---|---|
%d | 整数 | scanf("%d", &n); |
%lld | 长整型 | scanf("%lld", &x); |
%f | 单精度浮点数 | scanf("%f", &f); |
%lf | 双精度浮点数 | scanf("%lf", &d); |
%c | 字符 | scanf("%c", &ch); |
%s | 字符串 | scanf("%s", str); |
%x | 十六进制整数 | scanf("%x", &hex); |
%o | 八进制整数 | scanf("%o", &oct); |
%u | 无符号整数 | scanf("%u", &u); |
2. scanf 高级用法
#include <stdio.h>
int main() {
// 1.读取多个数据
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
// 2.读取特定格式
int year, month, day;
scanf("%d-%d-%d", &year, &month, &day);
// 3.读取字符串(不含空格)
char name[50];
scanf("%s", name);
// 4.读取字符(注意空格问题)
char ch;
scanf(" %c", &ch); // 前面的空格跳过空白字符
// 5.限制读取长度,即安全读取
char str[11];
scanf("%10s", str); // 最多读取10个字符
//提示:scanf() 函数返回值是读取的元素个数,如果输入的字符串长度小于指定长度,则返回值小于指定长度。**
char str[100];
// 6.读取整行(直到遇到换行符)
printf("请输入一行文字: ");
scanf("%[^\n]", str);
printf("你输入的是: %s\n", str);
// 7.读取直到遇到逗号
char str[100];
printf("请输入(以逗号结束): ");
scanf("%[^,]", str);
printf("你输入的是: %s\n", str);
//注意 scanf 函数不能读取 string 类型变量。
return 0;
}
3. printf 格式控制
#include <stdio.h>
int main() {
int n = 123;
double d = 3.14159;
// 控制输出宽度和对齐
printf("%5d\n", n); // " 123"
printf("%-5d\n", n); // "123 "
printf("%05d\n", n); // "00123"
// 控制浮点数精度
printf("%.2f\n", d); // "3.14"
printf("%8.3f\n", d); // " 3.142"
// 科学计数法
printf("%e\n", d); // "3.141590e+00"
return 0;
}
二、常用标准库函数
1. 数学函数 <math.h>
#include <stdio.h>
#include <math.h>
int main() {
int a = -5, b = 8;
double x = 2.5, y = 3.0;
printf("abs(%d) = %d\n", a, abs(a)); // 绝对值
printf("pow(%.1f, %.1f) = %.2f\n", x, y, pow(x, y)); // 幂运算
printf("sqrt(%.1f) = %.2f\n", x, sqrt(x)); // 平方根
printf("ceil(%.1f) = %.1f\n", x, ceil(x)); // 向上取整
printf("floor(%.1f) = %.1f\n", x, floor(x));// 向下取整
printf("round(%.1f) = %.1f\n", x, round(x));// 四舍五入
return 0;
}
2. 字符串函数 <string.h>
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
char str3[20];
// 字符串长度
printf("Length: %lu\n", strlen(str1));
// 字符串复制
strcpy(str3, str1);
printf("Copy: %s\n", str3);
// 字符串连接
strcat(str1, " ");
strcat(str1, str2);
printf("Concatenate: %s\n", str1);
// 字符串比较
printf("Compare: %d\n", strcmp("abc", "abd"));
return 0;
}
3. 字符函数 <ctype.h>
* 本节可以不用记
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
printf("isalpha('%c'): %d\n", ch, isalpha(ch));
printf("isdigit('%c'): %d\n", ch, isdigit(ch));
printf("toupper('%c'): %c\n", 'a', toupper('a'));
printf("tolower('%c'): %c\n", 'A', tolower('A'));
return 0;
}
三、算法竞赛实用技巧
1. 快速读取
// 快速读取整数(适用于大数据量)
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
2. 文件输入输出
#include <stdio.h>
int main() {
// 推荐:::重定向到文件(比赛时根据要求使用),
// 后续的scanf/cin 和 printf/cout 都会从文件中读取和写入数据
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// 或者使用文件指针
FILE *fin = fopen("input.txt", "r");
FILE *fout = fopen("output.txt", "w");
int n;
fscanf(fin, "%d", &n);
fprintf(fout, "%d\n", n);
fclose(fin);
fclose(fout);
return 0;
}
四、常见错误及注意事项
1. 数组越界
int arr[100];
// 错误:访问arr[100]会导致未定义行为
for (int i = 0; i <= 100; i++) { // 应该是 i < 100
arr[i] = i;
}
2. 浮点数比较
double a = 0.1 + 0.2;
double b = 0.3;
// 错误:直接比较浮点数
if (a == b) { ... }
// 正确:使用误差范围
if (fabs(a - b) < 1e-9) { ... }