初学水平有限,记录一下自己做的书上的部分习题,有错的地方还请多多交流指正。
2.5节 表达式 (习题22页)
1、假设变量x和n是两个正整数,我们知道x/n这个表达式的结果要取Floor,例如x是17,n是4,则结果是4。如果希望结果取Ceiling应该怎么写表达式呢?例如x是17,n是4,则结果是5;x是16,n是4,则结果是4。
1 #include <stdio.h>
2 int main(int argc, char *argv[])
3 {
4 int x,n,num;
5
6 printf("please input num x,n\n");
7 scanf("%d %d",&x,&n);
8
9 if(x%n==0)
10 printf("%d/%d=%d\n",x,n,num=x/n);
11 else
12 printf("%d/%d=%d\n",x,n,num=x/n+1);
13
14 return 0;
15 }
4.2节 if/else语句
(45页)写一个函数,参数是整型变量x,功能是打印x的个位和十位。(书上两道题功能差不多只遍了一道)
1 #include <stdio.h>
2 int main(int argc, char *argv[])
3 {
4 int x,y,z;
5 printf("please input a num:");
6 scanf("%d",&x);
7 y=x%10;
8 z=(x/10)%10;
9 printf("the last num is %d\n",y);
10 printf("the second num is %d\n",z);
11 return 0;
12 }
13
5.1节 return语句
(54页)1、编写一个布尔函数int is_leap_year(int year),判断参数year是不是闰年。如果某年份能被4整除,但不能被100整除,那么这一年就是闰年,此外,能被400整除的年份也是闰年。
方法一:
1 #include <stdio.h>
2
3 int is_leap_year(int year)
4 {
5 if ((year%4 == 0) && (year%100 != 0))
6 printf("%d is leap year\n",year);
7 else if (year%400 == 0)
8 printf("%d is leap year\n",year);
9 else
10 printf("%d is not leap year\n",year);
11 return 0;
12 }
13
14 int main(int argc, char *argv[])
15 {
16 int year;
17 printf("please input year\n");
18 scanf("%d",&year);
19 is_leap_year(year);
20
21
22 return 0;
23 }
方法二:
1 #include <stdio.h>
2
3 int is_leap_year(int year)
4 {
5 if ((year%4 == 0) && (year%100 != 0))
6 return 1;
7 else if (year%400 == 0)
8 return 1;
9 else
10 return 0;
11 }
12
13 int main(int argc, char *argv[])
14 {
15 int year;
16 printf("please input year\n");
17 scanf("%d",&year);
18 if(is_leap_year(year) == 1)
19
20 printf("%d is leap year\n",year);
21
22 else
23 printf("%d is not leap year\n",year);
24 return 0;
25 }
2、编写一个函数double myround(double x),输入一个小数,将它四舍五入。例如myround(-3.51)的值是-4.0,myround(4.49)的值是4.0。可以调用math.h中的库函数ceil和floor实现这个函数。
我写的代码里没有用到ceil和floor函数,本来是想用余数大于等于5小于5去写的,数据类型转换方面遇到点问题,编出来的程序老是不对,就换了个思路。
1 #include <stdio.h>
2
3 double myround(double x)
4 {
5 if(x>0.0)
6 {if((x-0.5) >= (int) x)
7 return (((int) x)+1);
8 else if ((x-0.5) < (int)x )
9 return((int)x);
10 }
11
12 else if(x<0.0)
13 {if((x-0.5) <= (int)x -1 )
14 return (((int) x)-1);
15 else if ((x-0.5) > (int)x -1)
16 return((int)x);
17 }
18 else
19 return 0.0;
20
21 }
22
23
24 int main(int argc, char *argv[])
25 {
26 double y;
27 putchar('\n');
28 printf("please input a num:");
29 scanf("%lf",&y);
30
31 printf("%f\n",myround(y));
32 return 0;
33 }
5.3节 递归
(62页)1.求最大公约数
负整数和0这两种情况的公约数定义不太清楚,按自己想的写了,如果是两个数有0就返回0了。
1 #include <stdio.h>
2 #include <math.h>
3 #include <stdlib.h>
4 int Euclid(int a,int b)
5 {
6 a = abs(a);
7 b = abs(b);
8 if(a != 0 && b != 0)
9 {
10 if(a%b==0)
11 return b;
12 else if (a%b != 0)
13 return(Euclid(b,a%b));
14 }
15 else
16 return 0;
17 }
18
19
20 int main(int argc, char *argv[])
21 {
22 int a,b;
23 printf("please input two numbers:");
24 scanf("%d %d",&a,&b);
25 printf("GCD is %d\n",Euclid(a,b));
26
27 return 0;
28 }
2.编写递归函数求Fibonacci数列的第N项
1 #include <stdio.h>
2
3 int fib(int x)
4 {
5 if(x == 0)
6 return 1;
7 else if(x == 1)
8 return 1;
9 else if(x >= 1)
10 return(fib(x-1)+fib(x-2));
11
12 }
13
14 int main(int argc, char *argv[])
15 {
16 int n;
17 printf("please input a postive number:");
18 scanf("%d",&n);
19 if(n>=0)
20 printf("the fibonacci of the number is %d\n",fib(n));
21 else
22 printf("error!please retry!\n");
23
24
25 return 0;
26 }
暂时就看到这些,隔几天写了新的再更新一下。