- 提醒:这不是老师给的答案,是我自己弄得,不一定全对(严谨,严谨,严谨)o.0
- 浏览器 ctrl+f 即可搜索关键词寻找答案
- 不对的有问题的联系我,我改
求1+2+3+。。。+200的 (while语句)
#include <stdio.h>
int main() {
int sum = 0;
int i = 1;
while (i <= 200) {
sum += i;
i++;
}
printf("1+2+3+...+200 = %d\n", sum);
return 0;
}
求1+2+3+。。。+200的 (do while语句)
#include <stdio.h>
int main() {
int sum = 0;
int i = 1;
do {
sum += i;
i++;
} while (i <= 200);
printf("1+2+3+...+200 = %d\n", sum);
return 0;
}
求1+2+3+。。。+200的 (for语句)
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 200; i++) {
sum += i;
}
printf("1+2+3+...+200 = %d\n", sum);
return 0;
}
求1+3+5+。。。+99的和 (while语句)
#include <stdio.h>
int main() {
int sum = 0;
int i = 1;
while (i <= 99) {
sum += i;
i += 2;
}
printf("1+3+5+...+99 = %d\n", sum);
return 0;
}
求1+3+5+。。。+99的和 (for语句)
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 99; i += 2) {
sum += i;
}
printf("1+3+5+...+99 = %d\n", sum);
return 0;
}
求2+4+6+。。。+100的和 (while语句)
int main() {
int sum = 0;
int i = 2;
while (i <= 100) {
sum += i;
i += 2;
}
printf("2+4+6+...+100 = %d\n", sum);
return 0;
}
求2+4+6+。。。+100的和 (do while语句)
#include <stdio.h>
int main() {
int sum = 0;
int i = 2;
do {
sum += i;
i += 2;
} while (i <= 100);
printf("2+4+6+...+100 = %d\n", sum);
return 0;
}
求2+4+6+。。。+100的和 (for语句)
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
printf("2+4+6+...+100 = %d\n", sum);
return 0;
}
求10的阶乘(while语句)
#include <stdio.h>
int main() {
int n = 10;
int factorial = 1;
while (n > 0) {
factorial *= n;
n--;
}
printf("10的阶乘 = %d\n", factorial);
return 0;
}
求10的阶乘 (for语句)
#include <stdio.h>
int main() {
int n = 10;
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
printf("10的阶乘 = %d\n", factorial);
return 0;
}
求10的阶乘(递归法)
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n = 10;
int result = factorial(n);
printf("10的阶乘 = %d\n", result);
return 0;
}
输入五个数,按从小到大输出 (冒泡法)
#include <stdio.h>
#define N 5
int main() {
int a[N], i, j, min, t;
printf("请输入5个数字:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N - 1; i++) {
min = i;
for (j = i + 1; j < N; j++) {
if (a[j] < a[min]) {
min = j;
}
}
if (min != i) {
t = a[i];
a[i] = a[min];
a[min] = t;
}
}
printf("从小到大的排序结果为:\n");
for (i = 0; i < N; i++)
printf("%d ", a[i]);
return 0;
}
输出左直角形状的九九乘法口诀
#include <stdio.h>
int main() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
printf("%d * %d = %d\t", j, i, j * i);
}
printf("\n");
}
return 0;
}
输出右直角形状的九九乘法口诀
#include <stdio.h>
int main() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9 - i; j++) {
printf("\t");
}
for (int k = 1; k <= i; k++) {
printf("%d * %d = %d\t", k, i, k * i);
}
printf("\n");
}
return 0;
}
输入四个数,并求最大值
#include <stdio.h>
int main() {
int num1, num2, num3, num4;
int max;
printf("请输入四个数,用空格分隔:");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
max = num1;
if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}
if (num4 > max) {
max = num4;
}
printf("最大值为:%d\n", max);
return 0;
}
输入四个数,并求最小值
#include <stdio.h>
int main() {
int num1, num2, num3, num4;
int min;
printf("请输入四个数,用空格分隔:");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
min = num1;
if (num2 < min) {
min = num2;
}
if (num3 < min) {
min = num3;
}
if (num4 < min) {
min = num4;
}
printf("最小值为:%d\n", min);
return 0;
}
输入三个数,按从大到小输出
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("请输入三个数,用空格分隔:");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 < num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
printf("从大到小输出: %d %d %d\n", num1, num2, num3);
return 0;
}
输入三个数,按从小到大输出
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("请输入三个数,用空格分隔:");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
printf("从小到大输出: %d %d %d\n", num1, num2, num3);
return 0;
}
输入五个数,按从小到大输出(数组名作为函数参数,冒泡法)
#include <stdio.h>
void bubbleSort(int arr[], int size) {
int i, j;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[5];
int i;
printf("请输入五个数,用空格分隔:");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, 5);
printf("从小到大输出:");
for (i = 0; i < 5; i++) {
printf(" %d", arr[i]);
}
printf("\n");
return 0;
}
在主函数中输入五个数,按从小到大输出(调用函数)
#include <stdio.h>
void bubbleSort(int arr[], int size) {
int i, j;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[5];
int i;
printf("请输入五个数,用空格分隔:");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, 5);
printf("从小到大输出:");
for (i = 0; i < 5; i++) {
printf(" %d", arr[i]);
}
printf("\n");
return 0;
}
编辑简单的程序,能分别进行加减乘除运算 (if语句)
#include <stdio.h>
int main() {
double num1, num2, result;
char operator;
printf("请输入两个数,用空格分隔:");
scanf("%lf %lf", &num1, &num2);
printf("请输入运算符(+、-、*、/):");
scanf(" %c", &operator);
if (operator == '+') {
result = num1 + num2;
printf("加法运算结果:%.2lf\n", result);
} else if (operator == '-') {
result = num1 - num2;
printf("减法运算结果:%.2lf\n", result);
} else if (operator == '*') {
result = num1 * num2;
printf("乘法运算结果:%.2lf\n", result);
} else if (operator == '/') {
if (num2 != 0) {
result = num1 / num2;
printf("除法运算结果:%.2lf\n", result);
} else {
printf("错误:除数不能为零!\n");
}
} else {
printf("错误:无效的运算符!\n");
}
return 0;
}
编辑简单的程序,能分别进行加减乘除运算 (switch语句)
#include <stdio.h>
int main() {
double num1, num2, result;
char operator;
printf("请输入两个数,用空格分隔:");
scanf("%lf %lf", &num1, &num2);
printf("请输入运算符(+、-、*、/):");
scanf(" %c", &operator);
switch (operator) {
case '+':
result = num1 + num2;
printf("加法运算结果:%.2lf\n", result);
break;
case '-':
result = num1 - num2;
printf("减法运算结果:%.2lf\n", result);
break;
case '*':
result = num1 * num2;
printf("乘法运算结果:%.2lf\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("除法运算结果:%.2lf\n", result);
} else {
printf("错误:除数不能为零!\n");
}
break;
default:
printf("错误:无效的运算符!\n");
break;
}
return 0;
}
编辑简单的程序,能同时进行加减乘除运算 (嵌套调用)
#include <stdio.h>
double add(double num1, double num2) {
return num1 + num2;
}
double subtract(double num1, double num2) {
return num1 - num2;
}
double multiply(double num1, double num2) {
return num1 * num2;
}
double divide(double num1, double num2) {
if (num2 != 0) {
return num1 / num2;
} else {
printf("错误:除数不能为零!\n");
return 0;
}
}
int main() {
double num1, num2;
printf("请输入两个数,用空格分隔:");
scanf("%lf %lf", &num1, &num2);
printf("加法运算结果:%.2lf\n", add(num1, num2));
printf("减法运算结果:%.2lf\n", subtract(num1, num2));
printf("乘法运算结果:%.2lf\n", multiply(num1, num2));
printf("除法运算结果:%.2lf\n", divide(num1, num2));
return 0;
}
输入10位同学的成绩 (58,76,80,95,70,65,88,92,68、5
0),判断优秀、良好,及格,不及格 (if else)
#include <stdio.h>
int main() {
int scores[10];
int i;
printf("请输入10位同学的成绩:\n");
for (i = 0; i < 10; i++) {
printf("同学%d的成绩:", i + 1);
scanf("%d", &scores[i]);
}
printf("成绩等级:\n");
for (i = 0; i < 10; i++) {
printf("同学%d:", i + 1);
if (scores[i] >= 90) {
printf("优秀\n");
} else if (scores[i] >= 80) {
printf("良好\n");
} else if (scores[i] >= 60) {
printf("及格\n");
} else {
printf("不及格\n");
}
}
return 0;
}
输入10位同学的成绩 (58,76,80,95,70,65,88,92,68、5
0),判断优秀、良好,及格,不及格 (switch)
#include <stdio.h>
int main() {
int scores[10];
int i;
printf("请输入10位同学的成绩:\n");
for (i = 0; i < 10; i++) {
printf("同学%d的成绩:", i + 1);
scanf("%d", &scores[i]);
}
printf("成绩等级:\n");
for (i = 0; i < 10; i++) {
printf("同学%d:", i + 1);
switch (scores[i] / 10) {
case 10:
case 9:
printf("优秀\n");
break;
case 8:
printf("良好\n");
break;
case 6:
case 7:
printf("及格\n");
break;
default:
printf("不及格\n");
break;
}
}
return 0;
}
输入10位同学的成绩 (58,76,80,95,70,65,88,92,68、5
0),按照从低到高的顺序输出
#include <stdio.h>
int main() {
int grades[] = {85, 92, 78, 90, 88, 82, 95, 87, 80, 91};
int n = sizeof(grades) / sizeof(grades[0]);
printf("原始成绩:\n");
for (int i = 0; i < n; i++) {
printf("%d ", grades[i]);
}
printf("\n");
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (grades[j] > grades[j + 1]) {
int temp = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = temp;
}
}
}
printf("排序后的成绩:\n");
for (int i = 0; i < n; i++) {
printf("%d ", grades[i]);
}
printf("\n");
return 0;
}
输入10位同学的成绩 (58,76,80,95,70,65,88,92,68、5
0),按照从高到低的顺序输出
#include <stdio.h>
int main() {
int grades[] = {85, 92, 78, 90, 88, 82, 95, 87, 80, 91};
int n = sizeof(grades) / sizeof(grades[0]);
printf("原始成绩:\n");
for (int i = 0; i < n; i++) {
printf("%d ", grades[i]);
}
printf("\n");
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (grades[j] < grades[j + 1]) {
int temp = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = temp;
}
}
}
printf("从高到低排序后的成绩:\n");
for (int i = 0; i < n; i++) {
printf("%d ", grades[i]);
}
printf("\n");
return 0;
}
输入正方体的棱长,输出其表面积和体积的大小
#include <stdio.h>
int main() {
float edgeLength;
printf("请输入正方体的棱长:");
scanf("%f", &edgeLength);
float surfaceArea = 6 * edgeLength * edgeLength;
float volume = edgeLength * edgeLength * edgeLength;
printf("正方体的表面积为:%.2f\n", surfaceArea);
printf("正方体的体积为:%.2f\n", volume);
return 0;
}
有一个3X4矩阵,要求编程输出该矩阵,并求出其中值最大的那个元31素的值,及其所在的行号和列号
#include <stdio.h>
int main() {
int matrix[3][4] = {
{5, 8, 12, 3},
{9, 4, 6, 1},
{7, 2, 10, 11}
};
int maxElement = matrix[0][0];
int maxRow = 0;
int maxCol = 0;
printf("矩阵:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", matrix[i][j]);
if (matrix[i][j] > maxElement) {
maxElement = matrix[i][j];
maxRow = i;
maxCol = j;
}
}
printf("\n");
}
printf("值最大的元素: %d\n", maxElement);
printf("所在的行号: %d\n", maxRow);
printf("所在的列号: %d\n", maxCol);
return 0;
}
当1+2+3+。。。+i的和sum>=100时,输出sum和i的值 (while)
#include <stdio.h>
int main() {
int i = 1;
int sum = 0;
while (sum < 100) {
sum += i;
i++;
}
printf("满足条件 1+2+3+...+i >= 100 的最小 i 值为:%d\n", i);
printf("对应的和 sum 为:%d\n", sum);
return 0;
}
当1+2+3+。。。+i的和sum>=100时,输出sum和i的值 (do while)
#include <stdio.h>
int main() {
int i = 1;
int sum = 0;
do {
sum += i;
i++;
} while (sum < 100);
printf("满足条件 1+2+3+...+i >= 100 的最小 i 值为:%d\n", i);
printf("对应的和 sum 为:%d\n", sum);
return 0;
}
当1+2+3+。。。+i的和sum>=100时,输出sum和i的值 (for)
#include <stdio.h>
int main() {
int sum = 0;
int i;
for (i = 1; sum < 100; i++) {
sum += i;
}
printf("满足条件 1+2+3+...+i >= 100 的最小 i 值为:%d\n", i);
printf("对应的和 sum 为:%d\n", sum);
return 0;
}
编写函数,计算x的n次方。在主函数中输入x和n的值,调用该函数,并输出结果
#include <stdio.h>
double power(double x, int n) {
double result = 1.0;
if (n >= 0) {
for (int i = 0; i < n; i++) {
result *= x;
}
} else {
for (int i = 0; i < -n; i++) {
result /= x;
}
}
return result;
}
int main() {
double x;
int n;
printf("请输入 x 的值:");
scanf("%lf", &x);
printf("请输入 n 的值:");
scanf("%d", &n);
double result = power(x, n);
printf("%.2lf 的 %d 次方结果为:%.2lf\n", x, n, result);
return 0;
}
利用数组实现: 输入10个整数,找出其中的最大值
#include <stdio.h>
int main() {
int numbers[10];
int i, max;
printf("请输入 10 个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
}
max = numbers[0];
for (i = 1; i < 10; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
printf("最大值为:%d\n", max);
return 0;
}
利用数组实现: 输入一个字符串,然后将其倒序输出
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int length, i;
printf("请输入一个字符串:\n");
scanf("%s", str);
length = strlen(str);
printf("倒序输出字符串:");
for (i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
编写程序,输入一个年份,判断该年是否是闰年(闺年的判断条件年份能被4整除,并且不能被100整除,或能被400整除)
#include <stdio.h>
int main() {
int year;
printf("请输入一个年份:\n");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d 年是闰年。\n", year);
} else {
printf("%d 年不是闰年。\n", year);
}
return 0;
}
输出所有的水仙花数
#include <stdio.h>
#include <math.h>
int main() {
int start, end, number, digit, sum;
printf("请输入要查找的范围(起始和结束数字):\n");
scanf("%d %d", &start, &end);
printf("水仙花数:\n");
for (number = start; number <= end; number++) {
int temp = number;
sum = 0;
while (temp != 0) {
digit = temp % 10;
sum += pow(digit, 3);
temp /= 10;
}
if (sum == number) {
printf("%d\n", number);
}
}
return 0;
}
有如下一组数据:98,12,87、4,65,23,54、33,48,78,利用冒泡排序法将其由小到大排序后输出。
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {98, 12, 87, 4, 65, 23, 54, 33, 48, 78};
int n = sizeof(arr) / sizeof(arr[0]);
printf("初始数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
bubbleSort(arr, n);
printf("\n排序后的数组:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
从键盘上任意输入一个四位整数,分别输出它的个位数、十位数、百位数和千位数
#include <stdio.h>
int main() {
int number;
printf("请输入一个四位整数:\n");
scanf("%d", &number);
int thousands = number / 1000;
int hundreds = (number / 100) % 10;
int tens = (number / 10) % 10;
int units = number % 10;
printf("千位数:%d\n", thousands);
printf("百位数:%d\n", hundreds);
printf("十位数:%d\n", tens);
printf("个位数:%d\n", units);
return 0;
}
Comments NOTHING