数组相关算法
来源:互联网 发布:c语言最大公约数函数 编辑:程序博客网 时间:2024/06/05 15:47
问题1、打印1到最大的n位数
//解法一、直接求出n位数的最大值,然后直接从1开始打印//缺陷:可能会溢出,大数问题void Print1toMax(int n){ int num=1; while(n>0){ num*=10; n--; } for(int i=1;i<num;i++){ cout<<i<<" "; } cout<<endl;}//解法二、在字符串上模拟数字加法void Print1toMax(int n){ int *a=new int[n+1]; while(!Increment(number)){ PrintNumber(number); }}bool Increment(char *number){ int len=strlen(number); int takeover=0; bool isoverflow=false; for(int i=len-1;i>=0;i--){ int nsum=number[i]-'0'+takeover; if(i==len-1){ nsum++; } if(nsum>=10){ if(i==0) isoverflow=true; else{ sum-=10; takeover=1; number[i]='0'+nsum; } }else{ number[i]='0'+nsum; break; } } return isoverflow;}void PrintNumber(char *number){ int len=strlen(number); for(int i=0;i<len;i++){ cout<<number[i]; } cout<<" ";}//解法三、可以发现这就是n个0--9的全排列,只是0开头不打印出来。void PrintNumber(int *number,int len){ for (int i = 0; i<len; i++){ cout << number[i]; } cout << " ";}void PermutationAll(int a[], int len, int index){ if (index == len - 1){//递归的结束条件是已经设置好了最后一位 PrintNumber(a,len); return; } for (int i = 0; i < 10; i++){ a[index + 1] = i ; PermutationAll(a,len,index+1); }}void Print(int n){ int *a = new int[n]; for (int i = 0; i<10; i++){ a[0] = i; PermutationAll(a,n,0); }}
问题2、调整数组顺序使奇数位于偶数前面
void ChangePos(int a[],int n){ int i=0; int j=n-1; while(i<j){ //从前往后找到第一个偶数,从后往前找到第一个奇数,互换 while(i<j&&(a[i]&0x01)!=0) i++; while(i<j&&(a[j]&0x01)==0) j--; swap(a[i],a[j]); i++; j--; }}
问题3、寻找最小的k个数
//解法一、可以基于Partition函数来解决,但是会修改原来的数组int Partition(int a[],int low,int high){ int pivot=a[low]; while(low<high){ for(;low<high&&a[high]>=pivot;high--); if(low<high) a[low++]=a[high]; for(;low<high&&a[low]<=pivot;low++); if(low<high) a[high--]=a[low]; } a[low]=pivot; return low;}void FindLeastKNum(int a[],int n,int k){ if(a==NULL||n<1||k<1||k>n) throw exception("error"); int start=0; int end=n-1; int index=Partition(a,start,end); while(start<end&&index!=k-1){ if(index>k-1){ end=index-1; index=Partition(a,start,end); }else{ start=index+1; index=Partition(a,n,start,end); } } for(int i=0;i<k;i++) cout<<a[i]<<" "; cout<<endl;}//解法二、特别适合处理海量数据void FindLeastKNum(int a[], int n, int k){ multiset<int, greater<int>> intSet; int i = 0; for (; i<k; i++) intSet.insert(a[i]); for (; i<n; i++){ if (a[i]<*intSet.begin()) { intSet.erase(intSet.begin()); intSet.insert(a[i]); } } for (auto c : intSet) cout << c << " "; cout << endl;}
问题4、把数组排成最小的数。
例如给定数组{3,32,321},则打印这3个数字能排成的最小数字321323
int compare(const void *str1, const void *str2){ char *combinestr1 = new char[20]; strcpy(combinestr1,*(char**)str1); strcat(combinestr1, *(char**)str2); char *combinestr2 = new char[20]; strcpy(combinestr2, *(char**)str2); strcat(combinestr2, *(char**)str1); return strcmp(combinestr1,combinestr2);}int main(void){ char *str[3] = {"32","3","321"}; qsort(str, 3, sizeof(char*),compare); for (int i=0;i<3;i++) cout << str[i]<<endl; system("pause"); return 0;}
0 0
- 与数组相关算法
- 数组相关算法
- 数组求和相关算法
- 几个数组相关算法代码
- 基础算法题-数组相关
- 【算法总结-数组相关】 数组中找特定元素相关~
- 【算法总结-数组相关】 数组中找特定元素相关~
- 两个有序数组相关的算法
- (1.2.4.1)数组的相关算法
- 数组排序及相关各种算法
- 数组与字符串相关的算法
- 【算法总结--数组相关】双指针法的常见应用。
- Java二维数组及常见相关算法讲解
- 【算法总结--数组相关】双指针法的常见应用。
- “异或”处理数组的相关算法的面试题
- 【算法总结--数组相关】双指针法的常见应用。
- 算法总结--数组相关】双指针法的常见应用
- 【学习记录】5.2 数组相关的基本算法
- Constrained Joint CRF for Simultaneous FAUR and FLD
- 设计模式之设计原则
- 【NOIP提高组模拟】看电影
- 再推荐一个基于JQuery的UI框架:J-UI
- MFC初始化窗口大小设定
- 数组相关算法
- strlen /sizeof/ 0,'\0','0'中的陷阱
- JavaScript 对象
- Yii中的Event和Behaviour理解
- CSUOJ 1808 地铁(用map储存DP数组的DP)
- Retrofit2.0使用
- PHP 使用.htaccess配置实现二级域名,RewriteCond,RewriteRule
- POJ 2454 Jersey Politics 分组问题 随机化算法
- 一个auto_ptr_ref引发的连锁思考