在C语言的学习过程中,编程题是检验学生掌握程度和逻辑思维能力的重要方式。本文将基于上一套博客的内容,继续分享10道经典的C语言编程题,难度适中,涵盖了C语言的核心知识点,适合用于综合测试学生的编程能力。
一、编程题(每题10分,共10题)
1. 输出杨辉三角形
题目描述: 编写程序,输入一个整数n,输出n行杨辉三角形。
示例输入: 输入:5 示例输出:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
代码实现:
#include <stdio.h>
int main() {
int n, coef = 1;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 1; j <= n - i; j++) printf(" ");
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0) coef = 1;
else coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
2. 判断回文字符串
题目描述: 编写程序,输入一个字符串,判断其是否为回文字符串。
示例输入: 输入:"racecar" 示例输出: racecar is a palindrome.
代码实现:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
int len = strlen(str), isPalindrome = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) {
isPalindrome = 0;
break;
}
}
if (isPalindrome) printf("%s is a palindrome.\n", str);
else printf("%s is not a palindrome.\n", str);
return 0;
}
3. 统计元音字母数量
题目描述: 编写程序,输入一个字符串,统计其中元音字母(a, e, i, o, u)的数量。
示例输入: 输入:"Hello World" 示例输出: Number of vowels: 3
代码实现:
#include <stdio.h>
int main() {
char str[100];
int count = 0;
printf("Enter a string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
count++;
}
printf("Number of vowels: %d\n", count);
return 0;
}
4. 输出整数的所有因子
题目描述: 编写程序,输入一个整数n,输出其所有因子。
示例输入: 输入:12 示例输出: Factors of 12: 1 2 3 4 6 12
代码实现:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factors of %d: ", n);
for (int i = 1; i <= n; i++) {
if (n % i == 0) printf("%d ", i);
}
return 0;
}
5. 字符串转换为大写
题目描述: 编写程序,输入一个字符串,将其转换为大写并输出。
示例输入: 输入:"hello" 示例输出: Uppercase string: HELLO
代码实现:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++) str[i] = toupper(str[i]);
printf("Uppercase string: %s\n", str);
return 0;
}
6. 统计二进制表示中1的个数
题目描述: 编写程序,输入一个整数n,输出n的二进制表示中1的个数。
示例输入: 输入:13 示例输出: Number of 1s: 3
代码实现:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n) {
count += n & 1;
n >>= 1;
}
printf("Number of 1s: %d\n", count);
return 0;
}
7. 删除字符串中的空格
题目描述: 编写程序,输入一个字符串,删除其中的所有空格并输出。
示例输入: 输入:"Hello World" 示例输出: String without spaces: HelloWorld
代码实现:
#include <stdio.h>
int main() {
char str[100], result[100];
int j = 0;
printf("Enter a string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] != ' ') result[j++] = str[i];
}
result[j] = '\0';
printf("String without spaces: %s\n", result);
return 0;
}
8. 判断完全数
题目描述: 编写程序,输入一个整数n,判断其是否为完全数(完全数是指等于其所有因子之和的数,例如6 = 1 + 2 + 3)。
示例输入: 输入:28 示例输出: 28 is a perfect number.
代码实现:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i < n; i++) {
if (n % i == 0) sum += i;
}
if (sum == n) printf("%d is a perfect number.\n", n);
else printf("%d is not a perfect number.\n", n);
return 0;
}
9. 计算阶乘末尾的0的个数
题目描述: 编写程序,输入一个整数n,输出n的阶乘的末尾有多少个0。
示例输入: 输入:25 示例输出: Number of trailing zeros: 6
代码实现:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n >= 5) {
n /= 5;
count += n;
}
printf("Number of trailing zeros: %d\n", count);
return 0;
}
10. 判断字符串是否为有效数字
题目描述: 编写程序,输入一个字符串,判断其是否为有效的数字(整数或小数)。
示例输入: 输入:"123.45" 示例输出: "123.45 is a valid number."
代码实现:
#include <stdio.h>
#include <ctype.h>
int isNumber(char *str) {
int i = 0, decimalFound = 0;
if (str[i] == '+' || str[i] == '-') i++;
while (str[i] != '\0') {
if (str[i] == '.') {
if (decimalFound) return 0;
decimalFound = 1;
} else if (!isdigit(str[i])) return 0;
i++;
}
return 1;
}
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
if (isNumber(str)) printf("%s is a valid number.\n", str);
else printf("%s is not a valid number.\n", str);
return 0;
}
参考答案
以上所有代码的正确性均已验证,示例输出基于标准测试数据。实际编程时允许代码风格差异,只需逻辑正确即可得分。
总结
这10道题目覆盖了C语言的经典算法和功能实现,包括字符串处理、循环、条件判断、数学运算等核心知识点,适合用于综合测试学生的编程能力和逻辑思维。希望通过这些题目的练习,能够帮助读者更好地掌握C语言的编程技巧。
欢迎在评论区讨论或分享你的解题思路和代码!
推荐阅读:
1、C语言进阶测试
2、C语言基础测试
上一篇:C语言基础经典编程题