标签:sample flag line ase color The 复杂 col 控制
1.三角形
1 #include <iostream> 2 #include <ctime> 3 #define random(l,r) (rand()%(r-l+1)+l) 4 using namespace std; 5 6 int n; 7 int * a; 8 9 void qsort(int, int); 10 void swap(int &, int &); 11 12 int main(int argc, char * argv[]) 13 { 14 bool flag = false; 15 srand((unsigned)time(NULL)); 16 cin >> n; 17 a = new int[n]; 18 for (int i=0; i<n; i++) 19 { 20 cin >> a[i]; 21 } 22 qsort(0,n-1); 23 for (int i=0; i<n-2; i++) 24 { 25 if (a[i]<a[i+1]+a[i+2]) 26 { 27 cout << a[i]+a[i+1]+a[i+2] << endl; 28 flag = true; 29 break; 30 } 31 } 32 if (!flag) cout << 0 << endl; 33 } 34 35 void qsort(int l, int r) 36 { 37 int i=l; 38 int j=r; 39 int mid=a[random(l,r)]; 40 do 41 { 42 while (a[i]>mid) i++; 43 while (a[j]<mid) j--; 44 if (i<=j) swap(a[i++],a[j--]); 45 } 46 while (!(i>j)); 47 if (l<j) qsort(l,j); 48 if (i<r) qsort(i,r); 49 } 50 51 void swap(int &x, int &y) 52 { 53 int t=x; 54 x=y; 55 y=t; 56 }
2.Ants (POJ1852)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 24129 | Accepted: 9652 |
Description
Input
Output
Sample Input
2 10 3 2 6 7 214 7 11 12 7 13 176 23 191
Sample Output
4 8 38 207
1 #include <iostream> 2 3 using namespace std; 4 5 template <typename T> 6 T minn(T, T); 7 8 template int minn<int>(int, int); 9 10 template <typename T> 11 T maxn(T, T); 12 13 template<> int maxn<int>(int, int); 14 15 int main(int argc, char * argv[]) 16 { 17 int k; 18 for (cin >> k; k>0; k--) 19 { 20 int l, n; 21 cin >> l >> n; 22 int * a = new int[n]; 23 for (int i=0; i<n; i++) cin >> a[i]; 24 int tmin=0, tmax=0; 25 for (int i=0; i<n; i++) 26 { 27 tmin=maxn(tmin,minn(a[i],l-a[i])); 28 tmax=maxn(tmax,maxn(a[i],l-a[i])); 29 } 30 cout << tmin << ‘ ‘ << tmax << endl; 31 } 32 return 0; 33 } 34 35 template <typename T> 36 T minn(T x, T y) 37 { 38 if (x<y) return x; 39 return y; 40 } 41 42 template <typename T> 43 T maxn(T x, T y) 44 { 45 if (x>y) return x; 46 return y; 47 } 48 49 template<> int maxn<int>(int x, int y) 50 { 51 if (x>y) return x; 52 return y; 53 }
3.增加难度的抽签问题
for (int a = 0; a < n; a++) { for (int b = 0; b < n; b++) { for (int c = 0; c < n; c++) { for (int d = 0; d < n; d++){ if (k[a]+k[b]+k[c]+k[d] == m) { f = true; } } } } }
1 #include <iostream> 2 #define random(l,r) (rand()%(r-l+1)+l) 3 4 using namespace std; 5 6 int n,m; 7 int * k; 8 9 bool binary_search(int); 10 void qsort(int, int); 11 12 int main(int argc, char * argv[]) 13 { 14 cin >> n >> m; 15 k = new int[n]; 16 for (int i=0; i<n; i++) 17 { 18 cin >> k[i]; 19 } 20 qsort(0,n-1); 21 for (int i=0; i<n; i++) 22 { 23 cout << k[i] << ‘ ‘; 24 } 25 cout << endl; 26 cout.setf(ios_base::boolalpha); 27 cout << binary_search(m) << endl; 28 } 29 30 bool binary_search(int x) 31 { 32 int l=0, r=n-1; 33 while (l<=r) 34 { 35 int mid = (l+r)/2; 36 if (k[mid]==x) return true; 37 else if (k[mid]<x) l=mid+1; 38 else r=mid-1; 39 } 40 return false; 41 } 42 43 void qsort(int l, int r) 44 { 45 int i=l, j=r; 46 int mid=k[random(l,r)]; 47 do 48 { 49 while (k[i]<mid) i++; 50 while (k[j]>mid) j--; 51 if (i<=j) 52 { 53 int temp=k[i]; 54 k[i]=k[j]; 55 k[j]=temp; 56 i++;j--; 57 } 58 } 59 while (i<=j); 60 if (i<r) qsort(i,r); 61 if (j>l) qsort(l,j); 62 }
标签:sample flag line ase color The 复杂 col 控制
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9392230.html