鱼C论坛

 找回密码
 立即注册
查看: 2884|回复: 5

[已解决]本题原始代码有误,请各位帮忙解答,谢谢!

[复制链接]
发表于 2018-8-13 14:17:54 | 显示全部楼层 |阅读模式
10鱼币
2.jpg


最佳答案
2018-8-13 14:17:55
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int str2set(int *lite, int length)
  4. {
  5.     int i, j, temp_length = 0;
  6.     int in_flag = 0;//1 is true, 0 is false
  7.     int temp[length];

  8.     for(i = 0;i < length; i++)
  9.     {
  10.         for(j = 0; j < temp_length; j++)
  11.         {
  12.             if(lite[i] == temp[j])
  13.             {
  14.                 in_flag = 1;
  15.                 break;
  16.             }
  17.         }
  18.         if(in_flag == 0)
  19.         {
  20.             temp[j] = lite[i];
  21.             temp_length++;
  22.         }
  23.         in_flag = 0;
  24.     }
  25.     return temp_length;
  26. }

  27. int str2set_forsorted(int *lite, int length)
  28. {
  29.     int i, max, j = 0;
  30.     int in_flag = 0;
  31.     int temp[length];

  32.     max = lite[0];

  33.     for(i = 0; i < length; i++)
  34.     {
  35.         if(lite[i] > max)
  36.         {
  37.             temp[j] = max;
  38.             j++;
  39.             max = lite[i];
  40.         }
  41.     }
  42.     j++;
  43.     return j;
  44. }

  45. int main()
  46. {
  47.     int A[8] = {1, 2, 2, 3, 3, 4, 4, 4};
  48.     int res;
  49.     res = str2set_forsorted(A, 8);
  50.     printf("The length of str2set is: %d",res);
  51.     return 0;
  52. }
复制代码


str2set是没有排序的数组变成集合的函数,需要两层嵌套循环,所以时间复杂度是O(N^2)。

而至于str2set_forsorted是排序之后的数组变成集合的函数,我默认是从小到大排序了。其实这是一个打擂台找最大值/最小值的问题,一旦有新的最大/最小值就需要将其写入数组当中,并更新长度计数器。

此函数用codeblocks测试过,但是我测试的样本较少,可能对某些数组有bug,请您指出。

最佳答案

查看完整内容

str2set是没有排序的数组变成集合的函数,需要两层嵌套循环,所以时间复杂度是O(N^2)。 而至于str2set_forsorted是排序之后的数组变成集合的函数,我默认是从小到大排序了。其实这是一个打擂台找最大值/最小值的问题,一旦有新的最大/最小值就需要将其写入数组当中,并更新长度计数器。 此函数用codeblocks测试过,但是我测试的样本较少,可能对某些数组有bug,请您指出。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-13 14:17:55 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. int str2set(int *lite, int length)
  4. {
  5.     int i, j, temp_length = 0;
  6.     int in_flag = 0;//1 is true, 0 is false
  7.     int temp[length];

  8.     for(i = 0;i < length; i++)
  9.     {
  10.         for(j = 0; j < temp_length; j++)
  11.         {
  12.             if(lite[i] == temp[j])
  13.             {
  14.                 in_flag = 1;
  15.                 break;
  16.             }
  17.         }
  18.         if(in_flag == 0)
  19.         {
  20.             temp[j] = lite[i];
  21.             temp_length++;
  22.         }
  23.         in_flag = 0;
  24.     }
  25.     return temp_length;
  26. }

  27. int str2set_forsorted(int *lite, int length)
  28. {
  29.     int i, max, j = 0;
  30.     int in_flag = 0;
  31.     int temp[length];

  32.     max = lite[0];

  33.     for(i = 0; i < length; i++)
  34.     {
  35.         if(lite[i] > max)
  36.         {
  37.             temp[j] = max;
  38.             j++;
  39.             max = lite[i];
  40.         }
  41.     }
  42.     j++;
  43.     return j;
  44. }

  45. int main()
  46. {
  47.     int A[8] = {1, 2, 2, 3, 3, 4, 4, 4};
  48.     int res;
  49.     res = str2set_forsorted(A, 8);
  50.     printf("The length of str2set is: %d",res);
  51.     return 0;
  52. }
