第二十六题(不会)

  1. 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.
  2. #include<stdio.h>
  3. #include<ctype.h>
  4.  
  5. char t[]={
  6. ,,,,,,,,,,
  7. ,,,,,,,,,,
  8. ,,,,,,,,,,
  9. ,,,,,,,,,,
  10. ,,,,,,,,,,
  11. ,,,,,,,,,,
  12. ,,,,,,,,,,
  13. ,,,,,,,,,,
  14. ,,,,,,,,,,
  15. ,,,,,,,,,,
  16. ,,,,,,,,,,
  17. ,,,,,,,,,,
  18. ,,,,,,,,,,
  19. ,,,,,,,,,,
  20. ,,,,,,,,,,
  21. ,,,,,,,,,,
  22. ,
  23. };
  24.  
  25. int main(int argc,char** argv)
  26. {
  27.  
  28. int r,pr;
  29. for(r=;r<;++r)
  30. {
  31. char *p=argv[];
  32.  
  33. while(pr&&*p)
  34. {
  35. int o=(toupper(*p++)-'A')*++r;
  36. o=(o<||o>=sizeof(t))?:o;
  37. for(pr=;pr>=-;--pr)
  38. {
  39. printf("%c",( ( (pr>=) && (t[o]&(<<pr)))?'#':' '));
  40.  
  41. }
  42. }
  43. printf("\n");
  44. }
  45. return ;
  46. }

第二十七题

  1. What is the output of the following program?
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
  6.  
  7. #define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
  8. int main()
  9. {
  10. /* The powers of 10 */
  11. int pot[] = {
  12. ,
  13. ,
  14. ,
  15.  
  16. };
  17. int i;
  18.  
  19. for(i=;i<SIZEOF(pot);i++)
  20. PrintInt(pot[i]);
  21. return ;
  22. }
  1. 知识点讲解:
  • 宏中的“#”

“#”将其后面的宏参数进行字符串化操作(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. 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.
  2. BTW, what does scanf function return?
  3. #include <stdio.h>
  4. int gcd(int u,int v)
  5. {
  6. int t;
  7. while(v > )
  8. {
  9. if(u > v)
  10. {
  11. t = u;
  12. u = v;
  13. v = t;
  14. }
  15. v = v-u;
  16. }
  17. return u;
  18. }
  19.  
  20. int main()
  21. {
  22. int x,y;
  23. printf("Enter x y to find their gcd:");
  24. while(scanf("%d%d",&x, &y) != EOF)
  25. {
  26. if(x > && y>)
  27. printf("%d %d %d\n",x,y,gcd(x,y));
  28. printf("Enter x y to find their gcd:");
  29. }
  30. printf("\n");
  31. return ;
  32. }
  33.  
  34. Also implement a C function similar to the above to find the GCD of integers.
  1. 知识点讲解:
  • 求最大公约数的三种算法

1)辗转相减法

  1. int gcd(int u, int v)
  2. {
  3. while()
  4. {
  5. if (u > v)
  6. {
  7. u -= v;
  8. }
  9. else if (u < v)
  10. {
  11. v -= u;
  12. }
  13. else
  14. {
  15. break;
  16. }
  17. }
  18. return u;
  19. }

2)辗转相除法

  1. int gcd(int u, int v)
  2. {
  3. int mod = ;
  4. if (u < v)
  5. {
  6. int temp = ;
  7. temp = u;
  8. u = v;
  9. v= temp;
  10. }
  11. while (v)
  12. {
  13. mod = u % v;
  14. u = v;
  15. v = mod;
  16. }
  17. return u;
  18. }

3)穷举法

  1. int gcd(int u, int v)
  2. {
  3. int min = ;
  4. int i = ;
  5.  
  6. if (u > v)
  7. {
  8. min = v;
  9. }
  10. else
  11. {
  12. min = u;
  13. }
  14. for(i = min; i > ; i--)
  15. {
  16. if (u%i== && v%i==)
  17. {
  18. break;
  19. }
  20. }
  21. return i;
  22. }
  • 求最小公倍数

两个数的最小公倍数=两个数的乘积/两个数的最大公约数

