|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
今天敲代码的时候发现的问题,代码要求是写一段代码,可以找出给定的两个自然数的最大公因数和最小公倍数,然后我写了下面这段代码:
#include <stdio.h>
#include <string.h>
#define MAX 1024
int compare(int x[], int a, int y[], int b);
int count(int x);
int find(int x);
int count(int x)
{
int i;
int j = 0;
for(i = 1; i <= x; i++)
{
if(x % i == 0)
{
j++;
}
}
return j;
}/*确认x的公因数个数*/
int find(int x)
{
int i;
int j = 0;
int p[MAX];
for(i = 0; i <= x; i++)
{
if(x % i == 0)
{
p[j] = i;
j++;
}
}
return p[j];
}
int compare(int x[], int a, int y[], int b)
{
int i, j;
int temp = 0;
for(i = 0; i< a; i++)
{
for(j = 0; j < b ; j++)
{
if(x[i] == y[j])
{
temp = x[i];
}
}
}
return temp;
}
int main(void)
{
int aa, bb;
int temp = 0;
printf("请输入两个自然数: ");
scanf("%d %d", &aa, &bb);
if(aa < bb)
{
temp = aa;
aa = bb;
bb = temp;
}
int ca, cb;
ca = count(aa);
cb = count(bb);
int p[ca], q[cb];
strcpy(p[ca], find(aa));
strcpy(q[cb], find(bb));
int yin;
yin = compare(p[ca], ca, q[cb], cb);
printf("最大公因数为: %d", yin);
printf("最小公倍数为: %d", aa*bb/yin);
return 0;
}
他的报错是这样子的:
:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2057: expected constant expression
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2466: cannot allocate an array of constant size 0
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2133: 'p' : unknown size
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2057: expected constant expression
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2466: cannot allocate an array of constant size 0
E:\VC6.0green\MyProjects\1117\17.cpp(76) : error C2133: 'q' : unknown size
E:\VC6.0green\MyProjects\1117\17.cpp(77) : error C2664: 'strcpy' : cannot convert parameter 1 from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\VC6.0green\MyProjects\1117\17.cpp(78) : error C2664: 'strcpy' : cannot convert parameter 1 from 'int' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
E:\VC6.0green\MyProjects\1117\17.cpp(81) : error C2664: 'compare' : cannot convert parameter 1 from 'int' to 'int []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
这个问题是出在什么地方了,怎么能改进呢 |
|