鱼C论坛

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

一个C语言程序运行时的问题

[复制链接]
发表于 2016-8-7 21:00:02 | 显示全部楼层 |阅读模式

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

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

x
我这个程序一开始运行成功,但试了几次以后就突然运行时候失败了。我也觉得很奇怪,有人能帮忙看看吗。

#include<stdio.h>
#include<stdlib.h>

struct hill
{  int height;
   int pass;   
}*path;
int R,L;
int slide(int ,int);

int main()
{ int i,j,length,longest=0;
  scanf("%d",&R);
  scanf("%d",&L);
  path = (struct hill*)malloc(sizeof(R*L*sizeof(struct hill)));
  for(i=0;i<R;i++)
    for(j=0;j<L;j++)
      {  scanf("%d",&((path+i*L+j)->height));
                 (path+i*L+j)->pass=0;
          }
  for(i=0;i<R;i++)
    for(j=0;j<L;j++)
       { if((path+L*i+j)->pass)
           length=(path+L*i+j)->pass;
         else
                   length=slide(i,j);
         if(length>longest)
           longest=length;
           }
  free(path);
  printf("%d",longest);          
  return 0;
}

int slide(int x,int y)
{ int i,temp,longest=0;
  int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
  for(i=0;i<4;i++)
    {  if(x+d[i][0]<0 || x+d[i][0]==R || y+d[i][1]<0 || y+d[i][1]==L)
          continue;
       if( (path+(x+d[i][0])*L+y+d[i][1])->height >= (path+x*L+y)->height)
          continue;
       if((path+(x+d[i][0])*L+y+d[i][1])->pass)
          temp=(path+(x+d[i][0])*L+y+d[i][1])->pass;
       else
              temp=slide(x+d[i][0],y+d[i][1]);
       longest= temp>longest ? temp:longest;
    }
    (path+x*L+y)->pass=1+longest;
           return 1+longest;
}


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-8 01:03:59 | 显示全部楼层
本帖最后由 人造人 于 2016-8-8 01:05 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. struct hill
  4. {
  5.         int height;
  6.         int pass;
  7. }*path;

  8. int R, L;

  9. int slide(int x, int y)
  10. {
  11.         int i, temp, longest = 0;
  12.         int d[4][2] = { { -1,0 },{ 1,0 },{ 0,-1 },{ 0,1 } };

  13.         for(i = 0; i < 4; i++)
  14.         {
  15.                 if(x + d[i][0] < 0 || x + d[i][0] == R || y + d[i][1] < 0 || y + d[i][1] == L)
  16.                         continue;

  17.                 if((path + (x + d[i][0])*L + y + d[i][1])->height >= (path + x*L + y)->height)
  18.                         continue;

  19.                 if((path + (x + d[i][0])*L + y + d[i][1])->pass)
  20.                         temp = (path + (x + d[i][0])*L + y + d[i][1])->pass;
  21.                 else
  22.                         temp = slide(x + d[i][0], y + d[i][1]);

  23.                 longest = temp > longest ? temp : longest;
  24.         }

  25.         (path + x * L + y)->pass = 1 + longest;
  26.         return 1 + longest;
  27. }

  28. int main()
  29. {
  30.         int i, j, length, longest = 0;
  31. /*
  32.         scanf("%d", &R);
  33.         scanf("%d", &L);*/

  34.         struct test
  35.         {
  36.                 int i;
  37.                 int j;
  38.         };
  39.         i = sizeof(struct test);
  40.         i = sizeof(2 * 3 * sizeof(struct hill));
  41.         path = malloc(sizeof(R * L * sizeof(struct hill)));
  42.         for(i = 0; i < R; i++)
  43.         {
  44.                 for(j = 0; j < L; j++)
  45.                 {
  46.                         scanf("%d", &((path + i * L + j)->height));
  47.                         (path + i * L + j)->pass = 0;
  48.                 }
  49.         }
  50. /*
  51.         for(i = 0; i < R; i++)
  52.         {
  53.                 for(j = 0; j < L; j++)
  54.                 {
  55.                         if((path + L * i + j)->pass)
  56.                                 length = (path + L*i + j)->pass;
  57.                         else
  58.                                 length = slide(i, j);

  59.                         if(length > longest)
  60.                                 longest = length;
  61.                 }
  62.         }*/

  63.         free(path);
  64.         printf("%d", longest);

  65.         return 0;
  66. }
复制代码

只是找了一个问题,其它还没看
51 行 path = malloc(sizeof(R * L * sizeof(struct hill)));
你认为 sizeof(2 * 3 * sizeof(struct hill)) 应该是多少
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-8 08:33:41 | 显示全部楼层
人造人 发表于 2016-8-8 01:03
只是找了一个问题,其它还没看
51 行 path = malloc(sizeof(R * L * sizeof(struct hill)));
你认为 si ...

天哪。。。。太感谢了   一直没检查出来。。。   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-8 08:52:27 | 显示全部楼层
人造人 发表于 2016-8-8 01:03
只是找了一个问题,其它还没看
51 行 path = malloc(sizeof(R * L * sizeof(struct hill)));
你认为 si ...

天哪。。。。太感谢了   一直没检查出来。。。   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-13 22:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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