ACM题,我该怎么处理大数的情况,求详解!!

a542107840 2010-11-23 09:43:36
这是ACM题目链接http://acm.nit.net.cn/showproblem.jsp?pid=1071
下面是我写的程序,但当测试数据是:
2(10086)3(10086)
2(10085)5(10086)就出错了,我猜就是数据类型太小了,但我想问的是我改成double可不可以,要不然改怎么做?

#include<stdio.h>
#include<string.h>
#include<math.h>
int yushu(int x,int y);
int shu(char*s,int z);
int main()
{
int la,lb,A,B,r;
char a[1025],b[1025];
while(gets(a))
{
gets(b);
if(a[0]=='\n'||b[0]=='\n')
{
printf("1\n");
continue;
}
else
{
la=strlen(a);
lb=strlen(b);
A=shu(a,la);
B=shu(b,lb);
r=yushu(A,B);
printf("%d\n",r);
}
}
return 0;
}
int shu(char*s,int z)
{
int sum=1,k=1,N,n=0,j,i;;
for(i=z-1;i>=0;i--)
{
if(s[i]==')')
{
N=k;
k=0;
j=0;
sum*=(int)pow(N,n);
continue;
}
if(s[i]=='(')
{
n=k;
k=0;
j=0;
continue;
}
k+=(s[i]-'0')*(int)pow(10,j);
}
N=k;
sum*=(int)pow(N,n);
return sum;
}
int yushu(int x,int y)
{
int k;
if(x<y)
{
k=y;
y=x;
x=k;
}
if(x%y==0)
return y;
else
return yushu(y,x%y);
}
...全文
324 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
a542107840 2010-11-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 logiciel 的回复:]
LZ程序在处理最右第1个')'时记录了一个实际无用的数据,但消耗了一个单元,因此
factor_t factor_buffer[128];
要改为
factor_t factor_buffer[129];
[/Quote]
谢谢,我用你说的方法过了,非常感谢~~~·
logiciel 2010-11-25
  • 打赏
  • 举报
回复
LZ程序在处理最右第1个')'时记录了一个实际无用的数据,但消耗了一个单元,因此
factor_t factor_buffer[128];
要改为
factor_t factor_buffer[129];
logiciel 2010-11-25
  • 打赏
  • 举报
回复
输入
2(10086)3(10086)
2(10085)5(10086)
的输出是2(10085),这个值超过了32-bit,因此NIT不会设计这样的输入.

按照LZ的算法,string1和string2的下标实际是质因子,而质因子的取值范围是0..0xFFFFFFFF,因此static int string1[13000],string2[13000]还是不够的.但不可能定义static int string1[0x100000000],string2[0x100000000].建议用其他数据结构来存放输入数据,并修改相应的算法.

以下修改供参考:
#include<stdio.h>
#include<string.h>
#include<math.h>

typedef struct
{
unsigned value;
unsigned number;
} factor_t;

typedef struct
{
factor_t factor_buffer[128];
unsigned factor_count;
} factor_chain_t;

void shu1(factor_chain_t *p,char*s,int z);
void shu2(factor_chain_t *p,factor_chain_t *q,char*s,int z);
int main()
{
int la,lb;
//static int string1[13000],string2[13000];
static factor_chain_t string1, string2;
unsigned
char a[1025],b[1025]; //char a[50000],b[50000];
while(gets(a))
{
gets(b);
la=strlen(a);
lb=strlen(b);
if(la==0||lb==0)
{
printf("1\n");
continue;
}
memset(&string1, 0, sizeof(string1));
memset(&string2, 0, sizeof(string2));
shu1(&string1,a,la);
shu2(&string1,&string2,b,lb);
}
return 0;
}
void shu1(factor_chain_t *p,char*s,int z)
{
int i;
unsigned k=1,N,n=0,j;
for(i=z-1;i>=0;i--)
{
if(s[i]==')')
{
N=k;
k=0;
j=0;
//p[N]=n;
p->factor_buffer[p->factor_count].number = N;
p->factor_buffer[p->factor_count].value = n;
p->factor_count++;
continue;
}
if(s[i]=='(')
{
n=k;
k=0;
j=0;
continue;
}
k+=(s[i]-'0')*(int)pow(10,j);
j++;
}
N=k;
//p[N]=n;
p->factor_buffer[p->factor_count].number = N;
p->factor_buffer[p->factor_count].value = n;
p->factor_count++;
}

unsigned get_factor_value (factor_chain_t *p, unsigned N)
{
unsigned i;
for (i = 0; i < p->factor_count; i++)
{
if (p->factor_buffer[i].number == N) return p->factor_buffer[i].value;
}
return 0;
}

