一个pascal程序,求转 c/c++,c#,java,VB,javascript,php,等等的计算机语言 10
{$IFDEFNORMAL}{$I-,OBJECTCHECKS-,Q-,R-,S-}{$ENDIFNORMAL}{$IFDEFDEBUG}{$I+,OBJECTCHECK...
{$IFDEF NORMAL}
{$I-,OBJECTCHECKS-,Q-,R-,S-}
{$ENDIF NORMAL}
{$IFDEF DEBUG}
{$I+,OBJECTCHECKS-,Q+,R+,S-}
{$ENDIF DEBUG}
{$IFDEF RELEASE}
{$I-,OBJECTCHECKS-,Q-,R-,S-}
{$ENDIF RELEASE}
program n_dice_distribution;
uses crt;
const max_dice = 6; //max dice number
max_n = 10; //max throwing dices times
f_max_size = max_dice * max_n; //max size of f
tab_sp = 10; //tab spaces
//put_text = 0;
var f:array[1..f_max_size]of longint;
i,j,k:longint;
n:longint;
ch:char;
fin:boolean;
procedure n_dice(m:longint; sum:longint);
var i:longint;
begin
if(m=n)then begin
inc(f[sum]);
end else begin
for i:=1 to max_dice do begin
n_dice(m+1,sum+i);
// after throwed the (m+1)th time, add i to the sum
end;
end;
end;
procedure print_res;
begin
writeln('n=', n:tab_sp-2);
write('sum: ');
for i:=1 to max_dice * n do write( i:tab_sp,' '); writeln;
write('distrib: ');
for i:=1 to max_dice * n do write(f[i]:tab_sp,' '); writeln;
for i:=1 to max_dice * n -1 do
if(f[i]<>0)and(f[i]>=f[i+1])then begin
if(f[i]>f[i+1]) then
write('max_psu= ',i:tab_sp)
else
write('max_psu= ',i:tab_sp,' ',i+1:tab_sp); writeln;
write('max_pst= ',f[i]:tab_sp); writeln;
write('thr_tme= ',trunc(exp(n * ln(max_dice) )) :tab_sp ); writeln;
write('max_pty= ',f[i]/ exp(n * ln(max_dice) ) :tab_sp:5); writeln;
break;
end;
writeln;
end;
begin
clrscr;
assign(input,'f:\n_dice\in.txt');
assign(output,'f:\n_dice\out.txt');
reset(input); rewrite(output);
fin:=false;
while( not fin )do begin
fillchar(f, sizeof(f) , 0);
{
write('input n: ');
}
readln(n);
if(n<=0) then begin
writeln('invalid : n = ',n:tab_sp);
halt;
end;
n_dice(0,0);
// after throwed the 0th time, get the sum = 0
print_res;
{
write('finished? (y/n): ');
ch:= readkey;
}
fin:=( eof(input)or(ch='y')or(ch='Y') );
if(fin)then break;
writeln;
end;
close(input); close(output);
end. 展开
{$I-,OBJECTCHECKS-,Q-,R-,S-}
{$ENDIF NORMAL}
{$IFDEF DEBUG}
{$I+,OBJECTCHECKS-,Q+,R+,S-}
{$ENDIF DEBUG}
{$IFDEF RELEASE}
{$I-,OBJECTCHECKS-,Q-,R-,S-}
{$ENDIF RELEASE}
program n_dice_distribution;
uses crt;
const max_dice = 6; //max dice number
max_n = 10; //max throwing dices times
f_max_size = max_dice * max_n; //max size of f
tab_sp = 10; //tab spaces
//put_text = 0;
var f:array[1..f_max_size]of longint;
i,j,k:longint;
n:longint;
ch:char;
fin:boolean;
procedure n_dice(m:longint; sum:longint);
var i:longint;
begin
if(m=n)then begin
inc(f[sum]);
end else begin
for i:=1 to max_dice do begin
n_dice(m+1,sum+i);
// after throwed the (m+1)th time, add i to the sum
end;
end;
end;
procedure print_res;
begin
writeln('n=', n:tab_sp-2);
write('sum: ');
for i:=1 to max_dice * n do write( i:tab_sp,' '); writeln;
write('distrib: ');
for i:=1 to max_dice * n do write(f[i]:tab_sp,' '); writeln;
for i:=1 to max_dice * n -1 do
if(f[i]<>0)and(f[i]>=f[i+1])then begin
if(f[i]>f[i+1]) then
write('max_psu= ',i:tab_sp)
else
write('max_psu= ',i:tab_sp,' ',i+1:tab_sp); writeln;
write('max_pst= ',f[i]:tab_sp); writeln;
write('thr_tme= ',trunc(exp(n * ln(max_dice) )) :tab_sp ); writeln;
write('max_pty= ',f[i]/ exp(n * ln(max_dice) ) :tab_sp:5); writeln;
break;
end;
writeln;
end;
begin
clrscr;
assign(input,'f:\n_dice\in.txt');
assign(output,'f:\n_dice\out.txt');
reset(input); rewrite(output);
fin:=false;
while( not fin )do begin
fillchar(f, sizeof(f) , 0);
{
write('input n: ');
}
readln(n);
if(n<=0) then begin
writeln('invalid : n = ',n:tab_sp);
halt;
end;
n_dice(0,0);
// after throwed the 0th time, get the sum = 0
print_res;
{
write('finished? (y/n): ');
ch:= readkey;
}
fin:=( eof(input)or(ch='y')or(ch='Y') );
if(fin)then break;
writeln;
end;
close(input); close(output);
end. 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询