By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

is this style good ?

P: n/a

code is from Warsaw university's CEPC code .
They are world champion in the ICPC finals.

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,x) __typeof(x) v=x
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)

template<class T>
inline int size(const T&a) { return a.size(); }

////////

const int NDIGS=40;
const int BASE = 100000000;
const int BASEDIGS = 8;

class Num {
int a[NDIGS];
void popraw() {
int p=0;
FORD(i,NDIGS-1,0) {
a[i]+=p;
p=a[i]/BASE;
a[i]%=BASE;
}
}
public:
void set0() { REP(i,NDIGS) a[i]=0; }
void set1() { REP(i,NDIGS) a[i]=0; a[0]=1; }
void dodaj(const Num&x,const Num&y) {
REP(i,NDIGS) a[i]=x.a[i]+y.a[i];
popraw();
}
void div2() {
int p=0;
REP(i,NDIGS) {
a[i]+=p;
p=(a[i]&1)==0?0:BASE;
a[i]>>=1;
}
}
/* void dodajIloczyn(const Num&x,const Num&y) {
REP(i,NDIGS) REP(j,NDIGS-i)
a[i+j] += x.a[i]*y.a[j];
popraw();
} */
void wypisz() {
int res[BASEDIGS*(NDIGS-1)+1];
int p=BASEDIGS*(NDIGS-1)+1;
FORD(i,NDIGS-1,1) {
int x=a[i];
REP(j,BASEDIGS) { res[--p]=x%10; x/=10; }
}
res[0]=a[0];
int aa=0,bb=(NDIGS-1)*BASEDIGS;
while(aa<2 && res[aa]==0) ++aa;
while(bb>2 && res[bb]==0) --bb;
FOR(i,aa,bb) {
putchar('0'+res[i]);
if(i==2 && i<bb) putchar('.');
}
printf("%%\n");
}
};
const int MAXN = 301;

int n;
int par[MAXN][2];
bool done[MAXN][MAXN];
Num pr[MAXN][MAXN];
int dnr[MAXN];
int ddd;

void dfs(int x) {
if(dnr[x]!=-1) return;
if(par[x][0]!=-1) REP(i,2) dfs(par[x][i]);
dnr[x]=ddd++;
}

void read() {
scanf("%d",&n);
REP(i,n) par[i][0]=par[i][1]=-1;
int k; scanf("%d",&k);
REP(i,k) {
int a,b,c; scanf("%d%d%d",&a,&b,&c); --a; --b; --c;
par[a][0]=b;
par[a][1]=c;
}
}

void calc(int a,int b) {
if(done[a][b]) return;
if(a==b) { pr[a][b].set1(); done[a][b]=true; return; }
if(dnr[a]<dnr[b]) swap(a,b);
if(par[a][0]==-1) swap(a,b);
if(par[a][0]==-1) {
pr[a][b].set0();
}
else {
calc(par[a][0],b);
calc(par[a][1],b);
pr[a][b].dodaj(pr[par[a][0]][b],pr[par[a][1]][b]);
pr[a][b].div2();
}
pr[b][a]=pr[a][b];
done[a][b]=done[b][a]=true;
}

int main() {
read();
REP(i,n) dnr[i]=-1;
ddd=0;
REP(i,n) dfs(i);
REP(a,n) REP(b,n) done[a][b]=false;
int m; scanf("%d",&m);
REP(i,m) {
int a,b;
scanf("%d%d",&a,&b);
--a; --b;
calc(a,b);
pr[a][b].wypisz();
}
}

Nov 27 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
* blackswift:

code is from Warsaw university's CEPC code .
They are world champion in the ICPC finals.
This style is very ungood for code that is to be maintained.

For maintenable code macros are evil, language-extensions are evil,
cryptic names are evil, global variables are evil, and so on.

But judging the style in a context different than the original is
probably not meaningful.

If this is write-only code where the goal is to write the code fastest
possible, any measure that reduces the number of letters typed can be
helpful.

Of course the programmer then needs to be able to keep it all in the
head, but presumably the programmer was able to do just that: it is,
after all, a small program.

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,x) __typeof(x) v=x
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)

template<class T>
inline int size(const T&a) { return a.size(); }

////////

const int NDIGS=40;
const int BASE = 100000000;
const int BASEDIGS = 8;

class Num {
int a[NDIGS];
void popraw() {
int p=0;
FORD(i,NDIGS-1,0) {
a[i]+=p;
p=a[i]/BASE;
a[i]%=BASE;
}
}
public:
void set0() { REP(i,NDIGS) a[i]=0; }
void set1() { REP(i,NDIGS) a[i]=0; a[0]=1; }
void dodaj(const Num&x,const Num&y) {
REP(i,NDIGS) a[i]=x.a[i]+y.a[i];
popraw();
}
void div2() {
int p=0;
REP(i,NDIGS) {
a[i]+=p;
p=(a[i]&1)==0?0:BASE;
a[i]>>=1;
}
}
/* void dodajIloczyn(const Num&x,const Num&y) {
REP(i,NDIGS) REP(j,NDIGS-i)
a[i+j] += x.a[i]*y.a[j];
popraw();
} */
void wypisz() {
int res[BASEDIGS*(NDIGS-1)+1];
int p=BASEDIGS*(NDIGS-1)+1;
FORD(i,NDIGS-1,1) {
int x=a[i];
REP(j,BASEDIGS) { res[--p]=x%10; x/=10; }
}
res[0]=a[0];
int aa=0,bb=(NDIGS-1)*BASEDIGS;
while(aa<2 && res[aa]==0) ++aa;
while(bb>2 && res[bb]==0) --bb;
FOR(i,aa,bb) {
putchar('0'+res[i]);
if(i==2 && i<bb) putchar('.');
}
printf("%%\n");
}
};
const int MAXN = 301;

