鱼C论坛

 找回密码
 立即注册
查看: 2372|回复: 3

c的问题

[复制链接]
发表于 2019-11-17 18:15:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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
这个问题是出在什么地方了,怎么能改进呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-17 18:30:32 | 显示全部楼层
:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2057:应为常量表达式

E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2466:无法分配大小为0的常量数组

E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2133:“p”:未知大小

E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2057:需要常量表达式

E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2466:无法分配大小为0的常量数组

E:\VC6.0green\MyProjects\1117\17.cpp(76):错误C2133:“q”:未知大小

E:\VC6.0green\MyProjects\1117\17.cpp(77):错误C2664:“strcpy”:无法将参数1从“int”转换为“char*”

从整数类型转换为指针类型需要重新解释强制转换、C样式强制转换或函数样式强制转换

E:\VC6.0green\MyProjects\1117\17.cpp(78):错误C2664:“strcpy”:无法将参数1从“int”转换为“char*”

从整数类型转换为指针类型需要重新解释强制转换、C样式强制转换或函数样式强制转换

E:\VC6.0green\MyProjects\1117\17.cpp(81):错误C2664:“compare”:无法将参数1从“int”转换为“int[]

从整数类型转换为指针类型需要重新解释强制转换、C样式强制转换或函数样式强制转换

百度翻译一下不难吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-17 18:44:20 | 显示全部楼层
本帖最后由 jackz007 于 2019-11-17 18:49 编辑

        求两个数的最大公约数和最小公倍数,87 行代码,整太复杂了,试试这个代码吧:
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int m , n , t , x , y                                                     ;
  5.         printf("输入两个整数 m , n : ")                                             ;
  6.         scanf("%d%d", & m , & n)                                                  ;
  7.         if(m > 0 && n > 0) for(x = m , y = n ; y > 0 ; t = x % y , x = y , y = t) ;
  8.         if (x > 1) printf("最大公约数为 : %d\n" , x)                                ;
  9.         else printf("m , n 没有最大公约数\n")                                       ;
  10.         printf("最小公倍数为 : %d\n" , m * n / x)                                   ;
  11.         return 0                                                                  ;
  12. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-17 18:57:43 | 显示全部楼层
本帖最后由 bin554385863 于 2019-11-18 02:38 编辑
  1. #include <stdio.h>
  2. #include <limits.h>
  3. typedef struct
  4. {
  5.     int MAXCommonDivisor;
  6.     int MinCommonMultiple;
  7. } num;

  8. num func1(const int n, const int m)
  9. {
  10.     num res = {-1, -1};
  11.     int tmax = m > n ? m : n;
  12.     int tmin = m < n ? m : n;
  13.     if (tmax >= __INT32_MAX__ )
  14.     {
  15.         return res;
  16.     }
  17.     else
  18.     {
  19.         for (size_t i = tmax;; i++)//最小公倍数一定大于等于较大的数
  20.         {
  21.             if (i % m == 0 && i % n == 0)
  22.             {
  23.                 res.MinCommonMultiple = i;
  24.                 break;
  25.             }
  26.         }
  27.         for (size_t i = tmin; i > 0; i--)//最小公约数一定小于等于较小的数,且1是所有数的公约数
  28.         {
  29.             if (m % i == 0 && n % i == 0)
  30.             {
  31.                 res.MAXCommonDivisor = i;
  32.                 break;
  33.             }
  34.         }
  35.     }
  36.     return res;
  37. }
  38. int main(int argc, char const *argv[])
  39. {
  40.     int a = 24, b = 288;
  41.     int m = 37, n = 15;
  42.     printf("%d & %d--(%d , %d)\n", a, b, func1(a, b).MAXCommonDivisor, func1(a, b).MinCommonMultiple);
  43.     printf("%d & %d--(%d , %d)\n", m, n, func1(m, n).MAXCommonDivisor, func1(n, m).MinCommonMultiple);
  44.     return 0;
  45. }
复制代码

------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-uzatlvzi.lj2 --stdout=Microsoft-MIEngine-Out-rjc5cw52.bqv --stderr=Microsoft-MIEngine-Error-r3ancrla.4xx --pid=Microsoft-MIEngine-Pid-ic40r14d.fft --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
24 & 288--(24 , 288)
37 & 15--(1 , 555)


E:\Users\admin\Documents\VScode>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-15 18:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表