全球前十强,年薪15万的面试题

kary 2007-08-08 08:08:06
程序风格的作用主要是使代码容易阅读。代码应该时清楚的和简单的-------具有直截了当的逻辑、自然的表达式、通行的语言使用方式、有意义的名字和有帮助作用的注释等,应该避免耍小聪明,绝不使用非正规的结构。
下面的代码具有很多缺陷,请从风格的角度评论并改正下面各个代码片断:
/* return SUCCESS */
return SUCCESS;

----------------------------------------------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id >= unblocks))

-----------------------------------------------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}

-------------------------------------------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;

----------------------计算n的阶乘---------------------------------------------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= n;
return fac;
}

-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)

-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a >b? a:b
...全文
1686 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
kary 2007-08-18
  • 打赏
  • 举报
回复
结贴了。
如果把前面每个人的答案综合一下,这题目就答圆满了。
所以答案不再给了,呵呵。
qzs19840104 2007-08-13
  • 打赏
  • 举报
回复

return SUCCESS;

----------------------------------------------------------------------------------------
if ((block_id >= actblks) && (block_id < unblocks))

------------------Java语言-----------------------------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) equals nametable.userName()) {
return true;
}
}

----------------------初始化一个数组,数组大小为n---------------------------------------------------------------------
int i= 0;
while(i <= (n-1))
array[i++] = 1.0;

----------------------计算n的阶乘---------------------------------------------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while ((n-1) > 0){
fac *= n;
n--;
}
return fac;
}

----------------------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i , j);
else if( i < j)
printf(“%d is smaller than %d.\n”, i, j);

-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) (a) >(b)? (a):(b)
真的很小奚 2007-08-13
  • 打赏
  • 举报
回复
15W很高吗还十强呢
SBtoSB 2007-08-13
  • 打赏
  • 举报
回复
15万就把你吹的?SB
fish_kun 2007-08-13
  • 打赏
  • 举报
回复
if ((block_id >= actblks) || (block_id < unblocks))




Cat_Of_9Life 2007-08-13
  • 打赏
  • 举报
回复
/* return SUCCESS */
return SUCCESS; 不需要注释

----------------------------------------------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id >= unblocks))

-----------------------------------------------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
应该把==换成equals
-------------------------------------------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;
用for循环简单明了
----------------------计算n的阶乘---------------------------------------------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= n; 循环用的不好,for循环即可
return fac; 没有考虑数据溢出问题, 应 该使用数组或者字符串存储阶乘的结果
}

-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j) //没有考虑相等

-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a >b? a:b //a,b可以为表达式,且结果外再加个括号,否则用在表
//达式可能会出错,#define MAX(a,b) ((a)>(b)?(a):(b))
fish_kun 2007-08-13
  • 打赏
  • 举报
回复
return SUCCESS;

----------------------------------------------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id >= unblocks))
错误语法

-----------------------------------------------------------------------------------------
for ( int i = 0; i<total; i ++ )
if (this.getName(i).equals(nametable.userName())) {
return true;
}
}

-------------------------------------------------------------------------------------------
i= 0;
while(i < n)
array[i++] = 1.0;

----------------------计算n的阶乘---------------------------------------------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= ++fac;
return fac;
}

-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j);
else if ( i < j)
printf(“%d is smaller than %d.\n”, i, j);
else
printf(“%d is equal with %d.\n”, i, j);


-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) (a) >(b)? a:b
kary 2007-08-11
  • 打赏
  • 举报
回复
没答出来的,答出来的,都回答下面的问题好吗?
1)做了几年了
2)最熟悉的是什么语言和平台
3)花费了多少时间做这道题目
4)自认为技术能力怎么样(不是指根据题目)
kary 2007-08-10
  • 打赏
  • 举报
回复
格式有点乱了,重新贴一下:

/* return SUCCESS */
return SUCCESS;

----------------------------------------------------------------------------------------
if (!(block_id < actblks) || !(block_id >= unblocks))

------------------Java语言-----------------------------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}

----------------------初始化一个数组,数组大小为n---------------------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;

----------------------计算n的阶乘---------------------------------------------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n-- > 0)
fac *= n;
return fac;
}

