c/c++练习–03
- 习题来源:C语言经典编程282例
21. 希尔排序
#include<iostream>
using namespace std;
void ShellSort(int a[],int size){
int k=size;
int tmp;
while(k>1){
k = k/3+1;
for(int i=0; i<k; i++){
for(int j=0;j<size;j+=k){
for(int m=j;m<size;m+=k){
if (a[j]>a[m]){
tmp = a[j];
a[j]=a[m];
a[m]=tmp;
}
}
}
}
}
}
int main(void){
int num[6] ={1,3,2,9,8,7};
for (int i=0;i<6;i++)
cout<<num[i]<<endl;
ShellSort(num,6);
for (int i=0;i<6;i++)
cout<<num[i]<<endl;
return(0);
}
22. 快排
#include<iostream>
using namespace std;
void QuickSort(int num[],int left,int right){
int key=num[left],tmp;
int low=left,mid=left,high=right;
if(left<right){
for(mid=left+1; mid<high;){
if(num[mid]<key){
num[mid-1] = num[mid];
mid++;
}else{
tmp=num[high-1];
num[high-1]=num[mid];
num[mid]=tmp;
high--;
}
}
num[mid-1]=key;
QuickSort(num,left,mid-1);
QuickSort(num,mid+1,right);
}
}
int main(void){
int num[6] ={1,3,2,9,8,7};
for (int i=0;i<6;i++)
cout<<num[i]<<endl;
QuickSort(num,0,6);
for (int i=0;i<6;i++)
cout<<num[i]<<endl;
return(0);
}
23. 归并排序
#include <iostream>
using namespace std;
void Comb(int *a,int l,int m,int r){
int p=m-l+1;
int q=r-m;
int *ll = new int[p+1];
int *rr = new int[q+1];
for(int i=0;i<p;i++)
ll[i]=a[l+i];
for(int i=0;i<q;i++)
rr[i]=a[m+1+i];
ll[p] = 100000000;
rr[q] = 100000000;
for(int i=0,j=0,k=l;k<=r;k++){
if(rr[j]<ll[i]){
a[k]=rr[j];
j++;
} else{
a[k]=ll[i];
i++;
}
}
delete[] ll;
delete[] rr;
}
void MergSort(int* a,int l, int r){
int low=l,high=r;
int mid = (l+r)>>1;
if(l<r){
MergSort(a,low,mid);
MergSort(a,mid+1,high);
Comb(a,low,mid,high);
}
}
int main(void){
int a[6]={3,2,1,8,7,9};
MergSort(a,0,5);
for (int i=0;i<6;i++)
cout<<a[i]<<'\t';
cout<<endl;
return(0);
}
24. 分块查找
#include <iostream>
using namespace std;
struct block{
int key;
int st;
int ed;
};
int blockFind(int x,int *num, block a[]){
int i=0,j=0;
while(i<4 && a[i].key<x)
i++;
j = 3*i;
while(i<4 && j<=a[i].ed && x != num[j])
j++;
return(j);
}
int main(void){
block a[4];
int num[]={1,2,3,4,5,6,7,8,9,10,11,12};
int j=0;
for (int i=0;i<4;i++) {
a[i].st = j;
j+=2;
a[i].ed = j;
a[i].key = num[j];
j+=1;
}
cout<<blockFind(13,num,a) ;
cout<<endl;
return(0);
}
25. 哈希查找
#include<iostream>
#include <string>
using namespace std;
int HashFind(int *a,int x){
int indx = x%7;
if(x == a[indx]) return(indx);
return(-1);
}
int main(void){
int num[7] ={0,1,2,3,7,8,9};
for (int i=0;i<6;i++)
cout<<num[i]<<endl;
cout<<HashFind(num,2)<<endl;
return(0);
}
26. 二分查找
#include<iostream>
#include <string>
using namespace std;
int FindHalf(int x,int *a,int left, int right){
int low=left,high = right;
int mid = (low+high)>>1;
int loc=-1;
while(low<high ){
if(x>a[mid]){
low = mid+1;
mid = (low+high)>>1;
}
else{
if(x<a[mid] ) {
high = mid-1;
mid = (low+high)>>1;
}
else{
loc = mid;
break;
}
}
}
return(loc);
}
int main(void){
int num[6] ={1,2,3,7,8,9};
for (int i=0;i<6;i++)
cout<<num[i]<<endl;
cout<<FindHalf(8,num,0,5)<<endl;
return(0);
}
27. 斐波那契数列
求解该数列的前30个元素。
#include <iostream>
#include <iomanip>
using namespace std;
int main(void){
int a=1,b=1,c;
cout<<a<<endl;
cout<<b<<endl;
for(int i=0;i<30;i++){
c = a+b;
a=b;
b=c;
cout<<c<<endl;
}
return(0);
}
28. 哥德巴赫猜想
100以内的正偶数可以表示为两个素数之和。
#include <iostream>
#include <iomanip>
using namespace std;
int main(void){
int a[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int sum=0,flag=0;
for (int i=4;i<100;i+=2){
for(int j=0;j<25,a[j]<i;j++){
for(int k=0;k<25,a[k]<i;k++){
if(a[j]+a[k]==i){
cout<<i<<"="<<a[j]<<'+'<<a[k]<<endl;
flag=1;
break;
}
}
if (flag)
{
flag =0;
break;
}
}
}
return(0);
}
29. 尼可切斯定理
任何一个整数的立方都可以写成一连串的奇数之和。
#include <iostream>
#include <iomanip>
using namespace std;
int main(void){
int a, a2, a3, high, low;
int sum=0;
cout<<"请输入一个整数:"<<endl;
cin>>a;
a2 =a*a*a;
a3 = a2>>1;
if(a3 %2==0) high= a3+1;
else high =a3;
low = high;
while(low>0){
if(sum>=a2){
if(sum==a2){
cout<<a<<"^"<<3<<"=";
for(int i=low+2;i<high;i+=2)
cout<<i<<"+";
cout<<high;
break;
}
high -=2;
low = high;
sum =0;
}
sum+=low;
low -=2;
}
cout<<endl;
return(0);
}
30. 数值型常量的使用
以十进制,八进制,十六进行分别表示123,并且分别以科学技术法及标准十进制表示123.4。
#include <iostream>
#include <iomanip>
using namespace std;
int main(void){
cout<<hex<<123<<endl;
cout<<dec<<123<<endl;
cout<<setbase(8)<<123<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<123.4<<endl;
cout<<resetiosflags(ios::fixed);
cout<<setiosflags(ios::scientific)<<setprecision(8);
cout<<123.4<<endl;
return(0);
}