第二十九题

  1. What's the output of the following program. (No, it's not !!!)
  2. #include <stdio.h>
  3. #define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
  4. int main()
  5. {
  6. int y = ;
  7. int *p;
  8. p = malloc(sizeof(int));
  9. *p = ;
  10. y = y/*p; /*dividing y by *p */;
  11. PrintInt(y);
  12. return ;
  13. }

题目讲解:

’/*’会被当成注释处理,所以

y = y/*p; /*dividing y by *p */;

等效于

y = y;

所以运行结果为

y: 100

第三十题

  1. The following is a simple C program to read a date and print the date. Run it and explain the behaviour
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int day,month,year;
  6. printf("Enter the date (dd-mm-yyyy) format including -'s:");
  7. scanf("%d-%d-%d",&day,&month,&year);
  8. printf("The date you have entered is %d-%d-%d\n",day,month,year);
  9. return ;
  10. }

题目讲解:

scanf函数的定义为:

  1. int scanf(const char *format,…);

scanf会按照format指定的形式从标准输入读入数据到变量中。

  1. scanf("%d-%d-%d",&day,&month,&year);

当标准输入为“1-2-3”时,day=1,month=2,year=3;

当标准输入为“1,2,3”时,day=1,month和year为随机值。

C puzzles详解【26-30题】的更多相关文章

  1. C puzzles详解【13-15题】

    第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...

  2. C puzzles详解【51-57题】

    第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...

  3. C puzzles详解【46-50题】

    第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...

  4. C puzzles详解【38-45题】

    第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...

  5. C puzzles详解【34-37题】

    第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...

  6. C puzzles详解【31-33题】

    第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...

  7. C puzzles详解【21-25题】

    第二十一题 What is the potential problem with the following C program? #include <stdio.h> int main( ...

  8. C puzzles详解【16-20题】

    第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...

  9. C puzzles详解【9-12题】

    第九题 #include <stdio.h> int main() { float f=0.0f; int i; ;i<;i++) f = f + 0.1f; if(f == 1.0 ...

随机推荐

  1. jquery实现点击页面其他地方隐藏指定元素

    代码实例如下: <!DOCTYPE html><html><head><meta charset=" utf-8"><meta ...

  2. js实现的新闻列表垂直滚动实现详解

    js实现的新闻列表垂直滚动实现详解:新闻列表垂直滚动效果在大量的网站都有应用,有点自然是不言而喻的,首先由于网页的空间有限,使用滚动代码可以使用最小的空间提供更多的信息量,还有让网页有了动态的效果,更 ...

  3. 使用系统UITabbarItem自定义图片显示原本颜色和自定义文字颜色

    ...... ThirdViewController *thirdVC = [[ThirdViewControlleralloc]initWithTitle:@"搜索信息"]; / ...

  4. Python基础03 序列

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! sequence 序列 sequence(序列)是一组有顺序的元素的集合 (严格的 ...

  5. linux下分卷tar.bz文件的合并并解压缩

    linux下分卷tar.bz文件的合并并解压缩 例: linux.tar.bz2.001;linux.tar.bz2.002;linux.tar.bz2.003; 首先 cat linux.tar.b ...

  6. java异常处理机制throws

    throws可以在方法声明时抛出的异常.原则上throws声明的异常,一定要在该方法中抛出,否则没有意义. 相反的,若方法中我们主动通过throw抛出一个异常,应该在throws中声明该种类异常,通知 ...

  7. 使用 Heka 导入自定义的nginx日志到Elasticsearch

    重置Heka执行进度 heka的进度配置文件存在配置项 base_dir 设置的目录,只需要删除这个文件夹下面的内容,就可以完全重置heka的进度. base_dir 配置项默认是在下面目录: '/v ...

  8. esriSRProjCS4Type Constants

    ArcGIS Developer Help  (Geometry)   esriSRProjCS4Type Constants See Also esriSRProjCSType Constants ...

  9. POJ 2154 【POLYA】【欧拉】

    前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...

  10. 在点击HOME键时, 在点击icon回到原来的应用。

    参考资料: http://www.linuxidc.com/Linux/2012-01/51332.htm