紊乱:代码审查请求 [英] derangement: code review request

查看:59
本文介绍了紊乱:代码审查请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

紊乱是一个集合到自身的映射,不会修复任何元素。我意识到这就是我在编程的时候,当我被要求随机地确定谁是b $ b来确定谁今年在我妻子的家里买了礼物。

显然,一个人不买自己。代码之后是一些

的问题。


#include< stdio.h>

#include< stdlib。 h>

#include< time.h>


#define fam_size 20

//必须介于2和randmax之间减去一个


int main(无效)

{

int i,t,a [fam_size],b [fam_size] ,notdone1,notdone2;

int m,j;

time_t计时器;

长计数器,top_num;


//确定好的随机数

srand(time(& timer));

top_num = RAND_MAX-(RAND_MAX%fam_size)-1;


//初始化数组


for(i = 0; i< fam_size; i ++){

a [i] = b [i] = 0;

}

//主要控制结构

counter = 0;

notdone1 = 1;


而(notdone1)

{

//首先忽略这个循环

//这将最终与

//任何给定的紊乱一起玩,看看它是否适合

//适合

for(j = 0; j< fam_size; j ++)

{

notdone2 = 1;

while (notdone2){


++ counter;

t = rand();

m = t%fam_size;

if((b [m] == 0)&&(m!= j)&&(t< = top_num)){

a [j] = m ;

b [m] = 1;

notdone2 = 0;

}

else {

notdone2 = 1;

}

/ *坏运气检查器

启动我们,除了柜台* /

if((j == fam_size-1)&&(b [j] == 0)){

for(i = 0; i< fam_size; i ++){

a [i] = b [i] = 0;

}

notdone2 = 0;

j = - 1;

}

//结束notdone2而

}

//结束j循环

}


for(i = 0; i< fam_size; i ++)

printf("%d%d \ n",我,a [i]);

printf(" counter =%d \ n",counter);

notdone1 = 0;

//结束外部而

}

返回0;

}


Q1)是这个代码符合ANSI标准且符合C99标准?


Q2)显而易见的样式缺点主要是我的IDE看起来产品与b / b
不同的副本/粘贴(没有Mair先生,我还没准备好被打破

off the tit)。你认为什么样的缺点不涉及

间距?


Q3)至少存在一个主要的设计缺陷。它遵循运气不好

checker的说法。并且涵盖了最终元素只能映射到

本身的事件,该事件在程序运行的每个fam_size时间发生1次。

任何想法如何制作那不那么可怕吗?


++谢谢。 MPJ

----------------------------------

* OT *我在喂食期间教过我9个月大的孩子:


我的大食者

讨厌Derek Jeter


我想我必须找到新材料:-) *结束OT *

A derangement is a mapping of a set onto itself leaving no element fixed. I
realized that that was what I was programming when I was asked to randomly
determine who buys presents for whom this year in my wife''s family.
Obviously, one does not buy for himself. The code is followed by some
questions.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define fam_size 20
//must be between 2 and randmax minus one

int main(void)
{
int i,t,a[fam_size],b[fam_size],notdone1,notdone2;
int m,j;
time_t timer;
long counter, top_num;

//determine good random numbers
srand(time(&timer));
top_num=RAND_MAX-(RAND_MAX%fam_size)-1;

//initialize arrays

for (i=0;i<fam_size;i++){
a[i]=b[i]=0;
}
//main control structures
counter=0;
notdone1=1;

while(notdone1)
{
//ignore this first while loop
//this will ultimately play with
//any given derangement to see if it''s
//suitable
for(j=0;j<fam_size;j++)

{
notdone2=1;
while(notdone2){

++counter;
t=rand();
m=t%fam_size;
if ((b[m]==0)&&(m!=j)&&(t<=top_num)){
a[j]=m;
b[m]=1;
notdone2=0;
}
else {
notdone2=1;
}
/* bad luck checker
starts us over except for counter*/
if ((j==fam_size-1)&&(b[j]==0)){
for (i=0;i<fam_size;i++){
a[i]=b[i]=0;
}
notdone2=0;
j=-1;
}
//end notdone2 while
}
//end j loop
}

for(i=0;i<fam_size;i++)
printf("%d %d\n",i,a[i]);
printf("counter= %d\n",counter);
notdone1=0;
//end outer while
}
return 0;
}

Q1) Is this code ANSI-compliant and C99-compliant?

Q2) The obvious style shortcomings are mostly a product of my IDE looking
different than what is copy/pasted (no Mr. Mair, I am not ready to be weened
off the tit). What style shortcomings do you see that don''t involve
spacing?