int n;
int par[MAXN][2];
bool done[MAXN][MAXN];
Num pr[MAXN][MAXN];
int dnr[MAXN];
int ddd;

void dfs(int x) {
if(dnr[x]!=-1) return;
if(par[x][0]!=-1) REP(i,2) dfs(par[x][i]);
dnr[x]=ddd++;
}

void read() {
scanf("%d",&n);
REP(i,n) par[i][0]=par[i][1]=-1;
int k; scanf("%d",&k);
REP(i,k) {
int a,b,c; scanf("%d%d%d",&a,&b,&c); --a; --b; --c;
par[a][0]=b;
par[a][1]=c;
}
}

void calc(int a,int b) {
if(done[a][b]) return;
if(a==b) { pr[a][b].set1(); done[a][b]=true; return; }
if(dnr[a]<dnr[b]) swap(a,b);
if(par[a][0]==-1) swap(a,b);
if(par[a][0]==-1) {
pr[a][b].set0();
}
else {
calc(par[a][0],b);
calc(par[a][1],b);
pr[a][b].dodaj(pr[par[a][0]][b],pr[par[a][1]][b]);
pr[a][b].div2();
}
pr[b][a]=pr[a][b];
done[a][b]=done[b][a]=true;
}

int main() {
read();
REP(i,n) dnr[i]=-1;
ddd=0;
REP(i,n) dfs(i);
REP(a,n) REP(b,n) done[a][b]=false;
int m; scanf("%d",&m);
REP(i,m) {
int a,b;
scanf("%d%d",&a,&b);
--a; --b;
calc(a,b);
pr[a][b].wypisz();
}
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 27 '05 #2

P: n/a

Alf P. Steinbach wrote in message
<43*****************@news.individual.net>...
* blackswift:
code is from Warsaw university's CEPC code .
They are world champion in the ICPC finals.

Was Hitler still in power when that code was written?

<huh?>
CEPC - Childish Error Producing Code.
ICPC - ( I ? Irritating : Irrational ) Coding Practices Contest.
</huh?>

For maintenable code macros are evil, language-extensions are evil,
cryptic names are evil, global variables are evil, and so on.


Hi Alf,

Flat-out makes my eyes bleed!

If I get time, I may compile it with the g++ -save_temps flag, just to see
what it really looks like. :-}
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come from?
(? declaration/definition ?)

--
Bob R
POVrookie
Nov 27 '05 #3

P: n/a

"BobR" <Re***********@worldnet.att.net> wrote in message
news:wF*******************@bgtnsc05-news.ops.worldnet.att.net...

Alf P. Steinbach wrote in message
<43*****************@news.individual.net>...
* blackswift:
code is from Warsaw university's CEPC code .
They are world champion in the ICPC finals.

Was Hitler still in power when that code was written?

<huh?>
CEPC - Childish Error Producing Code.
ICPC - ( I ? Irritating : Irrational ) Coding Practices Contest.
</huh?>

For maintenable code macros are evil, language-extensions are evil,
cryptic names are evil, global variables are evil, and so on.


Hi Alf,

Flat-out makes my eyes bleed!


Makes mine itch. :-)

If I get time, I may compile it with the g++ -save_temps flag, just to see
what it really looks like. :-}
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come from?
(? declaration/definition ?)


It's created in the expansions of macros FOR, FORD, and REP

-Mike
Nov 27 '05 #4

P: n/a

BobR wrote:
Alf P. Steinbach wrote in message
<43*****************@news.individual.net>...
* blackswift:
code is from Warsaw university's CEPC code .
They are world champion in the ICPC finals.


Was Hitler still in power when that code was written?


I think you just ended this thread pre-maturely... /david

Nov 27 '05 #5

P: n/a

Mike Wahler wrote in message

"BobR" wrote in message
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come from?
(? declaration/definition ?)


It's created in the expansions of macros FOR, FORD, and REP
-Mike


Hi Mike,

Ah, I see now, it's only used in the bodies after the macro(s). Thanks Mike.

Also noticed that 'FOR' is only used once. That seems like bad programming
practice to me (aside from using macros (like those) to begin with <G>). No
gain, adds clutter!

--
Bob R
POVrookie
Nov 27 '05 #6

P: n/a

da********@warpmail.net wrote in message

