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

来源:互联网 发布:中国网络歌曲 编辑:程序博客网 时间:2024/06/05 11:00

给定两个数 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