//void shu2(int*p,int*q,char*s,int z)
void shu2(factor_chain_t *p,factor_chain_t *q,char*s,int z)
{
int i;
unsigned sum=1,k=1,N,n=0,j;
unsigned p_N, q_N;
for(i=z-1;i>=0;i--)
{
if(s[i]==')')
{
N=k;
k=0;
j=0;
/*
q[N]=n;
if(p[N]!=0&&p[N]>q[N])
sum*=(int)pow(N,q[N]);
if(p[N]!=0&&p[N]<=q[N])
sum*=(int)pow(N,p[N]);
*/
q->factor_buffer[q->factor_count].number = N;
q->factor_buffer[q->factor_count].value = n;
q->factor_count++;
p_N = get_factor_value(p, N);
q_N = get_factor_value(q, N);
if(p_N!=0&&p_N>q_N)
sum*=(unsigned)pow(N,q_N);
if(p_N!=0&&p_N<=q_N)
sum*=(unsigned)pow(N,p_N);
continue;
}
if(s[i]=='(')
{
n=k;
k=0;
j=0;
continue;
}
k+=(s[i]-'0')*(unsigned)pow(10,j);
j++;
}
N=k;
/*
q[N]=n;
if(p[N]!=0&&p[N]>q[N])
sum*=(int)pow(N,q[N]);
if(p[N]!=0&&p[N]<=q[N])
sum*=(int)pow(N,p[N]);
*/
q->factor_buffer[q->factor_count].number = N;
q->factor_buffer[q->factor_count].value = n;
q->factor_count++;
p_N = get_factor_value(p, N);
q_N = get_factor_value(q, N);
if(p_N!=0&&p_N>q_N)
sum*=(unsigned)pow(N,q_N);
if(p_N!=0&&p_N<=q_N)
sum*=(unsigned)pow(N,p_N);
printf("%u\n",sum); //printf("%d\n",sum);
}


無_1024 2010-11-24
  • 打赏
  • 举报
回复
哎 用字符数组做一个大数运算吧
SENSSSZ 2010-11-24
  • 打赏
  • 举报
回复
其实题目有这么说: 输出:每一行输出a和b的最大公约数(不超过32-bit)。
失落的凡凡 2010-11-24
  • 打赏
  • 举报
回复
这类的题目较简单的做法是用字符串表示大数,自己实现+-*/等运算符。
qpx1125 2010-11-24
  • 打赏
  • 举报
回复
有一种_int64 是大数用的
但你的算法其实应当改进,不要把a和b算出来

比如
2(3)3(1)5(2)
2(2)5(1)7(4)

最大公约数是2(2)5(1)
a542107840 2010-11-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qpx1125 的回复:]
有一种_int64 是大数用的
但你的算法其实应当改进,不要把a和b算出来

比如
2(3)3(1)5(2)
2(2)5(1)7(4)

最大公约数是2(2)5(1)
[/Quote]

#include<stdio.h>
#include<string.h>
#include<math.h>
void shu1(int*p,char*s,int z);
void shu2(int*p,int*q,char*s,int z);
int main()
{
int la,lb;
static int string1[13000],string2[13000];
char a[50000],b[50000];
while(gets(a))
{
gets(b);
la=strlen(a);
lb=strlen(b);
if(la==0||lb==0)
{
printf("1\n");
continue;
}
shu1(string1,a,la);
shu2(string1,string2,b,lb);
}
return 0;
}
void shu1(int*p,char*s,int z)
{
int k=1,N,n=0,j,i;
for(i=z-1;i>=0;i--)
{
if(s[i]==')')
{
N=k;
k=0;
j=0;
p[N]=n;
continue;
}
if(s[i]=='(')
{
n=k;
k=0;
j=0;
continue;
}
k+=(s[i]-'0')*(int)pow(10,j);
j++;
}
N=k;
p[N]=n;
}
void shu2(int*p,int*q,char*s,int z)
{
int sum=1,k=1,N,n=0,j,i;
for(i=z-1;i>=0;i--)
{
if(s[i]==')')
{
N=k;
k=0;
j=0;
q[N]=n;
if(p[N]!=0&&p[N]>q[N])
sum*=(int)pow(N,q[N]);
if(p[N]!=0&&p[N]<=q[N])
sum*=(int)pow(N,p[N]);
continue;
}
if(s[i]=='(')
{
n=k;
k=0;
j=0;
continue;
}
k+=(s[i]-'0')*(int)pow(10,j);
j++;
}
N=k;
q[N]=n;
if(p[N]!=0&&p[N]>q[N])
sum*=(int)pow(N,q[N]);
if(p[N]!=0&&p[N]<=q[N])
sum*=(int)pow(N,p[N]);
printf("%d\n",sum);
}
你好,我也觉的我的方法太挫了,按照你的想法我改了程序,而且我把数组的长度也加大了,可是还是runtime erro,怎么会这样啊?该怎么办呢?路过的帮帮忙,谢谢!!

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