BobR wrote:
>* blackswift:
>> code is from Warsaw university's CEPC code .
>> They are world champion in the ICPC finals.


Was Hitler still in power when that code was written?


I think you just ended this thread pre-maturely... /david


It was NOT a statement, it was a question. And the connection was 'ugly
code', 'ugly war'.
[ The code IMHO looked like something the 'SS' would force upon the world. ].

Sorry you took offense.
--
Bob R
POVrookie
Nov 28 '05 #7

P: n/a
No, I didn't take offence. I was referring to Godwin's Law:
http://en.wikipedia.org/wiki/Godwin's_law

:-) /david

Nov 28 '05 #8

P: n/a
No, I didn't take offence. I was referring to Godwin's Law:
http://en.wikipedia.org/wiki/Godwin's_law

:-) /david

Nov 28 '05 #9

P: n/a

da********@warpmail.net wrote in message
<11*********************@g43g2000cwa.googlegroups. com>...
No, I didn't take offence. I was referring to Godwin's Law:
http://en.wikipedia.org/wiki/Godwin's_law

:-) /david


Interesting.

"Morgan's corollary to Godwin's Law
As soon as such a comparison occurs, someone will start a Nazi-discussion
thread on alt.censorship."

I don't subscribe to alt.censorship, so, would you be so kind as to keep me
informed? <G>

If I had used Nixon in reference to the time period, would I have still
invoked the wrath of Godwin? Does asking that question invoke Godwin's law
again? What questions are we allowed to ask?
Thanks for the link, glad your not mad.

OOOoohh NNOOooooo!
"Guy's corollary
If a Usenet discussion mentions Godwin's law as a conterrebuttal to a mention
of Hitler/Nazis, then the probability of Godwin's law being disputed is equal
to 1.
"
So, if we code:
while( Is_Godwins_law ){
}
....we're instantly in an endless loop?
BTW - "offence" is spelled "offense", I had looked it up. :-}
--
Bob R
POVrookie

Nov 28 '05 #10

P: n/a

BobR wrote:

[snip]
BTW - "offence" is spelled "offense", I had looked it up. :-}


Well, the difference between 'c' and 's' is one bit, so it's about the
same. /david

Nov 28 '05 #11

P: n/a
CEPC stands for Central Europe Programming Contest
http://icpc.cs.bme.hu/main.php?p=8

ICPC stands for ACM International Collegiate Programming Contest
http://icpc.baylor.edu/icpc/

Nov 28 '05 #12

P: n/a
BobR wrote:
BTW - "offence" is spelled "offense", I had looked it up. :-}


Ah... the US and the UK. Two countries separated by a common language.

Nov 28 '05 #13

P: n/a

"BobR" <Re***********@worldnet.att.net> wrote in message
news:Wb*******************@bgtnsc05-news.ops.worldnet.att.net...

BTW - "offence" is spelled "offense", I had looked it up. :-}


Both are 'correct', which spelling is used usually depends upon
which side of the pond you're on.

-Mike
Nov 28 '05 #14

P: n/a

"BobR" <Re***********@worldnet.att.net> wrote in message
news:CC*******************@bgtnsc05-news.ops.worldnet.att.net...

Mike Wahler wrote in message

"BobR" wrote in message
[ I am anti-macro ever since I got a 'macro Assembler'(1980).]

Where the heck does the 'i' variable, used throughout the code, come
from?
(? declaration/definition ?)


It's created in the expansions of macros FOR, FORD, and REP
-Mike


Hi Mike,

Ah, I see now, it's only used in the bodies after the macro(s). Thanks
Mike.

Also noticed that 'FOR' is only used once. That seems like bad programming
practice to me (aside from using macros (like those) to begin with <G>).
No
gain, adds clutter!

--


Not to mention the problem of attempting to nest those loops. What is "i"
referring to then???

for (int i = ...
for (int i = ...
a[i] = ...

If it's too much trouble to type in your own loop control statements, then
hire someone else to do it for you! :-)

-Howard
Nov 28 '05 #15

P: n/a

Mike Wahler wrote in message ...

"BobR" <Re***********@worldnet.att.net> wrote in message
news:Wb*******************@bgtnsc05-news.ops.worldnet.att.net...

BTW - "offence" is spelled "offense", I had looked it up. :-}


Both are 'correct', which spelling is used usually depends upon
which side of the pond you're on.

-Mike


Oh great! So now we have to write:

"offen( cp==437 ? s : c )e" // <G> [1]

Thanks for the heads-up, Mike.

My apology to david. Next time don't hide yer Englich accent, eh. :-}

[1] - and that only covers the old dos/window$ codepage (?). Is that covered
in the i18n specs?
--
Bob R
POVrookie
Nov 28 '05 #16

P: n/a
> #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,x) __typeof(x) v=x
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)


I really appreciate this style!
Nov 28 '05 #17

P: n/a
Elcaro Nosille <El************@xxx.invalid> wrote:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,x) __typeof(x) v=x
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)


I really appreciate this style!


It reminds me of

#define BEGIN {
#define END }

--
Marcus Kwok
Nov 28 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.