两道面试题

tfnadnt 2008-11-04 02:12:22
1. #define MIXFUNC(func) do ## func()

在预编译时展开的代码行是什么?(这个貌似在C里没见过)

2.有100块石头,1只大象一次能扛19块,1只老虎一次能扛12块,4只松鼠一起一次能扛一块。有15只动物一次就将这
100块石头扛完了,请编程求出这三种动物每种多少只?
...全文
330 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
holidiess 2008-11-05
  • 打赏
  • 举报
回复
晚了
holidiess 2008-11-05
  • 打赏
  • 举报
回复
有100块石头,1只大象一次能扛19块,1只老虎一次能扛12块,4只松鼠一起一次能扛一块。有15只动物一次就将这
100块石头扛完了,请编程求出这三种动物每种多少只?

for(int 松鼠=0; 松鼠<16; 松鼠+=4)
{
for(int 大象=0; 大象<15-松鼠;大象++)
{
老虎 = 15 - 松鼠 - 大象;
if(松鼠 / 4 + 大象 * 19 + 老虎 * 12>100)
break;
else if(松鼠 / 4 + 大象 * 19 + 老虎 * 12==100)
{
printf("松鼠=%d, 大象=%d, 老虎=%d", 松鼠, 大象, 老虎);
break;
}
}
}
looseing 2008-11-04
  • 打赏
  • 举报
回复
#include "stdio.h"
void main()
{
int i,j,l;
for (i=1;i<=(100/19);i++)
{for(j=1;j<=(100-19*i)/12;j++)
{l=15-i-j;
if(l%4==0&&(19*i+12*j+l/4==100))
printf("大象=%d,老虎=%d,松鼠=%d.\n",i,j,l);

}

}
}
azhen1986 2008-11-04
  • 打赏
  • 举报
回复
第一个是字符串合并的嘛 func 被定义为什么 就##后是什么的
xxgamexx 2008-11-04
  • 打赏
  • 举报
回复
MARK
maying_11 2008-11-04
  • 打赏
  • 举报
回复
学习
casperlong 2008-11-04
  • 打赏
  • 举报
回复
你的第二题他们都没给你写代码,没关系,我给你写了一个,你应该看得懂。
#include<stdio.h>

main()
{
int e,ti,m;/*e is elephant,ti is tiger,m is mouse.*/

for(e=0;e<=5;e++)/*e<=5 ,because 100/19>5*/
{
for(ti=0;ti<=9;ti++)/*ti<=9,becase 100/12<9*/
{
m=15-e-ti;

if(e*19+ti*12+m/4==100 && m%4==0 )
{
printf("Elephants are %d,Tigers are %d,mice are %d",e,ti,m);
}
}
}
}
LiTuX 2008-11-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 tfnadnt 的帖子:]
1. #define MIXFUNC(func) do ## func()

在预编译时展开的代码行是什么?(这个貌似在C里没见过)

2.有100块石头,1只大象一次能扛19块,1只老虎一次能扛12块,4只松鼠一起一次能扛一块。有15只动物一次就将这
100块石头扛完了,请编程求出这三种动物每种多少只?
[/Quote]
要是这个没在C中见过,那么您在什么中见过??

2.经典不定方程,传统做法是辗转相除,然后再逆回去。(辗转相除,就是所谓的欧几里得辗转相除,主要用于求最大公约数等)
hyyuanqiang 2008-11-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 tfnadnt 的帖子:]
1. #define MIXFUNC(func) do ## func()

在预编译时展开的代码行是什么?(这个貌似在C里没见过)

2.有100块石头,1只大象一次能扛19块,1只老虎一次能扛12块,4只松鼠一起一次能扛一块。有15只动物一次就将这
100块石头扛完了,请编程求出这三种动物每种多少只?
[/Quote]题目太容易了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
帅得不敢出门 2008-11-04
  • 打赏
  • 举报
回复
1。串连接
2。求解方程 是说刚刚好15只一起么?
19x+12y+(15-x-y)/4 = 100 && (15-x-y)%4 ==0

ivan8222 2008-11-04
  • 打赏
  • 举报
回复
2只大象
5只老虎
8只松鼠

列方程,两个或者三个循环可得结果
pingmail 2008-11-04
  • 打赏
  • 举报
回复
2楼解释的很明白了,其实就是字符串合并
iamliadai 2008-11-04
  • 打赏
  • 举报
回复
串链接
递归
tfnadnt 2008-11-04
  • 打赏
  • 举报
回复
还是不懂
能详细点吗
glacier3d 2008-11-04
  • 打赏
  • 举报
回复
第一题举例:
MIXFUNC(myFunc) 宏替换后即:domyFunc
wuyu637 2008-11-04
  • 打赏
  • 举报
回复
2.回溯一下。
leank 2008-11-04
  • 打赏
  • 举报
回复
第二题,三个for嵌套。没考虑过效率,呵呵。
glacier3d 2008-11-04
  • 打赏
  • 举报
回复
第二题:
4元1次方程组得整数解,和白鸡问题等类似
cyj626 2008-11-04
  • 打赏
  • 举报
回复
up
没有见过
glacier3d 2008-11-04
  • 打赏
  • 举报
回复
第一题:##连接用的,msdn解释:
The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.

If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.

Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler command line. White space preceding or following the operator is optional.

This example illustrates use of both the stringizing and token-pasting operators in specifying program output:

#define paster( n ) printf( "token" #n " = %d", token##n )
int token9 = 9;
If a macro is called with a numeric argument like

paster( 9 );
the macro yields

printf( "token" "9" " = %d", token9 );
which becomes

printf( "token9 = %d", token9 );
See Also

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