复制代码


str2set是没有排序的数组变成集合的函数,需要两层嵌套循环,所以时间复杂度是O(N^2)。

而至于str2set_forsorted是排序之后的数组变成集合的函数,我默认是从小到大排序了。其实这是一个打擂台找最大值/最小值的问题,一旦有新的最大/最小值就需要将其写入数组当中,并更新长度计数器。

此函数用codeblocks测试过,但是我测试的样本较少,可能对某些数组有bug,请您指出。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-13 14:17:55 | 显示全部楼层
1.
  1. #include<memory.h>
  2. //未测试,或许有bug,请指出,谢谢!
  3. int arr(int* array,int size)
  4. {
  5.         int* a = malloc(size);//重复列表
  6.         int* result = malloc(size);//结果
  7.         memset(a,0,sizeof(int) * size);
  8.         memset(result,0,sizeof(int) * size);
  9.         int i,j,count = 0,flag = 0,rsize = 0;//count:j的有效位数,flag:发现重复后置1
  10.         for(i = 0;i < size;i++)
  11.         {       
  12.                 for(j = 0;j < count;j++)
  13.                 {
  14.                         if(array[i] == a[j])//发现重复!
  15.                         {
  16.                                 flag = 1;
  17.                                 break;
  18.                         }
  19.                 }
  20.                 if(count == 0)
  21.                 {
  22.                         count++;
  23.                         result[i] = array[i];
  24.                         rsize++;
  25.                         a[i] = array[i];
  26.                         continue;
  27.                 }
  28.                 if(!flag)//没有重复
  29.                 {
  30.                         result[i] = array[i];
  31.                         a[i] = array[i];
  32.                         rsize++;
  33.                 }
  34.         }
  35.         return rsize;
  36. }
复制代码

2.时间复杂度应该是O(n^2)吧?(for嵌套)
3.
  1. #include <memory.h>
  2. //这个也没有调试过,或许有bug,请指出,谢谢!
  3. int arr(int* array,int size)
  4. {
  5.         int rsize = 0;
  6.         int flag = 0;//前不相等,置1
  7.         int i = 0;
  8.         for(i = 0;i < size;i++)
  9.         {
  10.                 if(i < size - 1)
  11.                         if(array[i+1] != array[i])//后面的没有重复
  12.                         {
  13.                                 rsize++;
  14.                                 flag = 0;
  15.                         }
  16.         }
  17.         return rsize;
  18. }
复制代码

一个for,时间复杂度是O(n)。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-13 17:01:49 | 显示全部楼层
ZKD 发表于 2018-8-13 16:04
str2set是没有排序的数组变成集合的函数,需要两层嵌套循环,所以时间复杂度是O(N^2)。

而至于str2s ...

       感谢您的帮助!结果正是我需要的,只是在看代码的过程中,不太明白temp[]这个数组的功能,还请解释一下。谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-14 10:20:12 | 显示全部楼层
Kitty喜欢小鱼干 发表于 2018-8-13 17:01
感谢您的帮助!结果正是我需要的,只是在看代码的过程中,不太明白temp[]这个数组的功能,还请解 ...

1、你在检查数组当中重复数字的时候需要将那些第一次出现的数字拷贝到一个新的容器当中,然后在这个新的容器不断对原有数组当中的数字进行检查看是否有重复。而temp数组可以充当这个容器的,temp数组的长度就是删除完重复数字的长度。
2、当你想要检查删除完重复数字的数组究竟是什么的时候,temp数组由于存放着删除完重复数字的结果,也可以给你提供一个参考(直接访问temp就可以)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-8-14 12:10:47 | 显示全部楼层
ZKD 发表于 2018-8-14 10:20
1、你在检查数组当中重复数字的时候需要将那些第一次出现的数字拷贝到一个新的容器当中,然后在这个新的 ...

好的,理解。谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-25 07:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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