C puzzles详解【26-30题】
2014-09-23 07:20
218 查看
第二十六题(不会)
第二十七题
宏中的“#”
“#”将其后面的宏参数进行字符串化操作(stringfication),即对它所引用的宏变量通过替换后在其左右各加上一个双引号。更多关于宏中”#””##”的讲解见第五题。
C语言中的二进制、八进制、十进制、十六进制数表示
以十进制数100为例:
二进制:C语言中无二进制数的表示方法;
八进制:以0开头,如0144;
十进制:以非0开头,如100;
十六进制:以0x开头,如0x64;
题目讲解:
输出为:
pot[i]:1
pot[i]:8
pot[i]:64
pot[i]:1000
PrintInt(pot[i])被替换为printf("%s:%d\n",“pot[i]”,pot[i]);
0001,0010,0100表示八进制数,1000表示十进制数。
第二十八题
求最大公约数的三种算法
1)辗转相减法
2)辗转相除法
3)穷举法
求最小公倍数
两个数的最小公倍数=两个数的乘积/两个数的最大公约数
第二十九题
题目讲解:
’/*’会被当成注释处理,所以
y = y/*p; /*dividing y by *p */;
等效于
y = y;
所以运行结果为
y: 100
第三十题
题目讲解:
scanf函数的定义为:
scanf会按照format指定的形式从标准输入读入数据到变量中。
如
当标准输入为“1-2-3”时,day=1,month=2,year=3;
当标准输入为“1,2,3”时,day=1,month和year为随机值。
The following is a simple program which implements a minimal version of banner command available on most *nix systems. Find out the logic used in the program. #include<stdio.h> #include<ctype.h> char t[]={ 0,0,0,0,0,0,12,18,33,63, 33,33,62,32,62,33,33,62,30,33, 32,32,33,30,62,33,33,33,33,62, 63,32,62,32,32,63,63,32,62,32, 32,32,30,33,32,39,33,30,33,33, 63,33,33,33,4,4,4,4,4,4, 1,1,1,1,33,30,33,34,60,36, 34,33,32,32,32,32,32,63,33,51, 45,33,33,33,33,49,41,37,35,33, 30,33,33,33,33,30,62,33,33,62, 32,32,30,33,33,37,34,29,62,33, 33,62,34,33,30,32,30,1,33,30, 31,4,4,4,4,4,33,33,33,33, 33,30,33,33,33,33,18,12,33,33, 33,45,51,33,33,18,12,12,18,33, 17,10,4,4,4,4,63,2,4,8, 16,63 }; int main(int argc,char** argv) { int r,pr; for(r=0;r<6;++r) { char *p=argv[1]; while(pr&&*p) { int o=(toupper(*p++)-'A')*6+6+r; o=(o<0||o>=sizeof(t))?0:o; for(pr=5;pr>=-1;--pr) { printf("%c",( ( (pr>=0) && (t[o]&(1<<pr)))?'#':' ')); } } printf("\n"); } return 0; }
第二十七题
What is the output of the following program? #include <stdio.h> #include <stdlib.h> #define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0])) #define PrintInt(expr) printf("%s:%d\n",#expr,(expr)) int main() { /* The powers of 10 */ int pot[] = { 0001, 0010, 0100, 1000 }; int i; for(i=0;i<SIZEOF(pot);i++) PrintInt(pot[i]); return 0; }
知识点讲解:
宏中的“#”
“#”将其后面的宏参数进行字符串化操作(stringfication),即对它所引用的宏变量通过替换后在其左右各加上一个双引号。更多关于宏中”#””##”的讲解见第五题。
C语言中的二进制、八进制、十进制、十六进制数表示
以十进制数100为例:
二进制:C语言中无二进制数的表示方法;
八进制:以0开头,如0144;
十进制:以非0开头,如100;
十六进制:以0x开头,如0x64;
题目讲解:
输出为:
pot[i]:1
pot[i]:8
pot[i]:64
pot[i]:1000
PrintInt(pot[i])被替换为printf("%s:%d\n",“pot[i]”,pot[i]);
0001,0010,0100表示八进制数,1000表示十进制数。
第二十八题
The following is the implementation of the Euclid's algorithm for finding the G.C.D(Greatest Common divisor) of two integers. Explain the logic for the below implementation and think of any possible improvements on the current implementation. BTW, what does scanf function return? #include <stdio.h> int gcd(int u,int v) { int t; while(v > 0) { if(u > v) { t = u; u = v; v = t; } v = v-u; } return u; } int main() { int x,y; printf("Enter x y to find their gcd:"); while(scanf("%d%d",&x, &y) != EOF) { if(x >0 && y>0) printf("%d %d %d\n",x,y,gcd(x,y)); printf("Enter x y to find their gcd:"); } printf("\n"); return 0; } Also implement a C function similar to the above to find the GCD of 4 integers.
知识点讲解:
求最大公约数的三种算法
1)辗转相减法
int gcd(int u, int v) { while(1) { if (u > v) { u -= v; } else if (u < v) { v -= u; } else { break; } } return u; }
2)辗转相除法
int gcd(int u, int v) { int mod = 0; if (u < v) { int temp = 0; temp = u; u = v; v= temp; } while (v) { mod = u % v; u = v; v = mod; } return u; }
3)穷举法
int gcd(int u, int v) { int min = 0; int i = 0; if (u > v) { min = v; } else { min = u; } for(i = min; i > 0; i--) { if (u%i==0 && v%i==0) { break; } } return i; }
求最小公倍数
两个数的最小公倍数=两个数的乘积/两个数的最大公约数
第二十九题
What's the output of the following program. (No, it's not 10!!!) #include <stdio.h> #define PrintInt(expr) printf("%s : %d\n",#expr,(expr)) int main() { int y = 100; int *p; p = malloc(sizeof(int)); *p = 10; y = y/*p; /*dividing y by *p */; PrintInt(y); return 0; }
题目讲解:
’/*’会被当成注释处理,所以
y = y/*p; /*dividing y by *p */;
等效于
y = y;
所以运行结果为
y: 100
第三十题
The following is a simple C program to read a date and print the date. Run it and explain the behaviour #include <stdio.h> int main() { int day,month,year; printf("Enter the date (dd-mm-yyyy) format including -'s:"); scanf("%d-%d-%d",&day,&month,&year); printf("The date you have entered is %d-%d-%d\n",day,month,year); return 0; }
题目讲解:
scanf函数的定义为:
int scanf(const char *format,…);
scanf会按照format指定的形式从标准输入读入数据到变量中。
如
scanf("%d-%d-%d",&day,&month,&year);
当标准输入为“1-2-3”时,day=1,month=2,year=3;
当标准输入为“1,2,3”时,day=1,month和year为随机值。
相关文章推荐
- PythonTip(26~30)题详解
- Lesson 26-30
- [视频]学打羽毛球26-30
- effecitve c++ 条款26 -30
- Project Euler 26~30
- C puzzles详解【21-25题】
- C puzzles详解【1-5题】
- SQL Server 2008 R2:error 26 开启远程连接详解
- PAT 1026. Table Tennis (30) 【26/30】
- compilation terminated. In file included from <command-line>:0:0: /usr/include/stdc-predef.h:30:26:
- 【译】提高网站访问速度的34条军规 26-30
- 尚学堂 马士兵 struts_2视频教程 笔记心得 1——30课详解
- chrome 漫画连载(26-30)
- SQL Server 2008 R2:error 26 开启远程连接详解
- windows 8 应用小技巧(26-30)
- 用户注册模块详解(30)
- 用户注册模块详解(30)
- 2_Lesson 26-30
- 提高网站访问速度的34条军规(26-30)
- C puzzles详解【13-15题】