第二十六题(不会)

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[]={
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,,,,,,,,,,
,
}; int main(int argc,char** argv)
{ int r,pr;
for(r=;r<;++r)
{
char *p=argv[]; while(pr&&*p)
{
int o=(toupper(*p++)-'A')*++r;
o=(o<||o>=sizeof(t))?:o;
for(pr=;pr>=-;--pr)
{
printf("%c",( ( (pr>=) && (t[o]&(<<pr)))?'#':' ')); }
}
printf("\n");
}
return ;
}

第二十七题

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[] = {
,
,
, };
int i; for(i=;i<SIZEOF(pot);i++)
PrintInt(pot[i]);
return ;
}
知识点讲解:
  • 宏中的“#”

“#”将其后面的宏参数进行字符串化操作(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 > )
{
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 > && y>)
printf("%d %d %d\n",x,y,gcd(x,y));
printf("Enter x y to find their gcd:");
}
printf("\n");
return ;
} Also implement a C function similar to the above to find the GCD of integers.
知识点讲解:
  • 求最大公约数的三种算法

1)辗转相减法

int gcd(int u, int v)
{
while()
{
if (u > v)
{
u -= v;
}
else if (u < v)
{
v -= u;
}
else
{
break;
}
}
return u;
}

2)辗转相除法

int gcd(int u, int v)
{
int mod = ;
if (u < v)
{
int temp = ;
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 = ;
int i = ; if (u > v)
{
min = v;
}
else
{
min = u;
}
for(i = min; i > ; i--)
{
if (u%i== && v%i==)
{
break;
}
}
return i;
}
  • 求最小公倍数

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

第二十九题

What's the output of the following program. (No, it's not !!!)
#include <stdio.h>
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
int main()
{
int y = ;
int *p;
p = malloc(sizeof(int));
*p = ;
y = y/*p; /*dividing y by *p */;
PrintInt(y);
return ;
}

题目讲解:

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

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 ;
}

题目讲解:

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为随机值。

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. jmeter测试某个QPS下的响应时间-设置QPS限制

    本次性能测试的需求中提到测试的目的是“了解博客的首页在负载达到20 QPS时的响应时间”,因此需要控制向博客首页发送请求的负载为20QPS. 一种可行的方法是逐步调整测试计划中的线程计算的数量以及为取 ...

  2. [Swift]枚举

    1. Swift的枚举的基本用法: 1) 和其它语言枚举的意义相同,就是用有限的一组值(不能是无限的)来表示一些特定的含义: 2) Swift使用关键字enum定义枚举类型,定义体中用case定义成员 ...

  3. 最完美的xslt数值函数与字符串函数(转)

    http://www.cnblogs.com/guoxu/articles/1744007.html 任何的编程语言或者是SQL语句都有内置的函数或方法,而强大灵活的xslt技术也是如此.熟练掌握XS ...

  4. VB EOF

    源地址: http://zhidao.baidu.com/link?url=zs49D5HzAgoyd3siE44oLbJlVLsWGpf1zqqvXVWZg_ZGXbFY7BgKFDpCqJdANO ...

  5. [Flex] ButtonBar系列——最后一个项目的样式设置

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  6. Delphi同步互斥总结

    多个线程同时访问一个共享资源或数据时,需要考虑线程同步,Synchronize()是在一个隐蔽的窗口里运行,如果在这里你的任务很繁忙,你的主窗口会阻塞掉:Synchronize()只是将该线程的代码放 ...

  7. 502 Bad Gateway什么意思

    http://baike.baidu.com/link?url=U2ijg5T5PG_tTkY67mqfx07co7qGqvMB32rbLwq4S2ThBSRIWWvU76Y0Mb8Z3z6nbViN ...

  8. XMLHttpRequest的五步使用方法

    <html> <head> <title>Demo</title> <style> body,input,button,select,h1{ ...

  9. 三分钟部署Laxcus大数据管理系统

    Laxcus是Laxcus大数据实验室历时五年,全体系自主设计研发的国内首套大数据管理系统.能够支撑百万台级计算机节点,提供EB量级存储和计算能力,兼容SQL和关系数据库.最新的2.x版本已经实现对当 ...

  10. 6-10k招几个.NET开发工程师(工作地点:成都)

    目前工作的公司是一家做新加坡公司在成都的研发中心,目前有个项目组在做电子医疗记录(EMR)软件系统,在新加坡卖得还不错,由于以前版本的技术有障碍(主要采用WPF技术),目前老板决定投资用比较主流的技术 ...