#include<bits/stdc++.h>using namespace std;const int SIZE = 100;int n,i,j,max_f,ans;int main(){ cin >> n; for(i = 1 ; i <= n ; i++){ cin >> x[i] >> y[i]; } max_f = 0; for(i = 1 ; i <= n ; i ++){ f[i] = 0; for(j = 1 ; j <= n ; j ++){ if(x[j] < x[i] && y[j] < y[i]){ f[i] = y[i]; } } if(f[i] <= max_f){ max_f = f[i]; ans = i; } } for(i = 1 ; i <= n ; i ++){ cout << f[i] << endl; } cout << ans << endl; return 0;}
#include<bits/stdc++.h>using namespace std;const int dayNum[] = {-1,31,28,31,30,31,31,30,31,30,31};int m,offset,i;int main(){ cin >> m; cout << "S\tM\tT\tW\tT\tW\tS" << endl; offset = 3; for(i = 1 ; i < m ; i ++){ offset = (offset+dayNum)%7; } for(i = 1 ; i < offset ; i ++){ cout << '\t'; } for(i =1 ; i < dayNum[m] ; i ++){ cout << i; if(i == dayNum[m] || (offset + i)){ cout << endl; }else{ cout << '\t'; } } return 0;}
给定两个数组,找出b数组中包含的数
int main(){ int n,m; cin >> n >> m; for(int i = 1 ; i <= n ; i ++){ cin >> arr[i]; } for(int i = 1 ; i <= m ; i ++){ cin >> brr[i]; } sort(arr+1,arr+1+n); sort(brr+1,brr+1+m); for(int i = 1 ; i <= m ; i ++){ if(binary_search(brr[i])){ cout << brr[i] << " "; } } return 0;}
[USACO05FEB] 进击的奶牛 Aggressive Cows G
#include<bits/stdc++.h>using namespace std;int a[10005];int b[10005];int n,m;int binary_search(int x){ int l = 0, r = n + 1; if(a[1] > x || a[n]<x){ return -1; } while(l + 1 != r){ int mid = (l+r) >> 1; if(a[mid] < x) r = mid; else if(mid < x) l = mid; } if(a[r] == x) return r; else return -1;}int main(){ cin >> n >> m; for(int i = 1 ; i <= n ; i ++){ cin >> a[i]; } for(int i = 1 ; i <= m ; i ++){ cin >> b[i]; } sort(a+1,a+1+n); sort(b+1,b+1+m); for(int i = 1 ; i <= m ; i ++){ if(binary_search(b[i])){ cout << b[i] << " "; } } return 0;}
方阵填数
#include<bits/stdc++.h>using namespace std;int a[10][10];void f(int x,int y , int k){ a[x][y] = k; if(y+1 <= 4 && a[x][y+1] == 0){ f(x,y+1,k+1); } if(x+1 <= 4 && a[x+1][y] == 0){ f(x+1,y,k+1); } if(y-1 >= 1 && a[x][y-1] == 0){ f(x,y-1,k+1); } if(x-1 >= 1 && a[x-1][y] == 0){ f(x-1,y,k+1); }}int main(){ f(1,1,1); for(int i = 1 ; i <= 4 ; i ++){ for(int j = 1 ; j <= 4 ; j ++){ cout << setw(5) <<a[i][j]; } cout << endl; }}
深搜基础框架
#include<bits/stdc++.h>using namespace std;int n;char a[10][10];int path[410][3];void print(int k){ for(int i = 1 ; i <= k ; i ++){ cout << "(" << path[i][1] << "," << path[i][2] << ")"; if(i != k){ cout << "->"; } }}void dfs(int x,int y , int k){ path[k][1] = x; path[k][2] = y; a[x][y] = '1'; if(x == n && y == n){ print(k); } if(a[x][y+1] == '0'){ dfs(x,y+1,k+1); } if(a[x+1][y] == '0'){ dfs(x+1,y,k+1); } if(a[x][y-1] == '0'){ dfs(x,y-1,k+1); } if(a[x-1][y] == '0'){ dfs(x-1,y,k+1); }}int main(){ cin >> n; for(int i = 1 ; i <= n ; i ++){ for(int j = 1 ; j <= n ; j ++){ cin >> a[i][j]; } } dfs(1,1,1);}
#include<bits/stdc++.h>using namespace std;int n = 0 ;bool mark[6][6];int path[26][3];int count;int dx[5] = {0,0,1,0,-1};int dy[5] = {0,1,0,-1,0};void print_path(int k){ for(int i = 1 ; i < k ; i ++){ cout << path[i][1] << "," << path[i][2] << "->"; } cout << path[k][1] << "," << path[k][2] << endl;}void dfs(int x,int y , int k){ path[k][1] = x; path[k][2] = y; if(x == n && y == n){ print_path(k); return; } for(int i = 1 ; i <= 4 ; i++){ int tx = x + dx[i]; int ty = y + dy[i]; if(mark[tx][ty] == false && tx >= 1 && tx <= n && tx <= n && ty <= n){ mark[tx][ty] = true; dfs(tx,ty,k+1); mark[tx][ty] = false; path[k+1][1] = 0; path[k+1][2] = 0; } }}int main(){ cin >> n; // 3 mark[1][1] = true ; dfs(1,1,1); // mark的行、mark的列、path的行}
// #include<bits/stdc++.h>// using namespace std;// const int SIZE = 100;// int main(){// int n , i , sum,x,a[SIZE];// cin >> n;// memset(a,0,sizeof(a));for(i = 1 ; i <= n ; i ++){// cin >> x ;// a[x] ++;// }// i = 0;// sum = 0;// while(sum < (n/2+1)){// i ++;// sum += a[i];// }// cout << i << endl;// return 0;// }// #include<bits/stdc++.h>// using namespace std;// int main(){// int a[6] = {1,2,3,4,5,6};// int pi = 0;// int pj = 5 ; // int t,i;// while (pi < pj){// t=a[pi];// a[pi] = a[pj];// a[pj] = t;// pi ++;// pj --;// }// for(i = 0 ; i < 5 ; i ++){// cout << a[i] << ",";// }// cout << a[5];// cout << endl;// return 0;// }// #include<bits/stdc++.h>// using namespace std;// int arr[9] = {11,22,66,77,33,55,44,88};// int brr[9];// void merge(int l , int mid , int r){// int i = l ; // int k = l;// int j = mid + 1 ;// while((i <= mid) && (j <=r)){// if(arr[i] >arr[j]){// brr[k++] = arr[i++];// }else{// brr[k++] = arr[j++];// }// }// if(i <= mid){// for(k = i ; k < mid ; k++){// cout << arr[k]<< ' ';// }// }else{// for(k = j ; k < r ; k++){// cout << brr[k]<< ' ';// }// }// while(i <= mid){// brr[k++] = arr[i++];n// }// while(j <= r){// brr[k++] = arr[j++];// }// for (int i = l ; i <= r ; i ++){// arr[i] = brr[i];// }// }// void mergeSort(int l , int r){// if(l >= r) return ;// int mid = (l+r)/2;// mergeSort(l,mid);// mergeSort(mid+1 , r);// merge(l,mid,r);// }// int main(){// mergeSort(1,8);// return 0;// }// #include<bits/stdc++.h>// using namespace std;// long long a[60] = {0};// a[1] = 1;// a[2] = 1;// int tz(long long x){// if(a[x] != 0){// return a[x];// }else{// a[x] = tz(x-1) + tz(x-2);// return a[x];// }// }// int main(){// }// #include<bits/stdc++.h>// using namespace std;// int a[10][10];// void f(int x,int y , int k){// a[x][y] = k;// if(y+1 <= 4 && a[x][y+1] == 0){// f(x,y+1,k+1);// }// if(x+1 <= 4 && a[x+1][y] == 0){// f(x+1,y,k+1);// }// if(y-1 >= 1 && a[x][y-1] == 0){// f(x,y-1,k+1);// }// if(x-1 >= 1 && a[x-1][y] == 0){// f(x-1,y,k+1);// }// }// int main(){// f(1,1,1);// for(int i = 1 ; i <= 4 ; i ++){// for(int j = 1 ; j <= 4 ; j ++){// cout << setw(5) <<a[i][j];// }// cout << endl;// }// }// #include<bits/stdc++.h>// using namespace std;// int n;// char a[10][10];// int path[410][3];// void print(int k){// for(int i = 1 ; i <= k ; i ++){// cout << "(" << path[i][1] << "," << path[i][2] << ")";// if(i != k){// cout << "->";// }// }// }// void dfs(int x,int y , int k){// path[k][1] = x;// path[k][2] = y;// a[x][y] = '1';// if(x == n && y == n){// print(k);// }// if(a[x][y+1] == '0'){// dfs(x,y+1,k+1);// }// if(a[x+1][y] == '0'){// dfs(x+1,y,k+1);// }// if(a[x][y-1] == '0'){// dfs(x,y-1,k+1);// }// if(a[x-1][y] == '0'){// dfs(x-1,y,k+1);// }// }// int main(){// cin >> n;// for(int i = 1 ; i <= n ; i ++){// for(int j = 1 ; j <= n ; j ++){// cin >> a[i][j];// }// }// dfs(1,1,1);// }// #include<bits/stdc++.h>// using namespace std;// int n = 0 ;// bool mark[6][6];// int path[26][3];// int count;// int dx[5] = {0,0,1,0,-1};// int dy[5] = {0,1,0,-1,0};// void print_path(int k){// for(int i = 1 ; i < k ; i ++){// cout << path[i][1] << "," << path[i][2] << "->";// }// cout << path[k][1] << "," << path[k][2] << endl;// }// void dfs(int x,int y , int k){// path[k][1] = x;// path[k][2] = y;// if(x == n && y == n){// print_path(k);// return;// }// for(int i = 1 ; i <= 4 ; i++){// int tx = x + dx[i];// int ty = y + dy[i];// if(mark[tx][ty] == false && tx >= 1 && tx <= n && tx <= n && ty <= n){// mark[tx][ty] = true;// dfs(tx,ty,k+1);// mark[tx][ty] = false;// path[k+1][1] = 0;// }// }// }// int main(){// cin >> n; // 3// mark[1][1] = true ;// dfs(1,1,1); // mark的行、mark的列、path的行// }#include<bits/stdc++.h>using namespace std;int n;int a[1000005];void quickSort(int left , int right){ if(left >= right){ return; } int i = left; int j = right; int mid = (left + right) / 2 ; int p = a[mid]; while (left <= right){ while(a[i] < p){ i++; } while(a[j] > p){ j--; } if(i <= j){ swap(a[i] ,a[j]); i ++; j --; } } if(left < j)quickSort(left,j); if(i < right) quickSort(i , right);}int main(){ cin >> n; int mid = 0; for(int i = 0 ; i < n ; i ++){ cin >> a[i]; } quickSort(0,n-1); for(int i = 0 ; i < n ; i ++){ cout << a[i] << ' '; }}