usingnamespace std; intComp(int *a, int n, int k) { int i, j = 0; priority_queue<int, vector<int>, greater<int>> pq; for (i = 0; i < n; i++) { pq.push(a[i]); } for (i = 1; i <= k; i++) { j = pq.top(); pq.pop(); } return j; } intmain() { int x; //输入的整数个数 cin >> x; //输入x int *a = newint[x]; for (int i = 0; i < x; i++) { //输入a[x] cin >> a[i]; } int y; //需要输出的第k=y大的数 cin >> y; int n = x; for (int k = 1; k <= n; k++) { if (k == y) { cout << Comp(a, n, k); break; } else continue; } return0;
#include<iostream> #include<bits/stdc++.h> //暴力枚举 usingnamespace std; intmain() { int counter = 0; int num = 0; int i, j;
cin >> num; int a[num];//定义一个数组来接收输入的数字 for (i = 0; i < num; i++) { cin >> a[i]; } for (i = 0; i < num - 1; i++) { for (j = i + 1; j < num; j++) { if (a[j] < a[i])
//采用分治法寻找序列中的最大和最小值 #include<bits/stdc++.h> #include<iostream> #define max(x, y) x >= y ? x : y #define min(x, y) x <= y ? x : y #define INF 0x3f3f3f3f usingnamespace std; constint maxn = 200; //数组最大尺寸200 int a[maxn];
int Max, Min;
voidinit() { Max = -INF; Min = INF; }
voidPartGet(int left, int right) { if (left == right) { if (a[left] > Max) Max = a[left]; if (a[left] < Min) Min = a[left]; return; } int mid = (left + right) / 2; PartGet(left, mid); PartGet(mid + 1, right); } intmain() { int Size = 0;
while (cin >> Size && Size) { for (int i = 0; i < Size; i++) cin >> a[i];
#include<iostream> #include<bits/stdc++.h> usingnamespace std; int a, b, c, d, e; int counter = 0; intmain() { for (a = 1; a <= 9; a++) { for (b = 1; b <= 9; b++) { for (c = 1; c <= 9; c++) { for (d = 1; d <= 9; d++) { for (e = 1; e <= 9; e++) { if (a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e && d != e) { if ((a * 10 + b) * (c * 100 + d * 10 + e) == (a * 100 + d * 10 + b) * (c * 10 + e)) counter++; } } } } } } cout << counter << endl; return0; }
#include<iostream> #include<bits/stdc++.h> #include<stdio.h> usingnamespace std; intTo_Judge(int array[20], int sum, int num, int i); intBacktrace(int array[20], int sum, int num,int r); intLayer(int array[20], int sum, int num,int r); int n, k, r = 0, flag = 0, operation[20] = {0}, s[20] = {0}; intTo_Judge(int array[20], int sum, int num, int i){//判断并存储序号 if (sum > k)//已在op里或和大于k return0; else { sum += array[i]; operation[num] = i; if (sum==k && num<=r) { r = num; for (int j = 1; j < r+1; j++) { s[j - 1] = array[operation[j]]; } flag = 1; return0; } Backtrace(array, sum, num + 1,i+1); } return0; } intLayer(int array[20], int sum, int num,int r){//树每层能取的值 for (int i = r; i <= n; i++) { To_Judge(array, sum, num, i); } return0; } intBacktrace(int array[20], int sum, int num,int r){ //树深度num行 if (num > n) return0; Layer(array, sum, num, r); return0; }
intmain(){ int array[20] = {0}; cin >> n; cin >> k; r = n; for ( int i = 1; i <= n; i++) { cin >> array[i]; } Backtrace(array, 0, 1,1); if (flag==0) { printf("No solution"); } else { for (int i = 0; i < r; i++) { cout << s[i] << " "; } cout << endl << r; } }
#include<iostream> #include<stdio.h> #include<bits/stdc++.h> usingnamespace std; intTo_Judge(char arr[20], int num, int i,int k); intBacktrace(char array[20], int num, int k); voidLayer(char arr[20], int num, int k); char operation[20]; int n, m; intTo_Judge(char array[20], int num, int i,int k) { if (i > n) return0; else { operation[num] = array[i]; Backtrace(array, num + 1, i + 1); } return0; } voidLayer(char array[20], int num, int k) { for (int i = k; i < n; i++) { To_Judge(array, num, i, k); } } intBacktrace(char array[20], int num, int k) { if (num >= m) { for (int i = 0; i < m; i++) { cout << operation[i]; } cout << endl; return0; } Layer(array, num, k); return0; }
#include<cstdio> #include<iostream> #include<bits/stdc++.h> // #include <algorithm> usingnamespace std; intcom(int a, int b) { // int a1 = a, b1 = b; int la = 10, lb = 10; while (a / la) { la *= 10; } while (b / lb) { lb *= 10; } return (a * lb + b) - (b * la + a); } intmain() { int i, j, n; int d[20], t; // scanf("%d", &n); cin >> n;
for (i = 0; i < n; i++) { // scanf("%d", &d[i]); cin >> d[i]; } for (j = 0; j < n - 1; j++) for (i = 0; i < n - 1 - j; i++) { if (com(d[i], d[i + 1]) < 0) { t = d[i + 1]; d[i + 1] = d[i]; d[i] = t; } } for (i = 0; i < n; i++) { cout << d[i]; } cout << endl;
for (int i = 1; i <= t; i++) { for (int j = 0; j < n; j++) { if (i >= val[j]) num[i] = min(num[i], num[i - val[j]] + 1); } }
for (int i = t; i >= 0; i--) { if (num[i] < inf) { cout << num[i]<< endl; break; } else { cout << "no answer"; break; } } }
return0; }
算法分析基础知识
算法的特征:
输入,输出,确定性,有穷性,可行性
常见的大 O 表达式
NP(Non-Deterministic Polynomial,非确定多项式)问题:
是指时间复杂度为非多项式量级的算法问题。当数据规模 n 越来越大时,非多项式量级算法的执行会急剧增加,求解问题的执行时间无限增长。所以,非多项式时间复杂度的算法是非常低效的。
常见复杂度从低阶到高阶有:
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2)
最好、最坏情况时间复杂度
1 2 3 4 5 6 7 8 9 10
function find(array,x){ var pos = -1; for(var i=0;i<array.length;i++){ if(array[i] == x){ pos = i; break; } } return pos; }
上面代码表示,查找变量 x 在数组 array 的位置。如果 x 不在数组 array 里,则返回-1。通过从数组第一个元素开始找,当找到变量 x 时则不继续往下找。否则,会遍历完整个数组。最好情况是,数组第一个元素就是要找的变量 x,此时的时间复杂度为 O(1),即最好情况时间复杂度。最坏情况是,要遍历完整个数组,此时的时间复杂度为 O(n),即最坏情况时间复杂度。