level 12
var
w,i,l,j,d1,d2,d,l1,l2,x2,x1,num,f:longint;
st,s1,s2,s,ans:string;
a1,a2:array[1..20]of longint;
k1,k2:int64;
begin
readln(st);
w:=pos(' ',st);
l:=length(st);
j:=0;
d1:=0;d2:=0;
s1:='';s2:='';
for i:=1 to w-1 do
if st[i]='.' then begin d1:=i;end;
for i:=w+1 to l do
if st[i]='.' then begin d2:=i-w;end;
if d1<d2
then begin
d:=d2;
for i:=1 to d-d1 do a1[i]:=0;
for i:=1 to d1-1 do a1[i+d-d1]:=ord(st[i])-48;
for i:=d1+1 to w-1 do a1[d+i-d1-1]:=ord(st[i])-48;
l1:=d+w-1-d1-1;
for i:=w+1 to d2+w-1 do a2[i-w]:=ord(st[i])-48;
for i:=d2+w+1 to l do a2[i-1-w]:=ord(st[i])-48;
l2:=l-1-w;
end
else begin
d:=d1;
for i:=1 to d1-1 do a1[i]:=ord(st[i])-48;
for i:=d1+1 to w-1 do a1[i-1]:=ord(st[i])-48;
l1:=w-2;
for i:=1 to d-d2 do a2[i]:=0;
for i:=w+1 to d2+w-1 do a2[i+d-d2-w]:=ord(st[i])-48;
for i:=d2+w+1 to l do a2[i+d-d2-1-w]:=ord(st[i])-48;
l2:=l+d-d2-1-w;
end;
if l1>l2
then begin l:=l1;for i:=l2+1 to l1 do a2[i]:=0;end
else begin l:=l2;for i:=l1+1 to l2 do a1[i]:=0;end;
for i:=1 to l do
begin
s1:=s1+chr(a1[i]+48);
s2:=s2+chr(a2[i]+48);
end;
val(s1,k1);
val(s2,k2);
num:=k1 mod k2;
str(num,s);
ans:='';
w:=l-d+1;
j:=0;
for i:=length(s) downto 1 do
begin
if j=w then ans:='.'+ans;
ans:=s[i]+ans;
inc(j);
end;
if j<w
then begin
while j<w do
begin
ans:='0'+ans;
inc(j);
end;
end;
if j=w
then begin
ans:='0.'+ans;
end;
s:='';
f:=0;
for i:=length(ans) downto 1 do
if f=0
then begin
if ans[i]<>'0'
then begin
s:=ans[i]+s;
f:=1;
end;
end
else s:=ans[i]+s;
writeln(s);
end.
2016年08月17日 08点08分
2
这是转成整数做的,只拿了2分
2016年08月17日 08点08分
level 12
var
x,a,b,d,c:double;
s,k,f:longint;
begin
readln(a,b);
s:=trunc(a/b);
c:=a-(b*s);
d:=c;
k:=0;
while 1=1 do
begin
d:=frac(d);
d:=d*10;
if trunc(d)<>0
then begin
inc(k,f+1);
f:=0;
end
else begin
inc(f);
if f>0 then break;
end;
end;
write(c:0:k);
end.
2016年08月17日 08点08分
3
这是直接算的,拿6分
2016年08月17日 08点08分
@✨小鱼出辉耀 并不是每次d*10以后d都会大于1的,例如a=0.01,b=1的时候。
2016年09月12日 08点09分
level 1
C的话很简单。但Pascal我还没做出来过。
可以试着找下printf的代码来实现……
2016年08月17日 13点08分
4
level 1
program DoubleMod;
procedure PrintFloatDot(x:Double);
var
DeciMant:LongWord=0;
OptStr:string;
begin
if (x>=999999.5) or (x<9.999995e-5) then
exit;
if x<9.999995e4 then
inc(DeciMant);
if x<9.999995e3 then
inc(DeciMant);
if x<9.999995e2 then
inc(DeciMant);
if x<9.999995e1 then
inc(DeciMant);
if x<9.999995e0 then
inc(DeciMant);
if x<9.999995e-1 then
inc(DeciMant);
if x<9.999995e-2 then
inc(DeciMant);
if x<9.999995e-3 then
inc(DeciMant);
if x<9.999995e-4 then
inc(DeciMant);
writestr(OptStr, x:0:DeciMant);
while OptStr[ord(OptStr[0])]='0' do
dec(OptStr[0]);
if OptStr[ord(OptStr[0])]='.' then
dec(OptStr[0]);
writeln(OptStr);
end;
procedure PrintFloatExp(x:Double);
var
OptStr:string;
LastSig:LongWord=7;
begin
if (9.999995e-5<=x) and (x<999999.5) then
exit;
writestr(OptStr, x:13);
Delete(OptStr, 1, 1);
OptStr[8]:='e';
while OptStr[LastSig]='0' do
dec(LastSig);
if OptStr[LastSig]='.' then
dec(LastSig);
Delete(OptStr, LastSig+1, 7-LastSig);
writeln(OptStr);
end;
procedure PrintFloatG(x:Double);
begin
if x=0.0 then
writeln('0')
else if x=-0.0 then
writeln('-0')
else if (9.999995e-5<=x) and (x<999999.5) then
PrintFloatDot(x)
else
PrintFloatExp(x)
end;
var
a, b, r:Double;
begin
read(a, b);
r:=a-trunc(a/b)*b;
PrintFloatG(r);
end.
上面的代码AC了。
不得不承认自己之前弄懂 printf 的 %g 是怎么回事,还是亲自试验了好几个数才完成的。
只写 %g 等价于写 %6g 。
%Ng 的意思浮点数输出时保留 N 位有效数字,四舍五入;若该数为 0 ,或四舍五入后小于 1eN 且大于等于 1e-(N-2) ,则按小数点方式输出,否则按科学记数法输出。
但要注意的是,假如这里小数数字末位有 0 ,则输出时要删掉 0 ;若小数点后全为 0 则连同小数点一同删掉。输出数字含至多 N 位有效数字。
本题要求是 [0.0001, 1000000) ∪ {0} 中的实数按小数点输出,否则按科学记数法输出。
该题期望的是如下的代码:
#include <cstdio> // #
include in C
int main(){ // int main(void) in C
double a, b, r;
scanf("%lf%lf", &a, &b);
r=a-((int)(a/b))*b;
printf("%g\n", r);
return 0;
}
2016年08月17日 18点08分
5
好吧,虽然看不到题目,但是你的程序a=1.00000001,b=100时输出为1显然错了。
2016年09月12日 09点09分
@黄金止手 1.00000001是 [0.0001, 1000000) ∪ {0} 中的实数,应该按照小数点输出。
2016年09月13日 19点09分
@ax_pokl 你去找下下%g的具体细节吧,不管是小数点还是科学计数法都是最多6位有效数字。
2016年09月13日 23点09分