Q3) There''s at least one major design flaw. It follows the remark "bad luck
checker" and covers the event that the final element can only be mapped to
itself, which happens 1 out of every fam_size times that the program is run.
Any ideas how to make that less hideous?

++thanks. MPJ
----------------------------------
*OT* I had been teaching my nine-month-old during feedings:

"My big eater
hates Derek Jeter"

I guess I''ll have to find new material :-) *end OT*

推荐答案

Merrill& Michele写道:
Merrill & Michele wrote:
紊乱是一个集合到自身的映射,没有任何元素被修复。我意识到这就是我编程的时候,当我被要求随机确定谁在今年为我妻子的家人购买礼物时。
显然,一个人不买本人。代码之后是一些
问题。

[代码剪断;看看线程问题

Q1)这个代码符合ANSI标准且符合C99吗?

Q2)明显的样式缺点主要是我的IDE看起来的产品/>不同于复制/粘贴的内容(没有Mair先生,我还没准备好被关闭的山雀)。你认为什么样的缺点不涉及间距?

Q3)至少存在一个主要的设计缺陷。它遵循坏运气检查器的说法。并且涵盖了最终元素只能映射到
本身的事件,这个事件在程序运行的每个fam_size时间发生一次。
任何想法如何使它不那么可怕?
A derangement is a mapping of a set onto itself leaving no element fixed. I
realized that that was what I was programming when I was asked to randomly
determine who buys presents for whom this year in my wife''s family.
Obviously, one does not buy for himself. The code is followed by some
questions.

[code snipped; see up-thread]

Q1) Is this code ANSI-compliant and C99-compliant?

Q2) The obvious style shortcomings are mostly a product of my IDE looking
different than what is copy/pasted (no Mr. Mair, I am not ready to be weened
off the tit). What style shortcomings do you see that don''t involve
spacing?

Q3) There''s at least one major design flaw. It follows the remark "bad luck
checker" and covers the event that the final element can only be mapped to
itself, which happens 1 out of every fam_size times that the program is run.
Any ideas how to make that less hideous?




我打算尝试第一季度和第二季度,但是我仍然是因为在游戏后的7个模糊之后遭受了
而且似乎无法

唤起学习实施的热情

的劣质算法。很抱歉 - 我不是

试图决定你的算法选择,但我只是

无法保持对它的兴趣。


Q3解决了算法选择问题,因此这里的主题是
。尽管如此,让我建议一个不同的

方法:


- 只使用一个数组来指示谁为谁购买。

那是,buys_for [i] == j表示人'我'

购买人'j'';你会找到一个像这样的所有相关的我来买一个这样的布局。这就是所有相关的我。


- 用任何排列初始化数组;

buys_for [i] = i(即使它没有达到你的
目标)也没关系。


- 对于每个数组位置,如果buys_for [i] == i那么

生成一个随机j!= i并用buys_for [j]交换buys_for [i]

。这个交换[*]总是删除

a自我碰撞并且不能创建一个新的,所以一个

通过阵列消除了所有的碰撞。

[*]鉴于一些令人厌倦的讨论,这个群体过于频繁,这很有意思

请注意,这种交换可以在没有一个临时的

变量*和*没有XOR黑客或任何亲属。

你明白为什么吗?


- -

Er*********@sun.com



I was going to attempt Q1 and Q2, but I''m still
suffering from post-Game-7 fogginess and can''t seem to
summon any enthusiasm for studying the implementation
of an inferior algorithm. Sorry about that -- I''m not
trying to dictate your algorithm choice, but I''m just
unable to maintain interest in it.

Q3 addresses the algorithm selection, and is thus
off-topic here. Nonetheless, let me suggest a different
approach:

