GDPU C语言 天码行空11
🍑 C语言实验专栏
填空题
1. 递归求数位
#include <stdio.h>
void order(int n)
{
if(n<10)
printf("%d",n);
else
{
order(n/10);
order(n%10);
}
}
void main()
{
int n;
scanf("%d",&n);
printf("out:");
order(n);
}
2. 递归求阶乘
#include <stdio.h>
double fac(int n)
{
double f;
if(n==1)
f=1;
else
f=n*fac(n-1);
return f;
}
main()
{
double s=0;
int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
s= s + fac(i);
printf("s=%.2lf",s);
return 0;
}
编程题
1. 字符串排序
🍤 选择排序:每次选择一个最大/小的元素放在待排序序列的第一个位置
🍤 strcmp(字符串1,字符串2);
🍿 串1 > 串2 :返回值 > 0
🍿 串1 = 串2 :返回值 = 0
🍿 串1 < 串2 :返回值 < 0
#include<stdio.h>
#include<string.h>
int st[15];//实现去重
int main() {
int n, i, j;
scanf("%d", &n);
char a[100][100];
for(i = 0; i < n; i++)
scanf("%s", a[i]);
int t = n;
while(t--)//找 n 次
{
int min = -1;//记录最小值的下标
for(j = 0; j < n; j++)//枚举所有元素
//st[j]==0表示当前元素待排序,找到最小元素的下标
if(!st[j] && (strcmp(a[j], a[min])< 0 || min == -1))
min = j;
st[min] = 1;//1,标记一下min所对应的元素已经排好序啦
//直接输出,不管啦(一般需要与当前未排序序列的第一个元素交换)
printf("%s\n", a[min]);
}
return 0;
}
2. 时间换算问题
#include <stdio.h>
int main()
{
int h,m,s;
int skip;
scanf("%d:%d:%d",&h,&m,&s);
scanf("%d",&skip);
int mm = m,hh = h;
int ss = s+skip;
if(ss >= 60)
{
mm += ss/60;
ss %= 60;
}
if(mm >= 60){
hh += mm/60;
mm %= 60;
}
if(hh >= 24)
hh %= 24;
printf("After %d seconds is %d:%d:%d",skip,hh,mm,ss);
return 0;
}
程序片断编程题
1. 学生成绩分析
#include "stdio.h"
#define N 10 //学生人数上限
#define M 5 //课程门数上限
struct stud
{ char name[10]; //姓名
double course[M];//成绩
double aver; //平均分
}s[N];
int main()
{
int n,m,i,j;
double sum,course[M]={0}; //用于统计每门课程的平均分
scanf("%d,%d",&n,&m); //输入学生人数与课程门数
printf("%d %d\n",n,m);
//输入学生信息
for(i = 0; i < n; i++)
{
scanf("%s",s[i].name);
sum = 0.0;
for(j = 0; j < m; j++)
{
int t;
scanf("%d",&t);
s[i].course[j] = t;
course[j] += t;
sum += t;
}
s[i].aver = sum/m;
}
printf("name ");
for(j=0;j<m;j++) //求每门课程的平均分
{ course[j]=course[j]/n;
printf("CNO:%d ",j+1);//显示栏目
}
printf("\n");
for(i=0;i<n;i++)
{ printf("%10s",s[i].name);
for(j=0;j<m;j++)
if(s[i].course[j]<course[j])
printf("%5.1f ",s[i].course[j]);
else
printf("%8c",32);
printf("\n");
}
return 0;
}
2. 图书信心查询问题
🤠 字符串匹配问题
🍑 空处代码
int len1 = strlen(s1);
int len2 = strlen(s2);
int i, j;
for(i = 0; i <= len2-len1; i++)//枚举s2中长度为 s1的长度的 所有可能起点
{
int x = 0, y = i;
while(*(s1 + x) == *(s2 + y))
{
if(x == len1)//匹配完成,结束循环
break;
x++;
y++;
}
if(x == len1)
return 1;
}
return 0;
🍑 完整代码
#include "stdio.h"
#include "string.h"
#define N 10
struct Book
{
char name[100];
float price;
};
int instring(char *s1, char *s2); //s1若属于s2的字串,则返回1,否则返回0
int main()
{
struct Book books[N] = { { "Fling in sky", 23.0 }, { "I love my mother", 15.5 }, { "My love", 25.6 }, { "Color is red", 34.9 }, { "Black and red", 20.5 }, { "Let me love you", 16.0 }, { "Sun is red", 45.0 }, { "Flower sunshine", 23.8 }, { "Say love not easy", 45.9 }, { "Friday black", 34.0 } };
int i;
char name[20];
gets(name);
for(i = 0; i<N; i++)
if(instring(name, books[i].name))
printf("%30s,%6.2f\n", books[i].name, books[i].price);
return 0;
}
//字符串匹配,s1 是待查找串,s2 是源串,s1 能与 s2的某个子串匹配 则返回非0值(true)
int instring(char *s1, char *s2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int i, j;
for(i = 0; i <= len2-len1; i++)//枚举s2中长度为 s1的长度的 所有可能起点
{
int x = 0, y = i;
while(*(s1 + x) == *(s2 + y))
{
if(x == len1)//匹配完成,结束循环
break;
x++;
y++;
}
if(x == len1)
return 1;
}
return 0;//s2 中所有与 s1长度相等的子串都匹配失败则返回 0 (false)
}
3. 复数的乘积运算
🍑 结构体
🍑 复数乘法运算法则:(a+bi)(c+di)=(ac-bd)+(bc+ad)i。
#include "stdio.h"
struct complex
{
double sb;
double xb;
};
struct complex complexproduct(struct complex s1, struct complex s2);
int main()
{
struct complex c1, c2, c3;
scanf("%lf+%lfi", &c1.sb, &c1.xb);
scanf("%lf+%lfi", &c2.sb, &c2.xb);
//自定义代码1
c3 = complexproduct(c1, c2);
printf("c1*c2=%f+%fi\n", c3.sb, c3.xb);
return 0;
}
struct complex complexproduct(struct complex s1, struct complex s2)
{
//自定义代码2
double sb = s1.sb*s2.sb - s1.xb*s2.xb;
double xb = s1.sb*s2.xb + s1.xb*s2.sb;
struct complex res = { sb, xb };
return res;
}
4. 递归函数ACK的实现
🍑 按照定义直接模拟
#include "stdio.h"
int ack(int m, int n)
{
if(m == 0)
{
return n + 1;
}
else if((m != 0) && (n == 0))
{
ack(m - 1, 1);
}
else if((m != 0) && (n != 0))
{
ack(m - 1, ack(m, n - 1));
}
}
int main()
{
int m, n, s;
scanf("%d,%d", &m, &n);
s = ack(m, n);
printf("s=%d\n", s);
return 0;
}