今天回到了学校,由于快要毕业可是自己的数据结构的知识还是非常的欠缺,于是萌发了学习一下数据结构的想法,我打算把这本书中的所有题敲击一遍,聊以自慰吧!!
第二章、C语言基础知识
编程题
1、打印9X9乘法表
#include <stdio.h>
#include <stdlib.h>
void black_space()
{
printf("\t");
}
int main()
{
int i,j;
for (j=1;j<=9;j++)
{
for (i=j;i<=9;i++)
{
printf("%d*%d=%d\t",j,i,j*i);
}
printf("\n");
for (int k=1;k<=j;k++)
{
black_space();
}
}
system("pause");
return 0;
}
有个问题,我现在才发现,tab键并不是单纯的空格作用,还有很微妙的作用,比如这道题中如果不是用tab键来分割元素的话,会导致最后的输出结果不是很爽朗。
2、编写一个万年历
我之前写万年历的时候都是很费劲的,今天看了这本书之后我才发现原来有蔡勒公式,这个公式真的太nice了。
蔡勒(Zeller)公式,是一个计算星期的公式,随便给一个日期,就能用这个公式推算出是星期几。
W =〔 [c/4] - 2c + y + [y/4] + [13 * (m+1) / 5] + d - 1 〕% 7
(或者是:w= 〔y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 〕% 7)
w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
c:世纪减1(年份前两位数)
y:年(后两位数)
m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d:日
[ ]代表取整,即只要整数部分。
当然你发现其中最后的d-1根本没有太大的作用。
直接上代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *week[7]=
{
"sun","mon","tue","wed","thu","fri","sat"
};
int years,month,days,day;
int leap;
printf("please input the year and month :\n");
scanf("%d%d",&years,&month);
int w,y,c,m;
m = month;
y = years;
if (month==1 || month==2)
{
m = m+12;
y-=1;
}
y = y%100;
c = y/100;
w = y+y/4+c/4-2*c+26*(m+1)/10;
w=w%7;
if (w<0)
w += 7;
if (y%400==0||(y%4==0 && y%100!=0))
leap = 1;
else
leap = 0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if(leap==1)
days = 29;
else
days = 28;
}
printf("%d年%d月的日历:\n\n",years,month);
for(int i=0;i<7;i++)
printf("%s\t",week[i]);
printf("\n");
int i;
for(i=0;i<w;i++)
printf("\t");
for (day=1;day<=days;day++,i++)
{
if (i%7==0)
printf("\n");
printf("%d\t",day);
}
system("pause");
return 0;
}
比之前的那种通过什么19**年的那一天是星期几这样算,好多了!
3、对10个整数进行正序排序,这道题很简单,关键就是搞清楚C语言中传值和传址的区别。
不多述,直接看代码吧
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *sort(int a[],int n)
{
int temp;
for (int i=0;i<n-1;i++)
{
for (int j=i+1;j<n;j++)
{
if (a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
int main()
{
srand(time(0));
int a[10];
int i,j;
for (i=0;i<10;i++)
{
a[i] = rand()%100;
}
printf("before sorting:\n");
for (i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
sort(a,10);
printf("after sorting:\n");
for (i=0;i<10;i++)
{
printf("%d\t",a[i]);
}
system("pause");
return 0;
}
这道题,我相信大家都没有问题
4、对输入的整数进行逆序输出,这道题唯一的考点,就是怎么思考这个问题,如果到后面的章节,其实很好办,直接上栈就可以了,先进后出呗,现在我们用一维动态数组也可以解决这种简单问题‘
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
int number;
int i=0,j;
int *a = (int *)malloc(sizeof(int));
while(1)
{
printf("please input a number:");
scanf("%d",&number);
a[i]=number;
printf("%d\t",a[i]);
i++;
a=(int *)realloc(a,(i+1)*sizeof(int));
if (number==000)
{
break;
}
}
printf("\n");
for (j=i-2;j>=0;j--)
{
printf("%d\t",a[j]);
}
printf("\n");
system("pause");
return 0;
}
同样也是没有难度。
5、对于字符串的比较大小,这道题,其实还是费了我不少的功夫,最后写出来的代码其实不是我想要的,我的代码中固定了需要排序的字符串的个数,我想写的是随便输入,最后统一比较就可以,
这个问题的核心就是动态二维数组的申请,在C语言中这样实现
char **s = (char**)malloc(sizeof(char*)*NROWS);
for(i=0;i<NROWS;i++)
s[i] = (char*)malloc(sizeof(char)*NCOLS);
我的意思其实就是不想要NROWS这个参数,如果可以通过自加加(++)的方式得到是最好的,有待于改进。
直接上该题的代码吧
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort(char **ss,int len)
{
char *p;
for (int i=0;i<len-1;i++)
{
for (int j=i;j<len;j++)
{
if (strcmp(ss[i],ss[j])>0)
{
p = ss[i];
ss[i] = ss[j];
ss[j] = p;
}
}
}
}
int main()
{
int n;
char a[50];
printf("please input a number:\n");
scanf("%d",&n);
char **s = (char **)malloc(n*sizeof(char *));
fflush(stdin);
for (int i=0;i<n;i++)
{
printf("please input a string:\n");
gets(a);
s[i]=(char *)malloc(strlen(a)*sizeof(char));
strcpy(s[i],a);
}
printf("before sorting:\n");
for(int i=0;i<n;i++)
puts(s[i]);
sort(s,n);
printf("after sorting:\n");
for(int i=0;i<n;i++)
puts(s[i]);
system("pause");
return 0;
}
其实看一下整个程序没有难点,我对二维数组真心不是很熟悉
6、对学生的信息按成绩进行排序,非常简单,就是用了一个结构体指针
上代码
#include <stdio.h>
#include <stdlib.h>
struct student
{
long ID;
char name[20];
float english;
float chinese;
float math;
}stu[3];
void sort(struct student *p,int n)
{
p=stu;
struct student s;
for (int i=0;i<n-1;i++)
{
for (int j=i;j<n;j++)
{
if ((p[i].chinese+p[i].english+p[i].math)>(p[i+1].chinese+p[i+1].english+p[i+1].math))
{
s = p[i];
p[i] = p[i+1];
p[i+1] = s;
}
}
}
}
int main()
{
for (int i=0;i<3;i++)
{
printf("please input the ID:");
scanf("%ld",&stu[i].ID);
fflush(stdin);
printf("please input the name :");
gets(stu[i].name);
fflush(stdin);
printf("please input the score:\n");
printf("English=");
scanf("%f",&stu[i].english);
fflush(stdin);
printf("chinese=");
scanf("%f",&stu[i].chinese);
fflush(stdin);
printf("math=");
scanf("%f",&stu[i].math);
//fflush(stdin);
}
printf("before sorting:\n");
for(int i=0;i<3;i++)
{
printf("ID:%ld\tName:%s\tScore:%0.1f\tScore:%0.1f\tScore:%0.1f\tScore:%0.1f\n",
stu[i].ID,stu[i].name,stu[i].chinese+stu[i].english+stu[i].math,
stu[i].chinese,stu[i].english,stu[i].math);
}
printf("after sorting:\n");
sort(stu,3);
for(int i=0;i<3;i++)
{
printf("ID:%ld\tName:%s\tScore:%0.1f\tScore:%0.1f\tScore:%0.1f\tScore:%0.1f\n",
stu[i].ID,stu[i].name,stu[i].chinese+stu[i].english+stu[i].math,
stu[i].chinese,stu[i].english,stu[i].math);
}
system("pause");
return 0;
}
结构体指针其实很简单,我认为指针中
比较难得部分是函数指针和指针的函数那个部分,我不是很明白
7、递归的方式求最大的元素,我对此题不发表什么言论,没有值得说的部分。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
int _max(int a[],int n)
{
if (n==1)
return *a;
else
return a[n-1]>_max(a,n-1)?a[n-1]:_max(a,n-1);
}
int main()
{
clock_t start,end;
start = clock();
srand(time(0));
int a[N]={0};
for (int i=0;i<N;i++)
{
a[i]=rand()%10000;
}
printf("the random number is:\n");
for (int i=0;i<N ;i++)
{
printf("%d\t",a[i]);
if ((i+1)%10==0)
{
printf("\n");
}
}
printf("the max number is:%d\n",_max(a,N));
end = clock()-start;
printf("the duration is %f\n",end);
system("pause");
return 0;
}