- Use just one array indicating who buys for whom.
That is, buys_for[i] == j means that person `i''
buys for person `j''; you seek a permuation such
that buys_for[i] != i for all relevant `i''.

- Initialize the array with any permutation at all;
buys_for[i] = i (even though it doesn''t meet your
goal) is fine.

- For each array position, if buys_for[i] == i then
generate a random j != i and exchange buys_for[i]
with buys_for[j]. This exchange[*] always removes
a self-collision and cannot create a new one, so one
pass over the array eliminates all collisions.
[*] In light of some tiresome discussions that crop
up on this group all too frequently, it''s interesting to
note that this exchange can be done without a temporary
variable *and* without the XOR hack or any of its kin.
Do you see why?

--
Er*********@sun.com


[新手 - 只有一个答案]


美林& Michele写道:

< snip>
[newbie -- just one answer]

Merrill & Michele wrote:
<snip>
#include< stdio.h>
#include< stdlib.h>
#include< ; time.h>

#define fam_size 20
//必须介于2和randmax之间减去一个

int main(void)
{
int i,t,a [fam_size],b [fam_size],notdone1,notdone2;
int m,j;
time_t timer;
long counter,top_num;
^^^^


< snip>

printf(" counter =%d \ n",counter);


计数器很长,它需要%ld格式为printf()

notdone1 = 0;
//结束外部而
}
返回0;
}
Q1)此代码符合ANSI标准且符合C99吗?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define fam_size 20
//must be between 2 and randmax minus one

int main(void)
{
int i,t,a[fam_size],b[fam_size],notdone1,notdone2;
int m,j;
time_t timer;
long counter, top_num; ^^^^

<snip>
printf("counter= %d\n",counter);
counter is a long, it needs %ld in the format for printf()
notdone1=0;
//end outer while
}
return 0;
}

Q1) Is this code ANSI-compliant and C99-compliant?




AFAICT //评论与ANSI不兼容


-

如果每个人都阅读,USENET会是一个更好的地方:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb .org / ~esr / faqs / smart-questions.html



AFAICT // comments aren''t ANSI-compatible

--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html


Merrill& ; Michele写道:
紊乱是一个集合到自身的映射,没有修复元素
。我意识到这就是我编程的时候我被要求
随机确定谁在今年为我妻子的家人买了礼物。
显然,一个人不买自己。代码之后是一些
问题。
Merrill & Michele wrote:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this year in my wife''s family.
Obviously, one does not buy for himself. The code is followed by some
questions.



[snip]" Eric Sosman"写道:
我打算尝试第一季度和第二季度,但是我仍然会受到游戏后的迷茫之后的困惑,似乎无法激发任何学习热情的热情。实现劣质算法。很抱歉 - 我不是在试图决定你的算法选择,但我只是无法保持对它的兴趣。


算法是否足够并且比一分钱便宜?我会

承认它不够优雅。

Q3解决了算法的选择问题,因此在这里是偏离主题的。尽管如此,让我建议一个不同的方法:

- 只使用一个数组来指示谁为谁购买。
也就是说,buys_for [i] == j意味着那个人我'
为'j'人购买;你会寻找一个像所有相关的我那样购买的错误。

- 用任何排列初始化数组;
buys_for [i ] = i(即使它没有达到你的目标)也没关系。

- 对于每个数组位置,如果buys_for [i] ==我那么
生成随机j!= i并用buys_for [j]交换buys_for [i]
。这个交换[*]总是消除了自我碰撞并且不能创建一个新的碰撞,所以一个通过阵列消除所有碰撞。

[*]鉴于一些令人讨厌的讨论过于频繁地关注这个群体,有趣的是
请注意,这种交换可以在没有临时变量的情况下完成*和*没有XOR黑客或任何一个亲属。
你明白为什么吗?


[snip] "Eric Sosman" wrote:
I was going to attempt Q1 and Q2, but I''m still
suffering from post-Game-7 fogginess and can''t seem to
summon any enthusiasm for studying the implementation
of an inferior algorithm. Sorry about that -- I''m not
trying to dictate your algorithm choice, but I''m just
unable to maintain interest in it.
Is the algorithm sufficient and less expensive than a penny to run? I''ll
concede that it''s less than elegant.
Q3 addresses the algorithm selection, and is thus
off-topic here. Nonetheless, let me suggest a different
approach:

- Use just one array indicating who buys for whom.
That is, buys_for[i] == j means that person `i''
buys for person `j''; you seek a permuation such
that buys_for[i] != i for all relevant `i''.

- Initialize the array with any permutation at all;
buys_for[i] = i (even though it doesn''t meet your
goal) is fine.

- For each array position, if buys_for[i] == i then
generate a random j != i and exchange buys_for[i]
with buys_for[j]. This exchange[*] always removes
a self-collision and cannot create a new one, so one
pass over the array eliminates all collisions.

[*] In light of some tiresome discussions that crop
up on this group all too frequently, it''s interesting to
note that this exchange can be done without a temporary
variable *and* without the XOR hack or any of its kin.
Do you see why?




感谢您的关注。 我为什么不在那里。我试着和一个小家庭一起写了一张纸,但是我无法通过我的报道来了解这个怎么回事。

让我远离最后没有一个奇怪的人。我将需要

时间来实际编码。 MPJ



Thank you for your attention. I''m not quite there on ''why.'' I tried it on
paper with a small family but couldn''t get past my blinder about how this
keeps me away from not having an odd man out at the end. I''m going to need
time to actually code this. MPJ


这篇关于紊乱:代码审查请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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