首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

ACM 高精度处理有关问题

2012-02-12 
ACM 高精度处理问题这题是北大ACM的1001一题在处理进位时有点小问题,麻烦高手还能给帮忙解决一下代码如下:

ACM 高精度处理问题
这题是北大ACM的1001一题
在处理进位时有点小问题,麻烦高手还能给帮忙解决一下

代码如下:
#include<iostream>
using namespace std;
typedef struct node
{
int n;//多少次幂
int value[5];//用来存放五位数,小数点单独存放
int point_num;//小数点位数
}type;
void input(type&t)
{//输入数字并转化到t.value[]中
t.point_num=0;
char c[10];
int i,j=0;
for(i=0;i<6;++i)
cin>>c[i];
cin>>t.n;
for(i=5;i>=0;--i)
if(c[i]=='.')
t.point_num=i;
else
t.value[j++]=c[i]-'0';
t.point_num=5-t.point_num; 
}
int main()
{
type t;input(t);
int i ,j,k;int length=5;//数字的位数
int a[150][150];//用来存放计算结果
memset(a,0,sizeof(a));
for(i=0;i<5;++i)a[0][i]=t.value[i];
for(i=1;i<t.n;++i)
{
for(j=0;j<length;++j)
for(k=0;k<5;++k)
a[i][j+k]+=a[i-1][j]*t.value[k];

for(k=0;k<=length;++k)
if(a[i][k]>=10)
{
a[i][k+1]+=a[i][k]/10;
a[i][k]=a[i][k]%10;
while(a[i][length]>0) length++;
}
}
//以下都是输出处理,输出处理也有些问题
j=0;
for(i=0;i<t.n*t.point_num;++i)
if(a[t.n-1][i]==0)
j++;
else break;

if(t.n*t.point_num>length)length=t.n*t.point_num+1;//如0.4321 20就属于这种情况
for(i=length-1;i>=j;--i)
if(a[t.n-1][i]!=0||i==t.n*t.point_num)
{
if(i==t.n*t.point_num)
{
cout<<'.';
while(i>=j)
{
cout<<a[t.n-1][i];
i--;
}
}
else 
{
while(i>=j)
{
cout<<a[t.n-1][i];
if(i==t.n*t.point_num)cout<<'.';
i--;
}
}
}


return 0;
}

对于一般情结果处理是正确的,对于如 1.0100 12,进位就有问题了,弄了半天也没找到原因
对于结果输出格式有什么好的建议也可说一说,帮我一把,呵呵,谢了……




[解决办法]
PKU的题,不太适合初学者,建议那经航空的很好
#include <iostream>
#include <vector>
#include <map>
using namespace std;

map<string,int> student,course;
vector<int> select[21];//select[i] :time that student i's be occured! 
int capacity[31]; //capacity of the class
int have[21][21]; // have[i][j] represents class i already has student j;
int crstime[21][21]; //crstime[i][0]:number of time to have class i ,c[i][j] :the time to have class
class request
{
public:
string stu_id;
string crs_id;
};
void init()
{
memset(capacity,0,sizeof(capacity));
memset(have,0,sizeof(have));
memset(crstime,0,sizeof(crstime));
student.clear();
course.clear();
for(int i = 0;i < 21;i++)
{
select[i].clear();
}
}
int main()
{
int n,m,p;
string id;
request re;
int result = 0;
int tmp_stu,tmp_crs;
int num = 0;
while(cin>>n>>m>>p)
{
init();
num += 1;
for(int i = 1;i <= n;i++)
{
cin>>id;
student[id] = i;

for(int i = 1;i <= m;i++)
{
cin>>id;
course[id] = i;
cin>>capacity[i];
cin>>crstime[i][0];
for(int j = 1;j <= crstime[i][0];j++)
{
cin>>crstime[i][j];
}
}
for(int i = 1;i <= p;i++)
{
vector<int>::iterator it;
cin>>re.stu_id;
cin>>re.crs_id;
tmp_stu = student[re.stu_id];
tmp_crs = course[re.crs_id];


if(have[tmp_crs][tmp_stu])
continue;
if(capacity[tmp_crs] <= 0)
continue;
for(int j = 1;j <= crstime[tmp_crs][0];j++)
{
it = find(select[tmp_stu].begin(),select[tmp_stu].end(),crstime[tmp_crs][j]);
if(it != select[tmp_stu].end())
goto another;
}
result += 1;
have[tmp_crs][tmp_stu] = 1;
capacity[tmp_crs] -= 1;
another: ;
}
cout<<"Case "<<num<<": "<<result<<endl;
result = 0;
}
return 0;
}

热点排行