1.判断点是否落在凸多边形内
1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 7 using namespace std; 8 9 typedef struct Node 10 { 11 int a,b; 12 13 }Node; 14 Node node[25]; 15 int N; 16 const double eps = 1e-7; 17 18 double solve1() 19 { 20 int i,j,k; 21 double sum = 0; 22 for(i=2; i<N; i++) 23 { 24 int x1 = node[1].a - node[i].a; 25 int x2 = node[1].a - node[i+1].a; 26 int y1 = node[1].b - node[i].b; 27 int y2 = node[1].b - node[i+1].b; 28 double temp = x1*y2 - x2*y1; 29 sum += temp; 30 31 } 32 return 0.5*fabs(sum); 33 } 34 35 36 double solve2() 37 { 38 int i,j,k; 39 double sum = 0; 40 for(i=1; i<N; i++) 41 { 42 int x1 = node[1].a - node[i].a; 43 int x2 = node[1].a - node[i+1].a; 44 int y1 = node[1].b - node[i].b; 45 int y2 = node[1].b - node[i+1].b; 46 double temp = x1*y2 - x2*y1; 47 /* 48 注意此处temp必须加上绝对值,求真实的面积,若是有向面积 ,则不论在内外都会成立 49 */ 50 sum += fabs(temp); 51 52 } 53 return 0.5*fabs(sum); 54 } 55 56 int main() 57 { 58 int i,j,k; 59 60 int x,y; 61 while(cin>>N>>x>>y) 62 { 63 memset(node,0,sizeof(node)); 64 for(i=1; i<=N; i++) 65 { 66 int a,b; 67 cin>>a>>b; 68 node[i].a = a; 69 node[i].b = b; 70 } 71 double ans1 = solve1(); 72 double ans2 = solve2(); 73 if(fabs(ans1 - ans2)<eps) 74 cout<<"YES"<<endl; 75 else 76 cout<<"NO"<<endl; 77 78 } 79 //while(1); 80 return 0; 81 82 }
2.最小生成树
1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 using namespace std; 7 8 const int maxn = 1010; 9 int u[maxn], v[maxn], w[maxn]; 10 int p[maxn], r[maxn]; 11 int edge = 0; 12 int N; 13 14 int find(int x) 15 { 16 if(p[x] == x) 17 return x; 18 else 19 return p[x] = find(p[x]); 20 } 21 22 bool cmp(int i, int j) 23 { 24 return w[i] < w[j]; 25 } 26 27 int kruskal() 28 { 29 int i,j,k; 30 for(i=0; i<N; i++) 31 p[i] = i; 32 for(i=0; i<edge; i++) 33 r[i] = i; 34 sort(r,r+edge,cmp);//是edge 35 int ans = 0; 36 for(i=0; i<edge; i++) 37 { 38 int e = r[i]; 39 int x = find(u[e]); 40 int y = find(v[e]); 41 if(x!=y) 42 { 43 ans += w[e]; 44 p[x] = y; 45 46 } 47 } 48 return ans; 49 } 50 51 int main() 52 { 53 int i,j,k; 54 55 while(cin>>N) 56 { 57 memset(u,0,sizeof(u)); 58 memset(v,0,sizeof(v)); 59 memset(w,0,sizeof(w)); 60 k = 0; 61 for(i=0; i<N; i++) 62 for(j=0; j<N; j++) 63 { 64 int temp; 65 if(j<=i) 66 { 67 cin>>temp; 68 continue; 69 } 70 cin>>temp; 71 if(temp>=0) 72 { 73 u[k] = i; 74 v[k] = j; 75 w[k++] = temp; 76 edge = k; 77 } 78 } 79 int ans = kruskal(); 80 cout<<ans<<endl; 81 } 82 return 0; 83 }
3.太水了
1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 using namespace std; 7 8 const int maxn = 1010; 9 int N, L; 10 int a[maxn]; 11 12 int main() 13 { 14 int i,j,k; 15 while(cin>>N) 16 { 17 cin>>L; 18 for(i=0; i<N; i++) 19 { 20 cin>>a[i]; 21 } 22 bool flag = false; 23 for(i=0; i<N&&!flag; i++) 24 for(j=i+1; j<N; j++) 25 { 26 if(a[i] + a[j]==L) 27 { 28 cout<<i+1<<endl; 29 cout<<j+1<<endl; 30 flag = true; 31 break; 32 33 } 34 35 } 36 } 37 return 0; 38 }
4.
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 int c[5]; 6 int N,w; 7 int m[5]; 8 int cnt = 0; 9 10 void dfs(int w, int k) 11 { 12 if(k==4)//k==4是出口,不是w==0 13 { 14 if(w==0) 15 { 16 cnt++; 17 return ; 18 19 } 20 return ; 21 } 22 else 23 for(int i=m[k]; i>=0; i--) 24 { 25 if(w>=(c[k]*i))//若是i为0,则某一刻w始终不变,会死循环 26 dfs(w-c[k]*i, k+1); 27 } 28 29 } 30 31 int main() 32 { 33 int i,j,k; 34 for(i=0; i<4; i++) 35 cin>>c[i]; 36 cin>>N; 37 for(i=0; i<N; i++) 38 { 39 cnt = 0; 40 for(j=0; j<4; j++) 41 cin>>m[j]; 42 cin>>w; 43 dfs(w,0); 44 cout<<cnt<<endl; 45 } 46 //while(1); 47 return 0; 48 }
5.
1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <algorithm> 7 8 using namespace std; 9 10 const int maxn = 1000010; 11 long long a[maxn]; 12 long long c[maxn]; 13 int avg; 14 int N; 15 16 void solve_c() 17 { 18 int i,j,k; 19 c[0] = 0; 20 c[1] = avg - a[1]; 21 for(i=2; i<N; i++) 22 { 23 c[i] = c[i-1] + avg - a[i]; 24 25 } 26 27 } 28 int main() 29 { 30 int i,j,k; 31 32 while(cin>>N) 33 { 34 memset(a,0,sizeof(a)); 35 memset(c,0,sizeof(c)); 36 37 long long sum = 0; 38 for(i=1; i<=N; i++) 39 { 40 cin>>a[i]; 41 sum += a[i]; 42 } 43 avg = sum/N; 44 solve_c(); 45 sort(c,c+N); 46 long long temp = c[N/2]; 47 long long ans = 0; 48 for(i=0; i<N; i++) 49 { 50 ans += abs(temp-c[i]); 51 52 } 53 cout<<ans<<endl; 54 } 55 return 0; 56 57 }
6.
7.
8.