请教高手一个算法

sxkjdxllw0406 2012-02-09 12:26:35
任意长度的int数组,取出这个数组中第95%个元素的值。比如有100个数,那么取出第95个元素的值。
任意长度不知该怎么实现。
函数的接口类似于以下:

int rate_95(int *p[])
{}

函数要返回第95%个元素的值

求教高手!!!
...全文
671 71 打赏 收藏 转发到动态 举报
写回复
用AI写文章
71 条回复
切换为时间正序
请发表友善的回复…
发表回复
godmmx 2012-02-25
  • 打赏
  • 举报
回复
你这个计算数组元素个数的方法太…………,出错的原因是数组引用越界了吧,一般计算个数都是下面这个方法:

int itestArray[100];
.
.
.
.
icount = sizeof(itestArray)/sizeof(itestArray[0]);

[Quote=引用 53 楼 sxkjdxllw0406 的回复:]
我这算法不知哪有问题,编译链接生成exe文件,在命令提示符状态下,运行exe文件,总是弹出xxx引用的内存xxx,该内存不能为read


引用 41 楼 lile1234_show 的回复:

引用 40 楼 sxkjdxllw0406 的回复:

这是我的全部代码:

#include "stdio.h"
#include "conio.h"
#include "math……
[/Quote]
sxkjdxllw0406 2012-02-22
  • 打赏
  • 举报
回复
vector<T>怎么用啊?详细,谢谢[Quote=引用 69 楼 zteny 的回复:]

为什么不用 vector啊

vector<T> col;
col.size()*98/100;

搞 定,
[/Quote]
破晓初 2012-02-21
  • 打赏
  • 举报
回复
为什么不用 vector啊

vector<T> col;
col.size()*98/100;

搞 定,
liustone1263027102 2012-02-21
  • 打赏
  • 举报
回复
int rate_95(int *p, int len)
{
int n = 0.95 * len;
return p[n-1];
}
再加一个表示数组长度的参数
sxldfang 2012-02-21
  • 打赏
  • 举报
回复
[Quote=引用 64 楼 sxkjdxllw0406 的回复:]
嗯,是二级指针。我是说只能是字符型的指针吧

引用 63 楼 sxldfang 的回复:

引用 62 楼 sxkjdxllw0406 的回复:
你的意思是说,main函数的第二个参数只能是以字符形式存储吧?

引用 59 楼 chenjx80 的回复:

楼主的代码写错了?
void main (int argc,int *arr[])
应该为
void main (in……
[/Quote]

是,指向命令行参数的指针
jiandingzhe 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 40 楼 sxkjdxllw0406 的回复:]

这是我的全部代码:

#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "iostream"

using std::cout;
using std::endl;

int rate_95(int *p,int ArrSize)

{
if(p!=0)

return *(p+(ArrS……
[/Quote]
看到里面没有malloc.h就知道楼主一定对内存管理毫无概念。楼主乃需要了解一下堆和栈、动态分配内存的概念,然后看下malloc、calloc、realloc、free函数的用法。
northcan 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sxkjdxllw0406 的回复:]
重点是任意长度的数组该如何实现,如何把任意长度的int数列存储起来?难道要用容器什么的?有没有简单点的方法?
[/Quote]

std::vector 是如何实现的?
sxkjdxllw0406 2012-02-20
  • 打赏
  • 举报
回复
嗯,是二级指针。我是说只能是字符型的指针吧[Quote=引用 63 楼 sxldfang 的回复:]

引用 62 楼 sxkjdxllw0406 的回复:
你的意思是说,main函数的第二个参数只能是以字符形式存储吧?

引用 59 楼 chenjx80 的回复:

楼主的代码写错了?
void main (int argc,int *arr[])
应该为
void main (int argc, char *arr[])



这么说吧,就是个字符二级指针

voi……
[/Quote]
sxldfang 2012-02-19
  • 打赏
  • 举报
回复
[Quote=引用 62 楼 sxkjdxllw0406 的回复:]
你的意思是说,main函数的第二个参数只能是以字符形式存储吧?

引用 59 楼 chenjx80 的回复:

楼主的代码写错了?
void main (int argc,int *arr[])
应该为
void main (int argc, char *arr[])
[/Quote]


这么说吧,就是个字符二级指针

void main (int argc, char **argv)
sxkjdxllw0406 2012-02-19
  • 打赏
  • 举报
回复
你的意思是说,main函数的第二个参数只能是以字符形式存储吧?[Quote=引用 59 楼 chenjx80 的回复:]

楼主的代码写错了?
void main (int argc,int *arr[])
应该为
void main (int argc, char *arr[])
[/Quote]
sxkjdxllw0406 2012-02-19
  • 打赏
  • 举报
回复
比如说有一百个数字,好么第95%个数字就是95.有两百个数字,那么就是190[Quote=引用 45 楼 duyufeng1995 的回复:]

应该是想输出定义的数组里的百分之九十五的数据吧
[/Quote]
采矿工 2012-02-13
  • 打赏
  • 举报
回复
动态分配内存呢?
「已注销」 2012-02-13
  • 打赏
  • 举报
回复
楼主的代码写错了?
void main (int argc,int *arr[])
应该为
void main (int argc, char *arr[])
「已注销」 2012-02-13
  • 打赏
  • 举报
回复
[Quote=引用 40 楼 sxkjdxllw0406 的回复:]

这是我的全部代码:

#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "iostream"

using std::cout;
using std::endl;

int rate_95(int *p,int ArrSize)

{
if(p!=0)

return *(p+(ArrS……
[/Quote]
楼主是不是要这个:
printf("the 95%%th number is: %s", arr[(int)((argc - 1) * 0.95) + 1]);
赵4老师 2012-02-13
  • 打赏
  • 举报
回复
任意长度?试问有一亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿亿个元素那么长吗?
ken_scott 2012-02-13
  • 打赏
  • 举报
回复
上面以为是读文件呢...

在楼主的函数中加个表示长度的参数就是了, 这个长度你一定有办法获得的, 否则你怎么知道到什么时候结束, 就像上面的, 你总得有个知道没有数据了的结束条件
ken_scott 2012-02-13
  • 打赏
  • 举报
回复

#include <stdio.h>

int main()
{
int num = 0;
int index = 0;
int count = 0;
while (true) {
int number = 0;
scanf("%d", &number);
if (number < 0) {
break;
}
++count;
if ((int)(count * 0.95) >= index + 1) {
index = count;
num = number;
}
}
printf("count = %d, index = %d, num = %d\r\n", count, index, num);
return 0;
}

大致思路如上, 上面代码中要解决的唯一问题是取95%的计算有没有什么问题, 你修改代码让它能准确取得95%就可以了
linux-novice 2012-02-13
  • 打赏
  • 举报
回复
int getdate(int p[],int num, double rate)
{
return p[(int)(num*rate) +1];
}
sxkjdxllw0406 2012-02-13
  • 打赏
  • 举报
回复
我这算法不知哪有问题,编译链接生成exe文件,在命令提示符状态下,运行exe文件,总是弹出xxx引用的内存xxx,该内存不能为read

[Quote=引用 41 楼 lile1234_show 的回复:]

引用 40 楼 sxkjdxllw0406 的回复:

这是我的全部代码:

#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "iostream"

using std::cout;
using std::endl;

int rate_95(int *p,int ArrSize)

{……
[/Quote]
面包大师 2012-02-12
  • 打赏
  • 举报
回复
[Quote=引用 51 楼 jialejiahi 的回复:]
引用 50 楼 czh3642210 的回复:

引用 49 楼 jialejiahi 的回复:
引用 47 楼 czh3642210 的回复:

引用 43 楼 jialejiahi 的回复:
引用 42 楼 jialejiahi 的回复:

引用 15 楼 czh3642210 的回复:

引用 14 楼 jialejiahi 的回复:
友情提示:这个不是4,O(∩_∩)……
[/Quote]
加载更多回复(49)

69,373

社区成员

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

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