基数排序(10^5 以内的数字),有空思考下 10^17 (整数越界的情况)

给定两个数 n,m 

n 代表 1-n,n个数字进行基数排序

m代表 输出第m个数字

如 

输入 11 4   (1,10,11,2,3,4,5,6,7,8,9)

输出 2 


思路 定义一个数组 s[9][100000]

因为 按序(从1-n)来添加,已经是默认的排序


#include<iostream>
//#include<vector>
#include <stdlib.h>
//#include<algorithm>
using namespace std;
const int N=1000;
/*struct Node
{
	int v;
	Node *next;
}s[N];*/

int s[9][N]={0};


void  main()
{ 
	int cc[9]={0};
 int n,m;
 cin>>n>>m;

 int i,j,k;
 for( i=1;i<=n;i++)
	 {
		 //char str1[20];
		 char *str1 = new char[17];
		 itoa(i,str1,10);

		 
		// cout<<"str:"<<str1<<endl;
		//cout<<"k:"<<k<<endl;
		//for(j=0;j<strlen(str1);j++)
		//{
				k=str1[0]-'0';
				int kk=cc[k-1]++;
				//cout<<"str:"<<str1<<" "<<kk<<endl;
				s[k-1][kk]=i;
		//}
		delete str1;

     }
  
 int c=0;
 bool t=true;
 for (i=0;i<9;i++)
	{
		if(t==false)break;
		for(j=0;j<cc[i];j++)
     {
		 if(s[i][j]!=0) {c++; if(c==m) {t=false;break;}}
			else break;
			 
		  //cout<<s[i][j]<<" ";
	 }
	// cout<<endl;	 
 }
  
 cout<<s[i-1][j]<<endl;  
 cout<<"\n排序后的数列:";	
  for (i=0;i<9;i++)
	{
		//if(t==false)break;
		for(j=0;j<cc[i];j++)
     {
		 //if(s[i][j]!=0) {c++; if(c==m) {t=false;break;}}
			//else break;
			 
		   cout<<s[i][j]<<" ";
	 }
	   
 }cout<<endl<<endl;	
 system("pause");
}


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值