| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 868 人关注过本帖
标题:这是一个选择查找的问题
只看楼主 加入收藏
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
 问题点数:0 回复次数:2 
这是一个选择查找的问题

可以帮我看看这个程序吗?

这个程序要求找到第K个大的元素

#include"iostream" using namespace std void Find_Kth_Largest(int list[],int n,int k){ //list:the value to look through //n:the size of the list //k:the element to select int i,largest,largest_location,j; for(i=1;i<=k;i++){//for_1 largest=list[1]; largest_location=1; for(j=2;j<=N-(i-1);j++){//for_2 if(list[j]>largest){ largest=list[j]; largest_location=j; }//if }//for_2 swap(list[n-(i-1)],list[largest_location]);//exchanging, making the largest elements //in the last elements of the list }//for_1 cout<<"The number is:"; <<largest; }//Find_Kth_Largest void swap(int &a,int &b){//exchange int temp; temp=a; a=b; b=temp; }//swap void main(){ int List[16],K; cout<<"Please input the K"; cin<<K; List[0]=1; for(i=1;i<=15;i++){ List[i]=2*i-1; List[0]++; }//for Find_Kth_Largest(List,List[0],K); }

程序出错时,那些提示信息到底是怎么看啊?

搜索更多相关主题的帖子: largest list int location std 
2004-11-04 16:00
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 

错误已经排除了,完整的程序如下:

#include"iostream"

using namespace std;

void Find_Kth_Largest(int list[],int n,int k){

//list:the value to look through

//n:the size of the list

//k:the element to select

int i,largest,largest_location,j;

for(i=1;i<=k;i++){//for_1

largest=list[1];

largest_location=1;

for(j=2;j<=n-(i-1);j++){//for_2

if(list[j]>largest){

largest=list[j];

largest_location=j;

}//if

}//for_2

swap(list[n-(i-1)],list[largest_location]);//exchanging, making the largest elements

//in the last elements of the list

}//for_1

cout<<"The number is:"<<largest<<"\n";

}//Find_Kth_Largest

void swap(int &a,int &b){//exchange

int temp;

temp=a;

a=b;

b=temp;

}//swap

void main(){

int List[16],K,i;

cout<<"Please input the K:";

cin>>K;

cout<<"\n";

List[0]=0;

for(i=1;i<=15;i++){

List[i]=2*i-1;

List[0]++;//List[0] is stored the length of the List

}//for

cout<<"\n"<<List[0]<<"\n";//output the length

Find_Kth_Largest(List,List[0],K);

}

2004-11-04 16:46
talking
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-12-20
收藏
得分:0 

问老师啊!问同学啊!这里不好解答

2005-12-20 10:19
快速回复:这是一个选择查找的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017085 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved