编程题部分:
1,编写一个程序,创建一个包含26个元素的数组,并在其中储存26个小写字母,然后打印所有内容。
#include <stdio.h>
#include <string.h>
#include <float.h>
#define NUM 26
int main(void)
{
char zimu[NUM];
int i;
for(i=0;i<NUM;i++)
zimu[i] = 'a'+i;
for(i=0;i<NUM;i++)
printf("%c",zimu[i]);
printf("\n");
return 0;
}
运行结果:
2,使用嵌套循环,按下面的格式打印字符:
$
$$
$$$
$$$$
$$$$$
#include <stdio.h>
#include <string.h>
#include <float.h>
#define rows 5
int main(void)
{
int row;
int column;
for(row=0;row<rows;row++)
{
for(column=0;column<=row;column++)
printf("$");
printf("\n");
}
return 0;
}
运行结果:
3,使用嵌套循环,按下面的格式打印字母:
F
FE
FED
FEDC
FEDCB
FEDCBA
在这里插入代码片
运行结果:
4,使用嵌套循环,按下面的格式打印字母:
A
BC
DEF
GHIJ
KLMNO
PQRSTU
#include <stdio.h>
#include <string.h>
#include <float.h>
#define NUM 6
int main(void)
{
int row,column;
char count='A';
for(row=0; row<NUM; row++)
{
for(column=0; column<row+1; column++)
{
printf("%C", count);
count+=1;
}
printf("\n");
}
return 0;
}
运行结果:
5,编写一个程序,提示用户输入大写字母。使用嵌套循环以下面的金字塔的格式打印字母:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
打印这样的图形,要根据用户输入的字母来决定。例如,上面的图形是在用户输入E后的打印结果。
在这里插入代码片
6,编写一个程序打印一个表格,每一行打印一个整数、该数的平方、该数的立方。要求用户输入表格的上下限。使用一个for循环。
#include <stdio.h>
#include <string.h>
#include <float.h>
int main(void)
{
int max,min;
int num,square,cube;
printf("please enter the min number: \n");
scanf("%d", &min);
printf("please enter the max number: \n");
scanf("%d", &max);
for(num=min; num<=max; num++)
{
square = num*num;
cube = num*num*num;
printf("number is %d square is %d cube is %d\n", num, square, cube);
}
return 0;
}
运行结果:
7,编写一个程序把一个单词读入一个字符数组中,然后倒序打印这个单词。提示:strlen()函数(第4章介绍过)可用于计算数组最后一个字符的下标。
8,编写一个程序,要求用户输入两个浮点数,并打印两数之差除以两数乘积的结果。在用户输入非数字之前,程序应循环处理用户输入的每对值。
#include <stdio.h>
#include <string.h>
#include <float.h>
int main(void)
{
double n,m;
double num;
printf("please enter the series of num: \n");
while(scanf("%lf %lf", &n,&m) == 2)
{
num = (n-m)/n*m;
printf("the result is %lf\n", num);
printf("please enter the next number:(no number to quit)\n");
}
return 0;
}
运行结果:
9,修改练习8,使用一个函数返回计算的结果。
10,编写一个程序,要求用户输入一个上限整数和一个下限整数,计算从上限到下限范围内所有整数的平方和,并显示计算结果。然后程序继续提示用户输入上限和下限整数,并显示结果,直到用户输入的上限整数小于下限整数为止。程序的运行示例如下:
Enter lower and upper integer limits: 5 9
The sums of the squares from 25 to 81 is 255
Enter next set of limits: 3 25
The sums of the squares from 9 to 625 is 5520
Enter next set of limits: 5 5
Done
#include <stdio.h>
#include <string.h>
#include <float.h>
int main(void)
{
int n,m;
int num,sum;
printf("Enter lower and upper integer limits:\n");
scanf("%d %d", &n,&m);
for(num=n; num<=m; num++)
{
sum = sum+num*num;
}
for(num=n; num<=m; num++)
{
printf("The sum of the squares from %d to %d is %d\n", n*n,m*m,sum);
printf("Enter next set of limits:\n");
scanf("%d %d", &n, &m);
}
printf("Done");
return 0;
}
运行结果:
11,编写一个程序,在数组中读入8个整数,然后按倒序打印这8个整数。
在这里插入代码片
12,考虑下面两个无限序列:
1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + …
1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + …
编写一个程序计算这两个无限序列的总和,直到到达某次数。提示:奇数个-1 相乘得-1,偶数个-1相乘得1。让用户交互地输入指定的次数,当用户输入0或负值时结束输入。查看运行100项、1000项、10000项后的总和,是否发现每个序列都收敛于某值?
13,编写一个程序,创建一个包含8个元素的int类型数组,分别把数组元素设置为2的前8次幂。使用for循环设置数组元素的值,使用do while循环显示数组元素的值。
#include <stdio.h>
#include <string.h>
#include <float.h>
#define SIZE 8
int main(void)
{
int zimu[SIZE];
int count;
int num;
// printf("please enter %d number of two: \n", SIZE);
for(count=0,num=1; count<SIZE; count++)
{
num *=2;
zimu[count] = num;
}
do
{
printf("zimu[%d] is %d\n", count-1, zimu[count-1]);
count--;
}
while(count>0);
return 0;
}
运行结果:
14,编写一个程序,创建两个包含8个元素的double类型数组,使用循环提示用户为第一个数组输入8 个值。第二个数组元素的值设置为第一个数组对应元素的累积之和。例如,第二个数组的第 4个元素的值是第一个数组前4个元素之和,第二个数组的第5个元素的值是第一个数组前5个元素之和(用嵌套循环可以完成,但是利用第二个数组的第5个元素是第二个数组的第4个元素与第一个数组的第5个元素之和,只用一个循环就能完成任务,不需要使用嵌套循环)。最后,使用循环显示两个数组的内容,第一个数组显示成一行,第二个数组显示在第一个数组的下一行,而且每个元素都与第一个数组各元素相对应。
15,编写一个程序,读取一行输入,然后把输入的内容倒序打印出来。可以把输入储存在char类型的数组中,假设每行字符不超过255。回忆一下,根据%c转换说明,scanf()函数一次只能从输入中读取一个字符,而且在用户按下Enter键时scanf()函数会生成一个换行字符(\n)。
#include <stdio.h>
#include <string.h>
#include <float.h>
#define BUF 255
#include<stdio.h>
int main(void)
{
char input[BUF];
int i,t;
printf("enter the the input:\n");
for(i=0; i<255 && input[i-1]!='\n'; i++)
scanf("%c",&input[i]);
for(t=i-2; t>=0; t--)
printf("%c",input[t]);
return(0);
}
运行结果:
16,Daphne以10%的单利息投资了100美元(也就是说,每年投资获利相当于原始投资的10%)。Deirdre以 5%的复合利息投资了 100 美元(也就是说,利息是当前余额的 5%,包含之前的利息)。编写一个程序,计算需要多少年Deirdre的投资额才会超过Daphne,并显示那时两人的投资额。
#include <stdio.h>
#include <string.h>
#include <float.h>
#define rate1 0.10
#define rate2 0.05
#define initial 100.00
#include<stdio.h>
int main(void)
{
double ra1=initial;
double ra2=initial;
int year=0;
while (ra2 <= ra1)
{ ra1 += rate1 * initial;
ra2 += rate2 * ra2;
++year;
}
printf("Investment values after %d years:\n", year);
printf("Daphne: $%.2f\n", ra1);
printf("Deidre: $%.2f\n", ra2);
return(0);
}
运行结果:
17,Chuckie Lucky赢得了100万美元(税后),他把奖金存入年利率8%的账户。在每年的最后一天, Chuckie取出10万美元。编写一个程序,计算多少年后Chuckie会取完账户的钱。
#include <stdio.h>
#include <string.h>
#include <float.h>
#define rate1 10
#define rate2 0.08
#define initial 100.00
#include<stdio.h>
int main()
{
double a = 1e6;
int n;
for(n = 0;a > 0;n++)
{
a *= 1.08;
a -= 1e5;
}
printf("%d\n", n);
return 0;
}
运行结果:
18,Rabnud博士加入了一个社交圈。起初他有5个朋友。他注意到他的朋友数量以下面的方式增长。第1周少了1个朋友,剩下的朋友数量翻倍;第2周少了2个朋友,剩下的朋友数量翻倍。一般而言,第N周少了N个朋友,剩下的朋友数量翻倍。编写一个程序,计算并显示Rabnud博士每周的朋友数量。该程序一直运行,直到超过邓巴数(Dunbar’s number)。邓巴数是粗略估算一个人在社交圈中有稳定关系的成员的最大值,该值大约是150。
#include <stdio.h>
#include <string.h>
#include <float.h>
int main()
{
int n = 5;
int i;
for(i = 1; n <= 150; i++)
{
printf("%d\n", n);
n -= i;
n *= 2;
}
return 0;
}
运行结果: