排序
#include<bits/stdc++.h>
using namespace std;
int a[110];
void BubbleSort(int a[],int n)
{
for(int i=1;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
if(a[j]<a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
BubbleSort(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i];
if(i<n-1)
cout<<" ";
}
return 0;
}
最大子段和问题
#include<bits/stdc++.h>
using namespace std;
int a[1100];
int maxSum(int a[], int n){
int sum = 0;
int b = 0;
for(int i = 0; i < n; i++){
if(b > 0)
b += a[i];
else
b = a[i];
if(b > sum)
sum = b;
}
return sum;
}
int main(){
int n=0;
int x;
while(cin>>x)
{
a[n]=x;
n++;
}
for(int i = 0; i < n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"数组a的最大连续子段和为:" << maxSum(a, n)<<endl;
return 0;
}
归并排序中,Merge实现合并操作
void Merge(int r[ ], int s, int m, int t)
{
int r1[t], i = s, j = m + 1, k = s;
while (i <= m && j <= t)
{
if (r[i] <= r[j]) r1[k++] = r[i++]; //较小者放入r1[k]
else r1[k++] = r[j++];
}
while (i <= m) //处理第一个子序列剩余记录
r1[k++] = r[i++];
while (j <= t) //处理第二个子序列剩余记录
r1[k++] = r[j++];
for (i = s; i <= t; i++) //将合并结果传回数组r
r[i] = r1[i];
}
二分查找
#include<iostream>
using namespace std;
int a[310];
int erfen(int left,int right,int t)
{
while(left<=right){
int mid=(left+right)/2;
if(a[mid]>t){
right=mid-1;
}
else{
if(a[mid]<t){
left=mid+1;
}
else{
return mid;
}
}
}
return -1;
}
int main()
{
int t=0,sum=0,i=0;
while(cin>>a[i++]&&a[i-1]!=-99999){
sum++;
}
cin>>t;
cout<<erfen(0,sum,t)<<endl;
return 0;
}
两个序列的中位数--减治法
#include<iostream>
using namespace std;
int findmid(int A[],int B[],int n)
{
int s1=0,e1=n-1,s2=0,e2=n-1;
int mid1,mid2;
while(s1<e1&&s2<e2)
{
mid1=(s1+e1)/2;
mid2=(s2+e2)/2;
if(A[mid1]==B[mid2])
return A[mid1];
if(A[mid1]<B[mid2])
{
if((s1+e1)%2==0)
s1=mid1;
else
s1=mid1+1;
e2=mid2;
}
else
{
if((s2+e2)%2==0)
s2=mid2;
else
s2=mid2+1;
e1=mid1;
}
}
if(A[s1]<B[s2])
return A[s1];
return B[s2];
}
int main(){
int n;
cin>>n;
int A[n],B[n];
for(int i=0;i<n;++i){
cin>>A[i];
}
for(int i=0;i<n;++i){
cin>>B[i];
}
cout<<findmid(A,B,n)<<endl;
}
折半查找--减治法
#include <iostream>
using namespace std;
int a[110];
int Search(int r[],int n,int k)
{
int mid, low=0, high=n-1;
while (low<=high)
{
mid = (low+high)/2;
if (k<r[mid]) high=mid-1;
else if (k>r[mid])low=mid+1;
else return mid + 1;
}
return 0;
}
int main()
{
int n;
cin >> n;
for (int i=0;i<n; i++)
{
cin>>a[i];
}
int k;
cin>>k;
cout<<Search(a,n,k)<<endl;
}
选择问题,输出第k小元素
//#include<iomanip>
using namespace std;
int Partition(int r[],int low,int high) //划分
{
int i=low,j=high;
while(i<j)
{
while(i<j&&r[i]<=r[j])
j--;
if(i<j){
int temp=r[i];
r[i]=r[j];
r[j]=temp;
i++;
}
while(i<j&&r[i]<=r[j])
i++;
if(i<j){
int temp=r[i];
r[i]=r[j];
r[j]=temp;
}
}
return i;
}
int SelectMinK(int r[],int low,int high,int k){
int s;
s=Partition(r,low,high);
if(s==k)
return r[s];
if(s>k)
return SelectMinK(r, low, s - 1, k);
else
return SelectMinK(r, s + 1, high, k);
}
int main(){
int a[]={62,47,54,76,95,34,42,84,72,56};
int k;
cin>>k;
int b=SelectMinK(a,0,9,k);
cout<<b<<endl;
return 0;
}
i++;
插入排序程序填空
void InsertSort(int r[ ], int n)
{
int i, j;
for (i = 2; i <= n; i++)
{
r[0] = r[i];
for (j = i - 1; r[0] < r[j]; j--)
r[j] = r[j - 1];
r[j] = r[0];
}
}
堆排序填空
void SiftHeap (int r[ ], int k, int m) //根结点的编号为k,最后一个结点的编号为m
{
int i = k, j = 2*i+1;
while (j <= m)
{
if (j < m && r[j] < r[j+1]) j++;//比较i的左右孩子,j为较大者
if (r[i] > r[j]) break;//根节点已经大于左右孩子中的较大者
else {
temp = r[i]; r[i] = r[j]; r[j] = temp; i = j; j = 2*i+;//将被筛结点与结点j交换
}
}
}
void HeapSort(int r[ ], int n)
{
int i, temp;
for (i = (n-1)/2; i >= 0; i--) //初始建堆
SiftHeap(r,i,n);
for (i = 1; i <= n-1; i++)
{
temp = r[0]; r[0] = r[n-i]; r[n-i] = temp;
SiftHeap(r,0,n-i);
}
}
求连续整数检测算法计算GCD(m, n)算法中需要减法计算的次数。
#include <iostream>
using namespace std;
int main()
{
int t,m,n,counter=0;
cin>>m>>n;
if(m>n)
t = n;
else
t = m;
while(t!=0)
{
if(m%t==0 && n%t==0)
break;
else
t--;
counter++;
}
cout<<counter;
return 0;
}
捕鱼知多.
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
long long ans=1;
for(int i=1;i<=n;i++)
{
ans*=n;
}
cout<<ans-n+1;
return 0;
}
连续或分数背包问题
#include <iostream>
using namespace std;
int main() {
int w[5] = {4, 1, 2, 1, 12}, v[5] = {10, 2, 2, 1, 4}, C;
cin >> C;
int i;
for (i = 0; i < 5; i++) {
if (w[i] <= C) {
C -= w[i];
} else {
break;
}
}
if (i < 5 && C < w[i]) {
cout << i;
}
return 0;
}
找零问题(贪心算法求启发解)
#include <iostream>
using namespace std;
int main()
{
int money[6] = {50, 20, 10, 5, 2, 1}, i, cnt = 0, n;
cin>>n;
for (i = 0; i < 6; i++) /*选取面值依次试探*/
{
if (n >= money[i])
{
int t = n/money[i];
cnt+=t;
n-=money[i]*t;
}
}
cout << cnt;
return 0;
}
0/1背包问题(动态规划)
#include <iostream>
#include <algorithm>
using namespace std;
int fun(int C, int weights[], int values[], int n) {
int dp[n + 1][C + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= C; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (weights[i - 1] > j) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i - 1]] + values[i - 1]);
}
}
}
return dp[n][C];
}
int main() {
int C;
cin >> C;
const int n = 5;
int weights[] = {1,2,12,1,4};
int values[] = {1,2,4,2,10};
int result = fun(C, weights, values, n);
cout << result << endl;
return 0;
}
网格上的最长路径
#include <iostream>
using namespace std;
int main()
{
int m=5,n=3;
int a[5][3]={ 8,75,92, 6,65,71, 5,63,7, 5,87,90, 76,7,8 };
int dist[m][n], path[m][n], i, j,k,l;
cin>>k>>l;
dist[0][0] = a[0][0];
path[0][0] = 0;
for (j = 1; j <n ; j++) //填写第0行
{
dist[0][j] = dist[0][j-1] + a[0][j];
path[0][j] = 1;
}
for (i = 1; i <m; i++) //填写第0列
{
dist[i][0] = dist[i-1][0] + a[i][0];
path[i][0] = 0;
}
for (i = 1; i < m; i++) //填写每一行
for (j = 1; j <n; j++)
if (dist[i-1][j] > dist[i][j-1])
{
dist[i][j] = dist[i-1][j] + a[i][j];
path[i][j] = 0;
}
else
{
dist[i][j] = dist[i][j-1] + a[i][j];
path[i][j] = 1;
}
cout<<dist[k][l];//返回最优值
return 0;
}