----------------------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j);
else
printf(“%d is smaller than %d.\n”, i, j);

-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a >b? a:b
kary 2007-08-09
  • 打赏
  • 举报
回复
mail_ricklee(NickLee):
不好意思,完全不对。
lauxp 2007-08-09
  • 打赏
  • 举报
回复
printf("%d is %s than %d", i, (i>j)? "greater":"smaller", j);
siwen299 2007-08-09
  • 打赏
  • 举报
回复
刚建立了一个软件开发技术和休闲交流的QQ群44540509,java,.net,c/c++,php,linux,unix、软件测试等技术、休闲讨论区,欢迎大家加入!
lauxp 2007-08-09
  • 打赏
  • 举报
回复
printf("%d is %s than %d", (i>j)? "greater":"smaller");
ochinchina 2007-08-09
  • 打赏
  • 举报
回复
/* return SUCCESS */
return SUCCESS;
这个注释是多余的
----------------------------------------------------------------------------------------
if (!(block_id < actblks) ¦ ¦ !(block_id >= unblocks))

if没有这种格式
-----------------------------------------------------------------------------------------
int count = 0;
while (count < total) {
count++;
if (this.getName(count) == nametable.userName()) {
return (true);
}
}
在Java中比较相等应该用equals()方法
-------------------------------------------------------------------------------------------
i= 0;
while(i <= n-1)
array[i++] = 1.0;

应该用Arrays.fill()
----------------------计算n的阶乘---------------------------------------------------------------------
int factorial(int n)
{
int fac;
fac = 1;
while (n--)
fac *= n;
return fac;
}

错误,少乘一个n,并且Java中应该明确判断n是否大于零,如:

while( n > 0 ) {
fac *= n --;
}

-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)

在printf(...)后缺了;
-------------------------------------- C语言--------------------------------------------
#define MAX(a, b) a >b? a:b

最好如下定义:

#define MAX(a,b) ((a) > (b))?a:b

并且在C中用宏造成错误的可能性较大,尽量不用
dishpzga 2007-08-09
  • 打赏
  • 举报
回复
不好意思我错了 是const
dishpzga 2007-08-09
  • 打赏
  • 举报
回复
在Java中允许使用count做变量吗
mail_ricklee 2007-08-09
  • 打赏
  • 举报
回复
kary给打个分,我的格式
mail_ricklee 2007-08-09
  • 打赏
  • 举报
回复
/*
*return SUCCESS
*/
return SUCCESS;

----------------------------------------------------------------------------------------
if (
!(block_id < actblks)
&
brvbar;
&
brvbar; !(block_id >= unblocks)
)

-----------------------------------------------------------------------------------------
int count = 0;
while (count < total)
{
count++;
if (this.getName(count) == nametable.userName())
{
return true;
}
}

-------------------------------------------------------------------------------------------
i=0;
while(i <= (n-1))
{
i++
array[i] = 1.0;
}

----------------------计算n的阶乘---------------------------------------------------------------------
int factorial(int n)
{
int fac = 1;
while(n--)
{
fac *= n;
}
return fac;
}

-----------C函数,但是Java开发人员也可以做-----------------------------------------
if (i > j)
{
printf(“%d is greater than %d.\n”, i, j) ;
}
else
{
printf(“%d is smaller than %d.\n”, i, j) ;
}

-------------------------------------- C语言--------------------------------------------
#define MAX((a),(b)) (a)>(b)? (a):(b);
justinavril 2007-08-09
  • 打赏
  • 举报
回复
第二题,最好不要用那个三元的表达式,不好懂~~
kaukiyou 2007-08-09
  • 打赏
  • 举报
回复
明显逻辑错误,不是大于就是小于,那等于呢。。。



-----------C函数,但是Java开发人员也可以做---------------------------------------
if (i > j)
printf(“%d is greater than %d.\n”, i, j)
else
printf(“%d is smaller than %d.\n”, i, j)
后面少分号了:
if (i > j)
printf(“%d is greater than %d.\n”, i, j);
else
printf(“%d is smaller than %d.\n”, i, j);
-------------------------------------- C语言--------------------------------------
加载更多回复(5)

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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