一个有趣的C问题 [英] An interesting C problem

查看:64
本文介绍了一个有趣的C问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我对一家公司进行了笔试,有一个有趣的问题。它如下:

用输入参数i和n编写一个函数,然后只用一行

语句来生成格式化结果如下: />
i

i + 1

....

n

n-1

....

i


例如,让i = 1,n = 4,那么输出应为:

1

2

3

4

3

2

1


最棘手的是你应该只使用一个声明来实现这个目标。

问题是10分,如果你使用一个循环,4分将是

minused,如果你使用另一个声明,2分将被删除。

任何人都可以在不丢失任何积分的情况下弄清楚它是否可行?


谢谢,

解决方案

< gt ******* @ gmail.comha scritto nel messaggio

news:11 ************* *********@p15g2000hsd.googlegr oups.com ...


>前几天我对一家公司进行了笔试,有一个有趣的问题。它如下:

用输入参数i和n编写一个函数,然后只用一行

语句来生成格式化结果如下: />
i

i + 1

...

n

n-1
...

i


例如,让i = 1,n = 4,那么输出应为:

1

2

3

4

3

2

1


最棘手的是你应该只使用一个声明来实现这个目标。

问题是10分,如果你使用一个循环,4分将是

minused,如果你使用另一个声明,2分将被删除。


任何人都可以在不丢失任何积分的情况下弄清楚它是否可行吗?


谢谢,



#include < stdio.h>

void idiotic_thing(int i,int n);

void idiotic_thing(int i,int n)

{

idiotic_thing1(i,n) ;

}


void idiotic_thing1(int i,int n)

{

int j ;

if(i< n)返回;

for(j = i; j< = n; j ++)

printf("%d \ n",j);

for(j = n-1; j> = i; j ++)

printf("%d \ n",j);

返回;

}


函数idiotic_thing本身只使用一个语句而没有循环。


< blockquote> Army1987写道:


< gt ******* @ gmail.comha scritto nel messaggio

news:11 * *********************@p15g2000hsd.googlegr oups.com ...


我有一个前几天公司的笔试,有一个有趣的问题。它如下:

用输入参数i和n编写一个函数,然后只用一行

语句来生成格式化结果如下: />
i

i + 1

...

n

n-1
...

i


例如,让i = 1,n = 4,那么输出应为:

1

2

3

4

3

2

1


最棘手的是你应该只使用一个声明来实现这个目标。

问题是10分,如果你使用一个循环,4分将是

minused,如果你使用另一个声明,2分将被删除。


任何人都可以知道它是否可行而不会丢失任何积分?


谢谢,



#include < stdio.h>

void idiotic_thing(int i,int n);



ITYM,void idiotic_thing1(int i,int n);


void idiotic_thing(int i, int n)

{

idiotic_thing1(i,n);

}


void idiotic_thing1 (int i,int n)

{

int j;

if(i< n)return;



这个检查压缩了这个例程的目的。


for(j = i; j< = n; j ++)

printf("%d \ n",j);

for(j = n-1; j> = i; j ++ )

printf("%d \ nn",j);

返回;

}


函数idiotic_thing本身只使用一个语句而没有循环。


gt ******* @ gmail .com 说:


最重要的是你应该只使用一个声明来实现这个目标。
>
问题是10分,如果你使用一个循环,4分将是

minused,如果你使用另一个循环,2分将被删除。


任何人都可以在不丢失任何积分的情况下弄清楚它是否可行?



一个循环是一个迭代语句,它是由语法定义的几种不同的b / b $ b种语句之一,所以我看到了没有理由为什么它b / b
应该失去任何积分。当你的提问者可以产生这个:


*

* *

* *

* *

* *

* *

*


在单一的无体迭代语句中(其中)

钻石的高度可以在运行时配置),我会考虑考虑他的问题。在那之前,告诉他他已经落后于曲线。


-

理查德希思菲尔德

" Usenet is一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


I had a written test of a company the other day, there''s an
interesting problem. it''s as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
....
n
n-1
....
i

For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it''s feasible without loosing any points?

Thanks,

解决方案

<gt*******@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...

>I had a written test of a company the other day, there''s an
interesting problem. it''s as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
...
n
n-1
...
i

For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it''s feasible without loosing any points?

Thanks,

#include <stdio.h>
void idiotic_thing(int i, int n);
void idiotic_thing(int i, int n)
{
idiotic_thing1(i, n);
}

void idiotic_thing1(int i, int n)
{
int j;
if (i < n) return;
for (j = i; j <= n; j++)
printf("%d\n", j);
for (j = n-1; j >= i; j++)
printf("%d\n", j);
return;
}

The function idiotic_thing itself uses only one statement and no loop.


Army1987 wrote:

<gt*******@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...

I had a written test of a company the other day, there''s an
interesting problem. it''s as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
...
n
n-1
...
i

For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it''s feasible without loosing any points?

Thanks,


#include <stdio.h>
void idiotic_thing(int i, int n);

ITYM, void idiotic_thing1(int i, int n);

void idiotic_thing(int i, int n)
{
idiotic_thing1(i, n);
}

void idiotic_thing1(int i, int n)
{
int j;
if (i < n) return;

This check squashes the purpose of this routine.

for (j = i; j <= n; j++)
printf("%d\n", j);
for (j = n-1; j >= i; j++)
printf("%d\n", j);
return;
}

The function idiotic_thing itself uses only one statement and no loop.


gt*******@gmail.com said:

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it''s feasible without loosing any points?

A loop is an iteration-statement, which is one of the several different
kinds of statement defined by the grammar, so I see no reason why it
should lose any points. When your questioner can produce this:

*
* *
* *
* *
* *
* *
*

in a single bodyless iteration-statement (where the height of the
diamond is configurable at runtime), I''ll think about thinking about
his problem. Until then, just tell him he''s behind the curve.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于一个有趣的C问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