1. 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,请编写程序求它在 第10次落地时,共经过多过少米?第10次反弹多高?
#include<stdio.h>
int main() {
float h = 50.0; // 100米落后,在反弹,弹高50米
int i;
float sum = 100.0; // 第一次下坠100米
for (i = 2; i <= 10; i++) {
sum += h*2; // 上去、下来 所以乘以2
h = h / 2.0;
}
printf("第十次反弹高度:%f\n", h);
printf("已经过 %f 米", sum);
printf("\n");
return 0;
}
运行结果:
2. 猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第2天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。 到第10天早上想再吃时,就只剩一个桃子了。求猴子第1天共摘了多少个桃子?
#include<stdio.h>
int main() {
int x = 1;
for (int i = 1; i <= 9; i++) {
x = (x + 1) * 2;
}
printf("第一天共摘了 %d 个桃子", x);
return 0;
}
运行结果:
3.任意输入一个正整数n,求n的所有因子,并按照下面例子的格式输出其因子。如:8的因子有:1 2 4
#include<stdio.h>
int main() {
int n;
printf("任意输入一个正整数");
scanf_s("%d", &n);
printf("%d的因子有:", n);
for (int i = 1; i < n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}
return 0;
}
运行结果:
4. 用二分法求方程2X3 - 4X2 + 3X - 6 = 0在( - 10, 10)之间的根。
#include<stdio.h>
#include<math.h>
int main()
{
double a, b, c, d;
a = 10;
b = -10;
c = (a + b) / 2;
d = 2 * c*c*c - 4 * c*c + 3 * c - 6;
while (fabs(d)>1e-8)
{
if (d<0)
{
b = c;
c = (a + b) / 2;
d = 2 * c*c*c - 4 * c*c + 3 * c - 6;
}
else
{
a = c;
c = (a + b) / 2;
d = 2 * c*c*c - 4 * c*c + 3 * c - 6;
}
}
printf("The answer is %lf\n", c);
return 0;
}
运行结果:
5. 写一个用选择法排序的函数sort,并在主函数中调用sort函数对10个整数排序。
#include<stdio.h>
int main() {
void sort(int array[], int n);
int a[10];
int i;
printf("enter array:\n");
for (i = 0; i < 10; i++) {
scanf_s("%d", &a[i]);
}
sort(a, 10);
printf("The sorted array:\n");
for (i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
void sort(int array[], int n) {
int i, j, k, t;
for (i = 0; i < n - 1; i++) { // i < 9;
k = i;
for (j = i + 1; j < n; j++) // j < 10; j 从 1 开始
if (array[j] < array[k])
k = j;
t = array[k];
array[k] = array[i];
array[i] = t;
}
}
运行结果:
6. 输入两个整数,按先大后小的顺序输出。要求用函数实现,且不能使用全局变量。
#include<stdio.h>
int main(){
void paixu(int x, int y);
int a,b;
printf_s("请输入两个整数:");
scanf_s("%d%d", &a, &b);
paixu(a, b);
return 0;
}
void paixu(int x, int y) {
if (x > y) {
printf("%d,%d", x, y);
}
else if (x == y) {
printf("这两个数相等!");
}
else {
printf("%d,%d", y, x);
}
}
运行结果:
7. 汉诺塔:
#include<stdio.h>
int main() {
void hanoi(int n, char one, char two, char three);
int m;
printf("input the number of deskes:");
scanf_s("%d", &m);
printf("The step to move %d diskes:\n", m);
hanoi(m, 'A', 'B', 'C');
}
void hanoi(int n, char one, char two, char three) {
void move(char x, char y);
if (n == 1) {
move(one, three);
}
else {
hanoi(n - 1, one, three, two);
move(one, three);
hanoi(n - 1, two, one, three);
}
}
void move(char x, char y) {
printf("%c -- > %c\n", x, y);
}
运行结果:
8> 写一个字符串连接函数,连接2个字符串,要求函数返回连接后新字符串的长度,并在主函数中调用它,连接两个字符串,并给出连接后字符串的长度。
#include<stdio.h>
#include<string.h>
int main() {
void link(char x[], char y[]);
char a[20];
char b[20];
printf("请输入a的值:");
gets_s(a);
printf("请输入b的值:");
gets_s(b);
link(a, b);
return 0;
}
void link(char x[], char y[]) {
int c = strlen(x); // 计算数组a的字符串长度
int d = strlen(y); // 计算数组b的字符串长度
int i;
char s[30] = { 0 }; // 对连接后的数组初始化
for (i = 0; i < c; i++) {
s[i] = x[i];
}
for (i = 0; i < d; i++) {
s[c + i] = y[i];
}
puts(s); // 输出连接后的字符串
printf("字符串长度是:%d", strlen(s)); // 计算连接后的字符串长度
printf("\n");
}
运行结果:
9.矩阵乘积:
#include<stdio.h>
int main()
{
int a[2][3] = { 1,2,3,4,5,6 }, b[3][3] = { 1,2,3,4,5,6,7,8,9 }, c[2][3] = { 0 }, i, j, k;
for (k = 0; k<3; k++)
{
for (i = 0; i<2; i++)
{
for (j = 0; j<3; j++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
for (i = 0; i<2; i++)
{
for (j = 0; j<3; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
运行结果:
10> 杨辉三角:
#include<stdio.h>
int main() {
int a[11][20], i, j;
//初始化
for (i = 0; i < 11; i++)
for (j = 0; j < 20; j++)
a[i][j] = 0;
//给数组赋值
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
{
if (j == 0)a[i][j] = 1; //开头的第一个数为1
else
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
//杨辉三角的规律
}
}
//输出
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++) //在这里只打印到i表明只打印多少个数
printf("%d ", a[i][j]);
printf("\n");
}
return 0; }
运行结果:
11> 编写一个函数change用于交换两个整型变量a、b的值:
#include<stdio.h>
int main() {
void change(int *x, int *y);
int *p1, *p2;
int a, b;
printf("请输入a的值:");
scanf_s("%d", &a);
printf("请输入b的值:");
scanf_s("%d", &b);
p1 = &a;
p2 = &b;
change(p1, p2);
return 0;
}
void change(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
printf("a = %d\nb = %d\n", *x, *y);
}
运行结果:
12. 求一维数组的最大值及其下标:
#include<stdio.h>
int main() {
int a[6];
int i;
int t = 0;
int max = 0;
for (i = 1; i < 6; i++) {
scanf_s("%d", &a[i]);
if (a[i] > max) {
max = a[i];
t = i;
}
}
printf("下标是:%d", t);
printf("最大数为:%d", max);
return 0;
}
运行结果:
13> 编写一个函数求阶乘,要求用静态局部变量实现。在主函数中求1到5的阶乘,并输出
#include<stdio.h>
int main()
{
int jc(int a);
int i;
for (i = 1; i<6; i++)
printf("%d!=%d\n", i, jc(i));
return 0;
}
int jc(int i)
{
static int a = 1; // static:静态局部变量的值在函数调用结束后不消失而继续保留原值,即其占用的存储单元不释放
a = a * i;
return a;
}
运行结果:
14. 写一个函数,将一维数组的元素按倒序重新存放,并在主函数中调用f函数对10个整数进行逆序操作
#include<stdio.h>
int main() {
void nx(int x[]);
int a[10];
printf("请输入十个数:\n");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &a[i]);
}
nx(a);
return 0;
}
void nx(int x[]) {
int i;
int temp;
for (i = 0; i < 5; i++) {
temp = x[i];
x[i] = x[9 - i];
x[9 - i] = temp;
}
for (i = 0; i < 10; i++) {
printf("%d ", x[i]);
}
}
运行结果:
15> 输入一行字符,分别统计出其中字母、数字、空格和其它字符的个数:
#include<stdio.h>
#include<string.h>
int main(){
char a[20];
int i;
int gua = 0,
code = 0,
num = 0,
qt = 0;
gets_s(a);
int strNum = strlen(a);
for (i = 0; i < 20; i++) {
if (a[i] == ' ') {
gua++;
}
else if ((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')) {
code++;
}
else if (a[i] >= '0' && a[i] <= '9') {
num++;
}
}
qt = strNum - gua - num - code;
printf("数字个数有:%d\n 字母个数有:%d\n 空格有:%d\n 其他有:%d", num, code, gua, qt);
return 0;
}
运行结果: