| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:有咩有天理了,输出结果错误的程序居然AC了In Reply To:有咩有天理了,输出结果错误的程序居然AC了 Posted by:bdictator at 2011-12-22 15:23:50 附上“AC”的代码,测试2遍均通过
#include <stdlib.h>
#include <string.h>
#include<iostream>
#include<cmath>
using namespace std;
int min(int a,int b){return a<b?a:b;}
void multiply(int *a,int *b,int *c,int la,int lb,int &lc)
{
int i,j,tmp;
for (i=0;i<la;i++)
{
tmp=0;
for (j=0;j<lb&&i+j<125;j++)
{
c[i+j]+=a[i]*b[j]+tmp;
tmp=c[i+j]/10000;
c[i+j]=c[i+j]%10000;
}
if (tmp>0&&i+lb<125)
{
c[i+lb]+=tmp;
}
}
lc=min(125,la+lb+2);
while (c[lc-1]==0&&lc>0) lc--;
}
int main()
{
char s[10];
int binary[100],ans[100],tmp[100],la,lb,lt;
int n,dot,i,j,count,r;
bool div;
while (cin.getline(s,10))
{
memset(binary,0,sizeof(0));
memset(ans,0,sizeof(0));
la=0;lb=0;
r=0;
for (i=0;i<6;i++)
{
if (s[i]=='.')
dot=5-i;
else
{
r*=10;
r+=s[i]-'0';
}
}
while (r%10==0&&dot>0)
{
r/=10;
dot--;
}
n=atoi(s+7);
dot*=n;
if (r<10000)
{
binary[0]=r;
lb=1;
}
else
{
binary[0]=r%10000;
binary[1]=r/10000;
lb=2;
}
ans[0]=1;
la=1;
while (n>0)
{
div=n%2;
n/=2;
if (div)
{
memset(tmp,0,sizeof(tmp));
multiply(binary,ans,tmp,lb,la,lt);
for (i=0;i<lt;i++) ans[i]=tmp[i];
la=lt;
}
memset(tmp,0,sizeof(tmp));
multiply(binary,binary,tmp,lb,lb,lt);
for (i=0;i<lt;i++) binary[i]=tmp[i];
lb=lt;
}
if (dot==0)
{
cout<<ans[la-1];
for (i=la-2;i>=0;i--)
{
j=1000;
while (j>0)
{
cout<<ans[i]/j%10;
j/=10;
}
}
}
else
if ((la-1)*4>dot)
{
cout<<ans[la-1];
count=0;
for (i=la-2;i>=0;i--)
{
j=1000;
while (j>0)
{
cout<<ans[i]/j%10;
j/=10;
count++;
if (count+dot==(la-1)*4) cout<<'.';
}
}
}
else
if ((la-1)*4<=dot&&la*4>dot)
{
dot-=(la-1)*4;
j=1000;
count=0;
while (j>0)
{
if (ans[la-1]/j>0) cout<<ans[la-1]/j%10;
j/=10;
count++;
if (count+dot==4) cout<<'.';
}
for (i=la-2;i>=0;i--)
{
j=1000;
while (j>0)
{
cout<<ans[i]/j%10;
j/=10;
}
}
}
else
if (dot>=la*4)
{
cout<<'.';
for (i=0;i<dot-4*la;i++)
cout<<0;
for (i=la-1;i>=0;i--)
{
j=1000;
while (j>0)
{
cout<<ans[i]/j%10;
j/=10;
}
}
}
cout<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator