标签:
A. Chewbaсca and Number
题意:
Inverting digit t means replacing it with digit 9 - t. Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn‘t contain leading zeroes. 分析: It is obvious that all the digits, which are greater than 4, need to be inverted. The only exception is 9, if it‘s the first digit. 时间复杂度:
O(len)
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; char s[ 1000 ]; int main() { scanf( "%s", s ); int i = 0; if( s[i] == ‘9‘ ) i++; for( ; s[i]; ++i ) { char c = ‘9‘ - s[i] + ‘0‘; if( c < s[i] ) s[i] = c; } puts( s ); return 0; }
B. Han Solo and Lazer Gun
题意:
在一个坐标系上有一个机关枪,能够射穿一条线上的所有敌人,问射死整个坐标系中的敌人,至少需要多少发子弹。
分析:
开始考虑的是计算斜率,但是却用了int型导致WA两次,后来改为double AC了,但是使用这样的斜率是存在无常的。如果数据大一点也许就会出现错误。
这里可以考虑三点共线的条件:
Points (x1, y1), (x2, y2), (x3, y3) are on the same line ,if (x2 - x1)(y3 - y1) = (x3 - x1)(y2 - y1).
或者使用gcd和set来避免精度问题
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int sx, sy, n; double res[ 1010 ]; int main() { scanf( "%d %d %d", &n, &sx, &sy ); int flag = 0; int ct = 0; int out = 0; for( int i = 0; i < n; ++i ) { int x, y; scanf( "%d %d", &x, &y ); if( x == sx && y == sy ) continue; int ex = x - sx; int ey = y - sy; if( ex == 0 ) { int k = 0x7fffffff; res[ ct++ ] = k; } else if( ey == 0 ) { int k = 0x3f3f3f3f; res[ ct++ ] = k; } else { double k = (double)ex / (double)ey; res[ ct++ ] = k; } } sort( res, res + ct ); for( int i = 0; i < ct; ++i ) { if( res[i] == res[i+1] ) continue; out += 1; } printf( "%d\n", out ); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1000; long long x[N]; long long y[N]; bool killed[N]; int main() { int n, x0, y0; cin >> n >> x0 >> y0; for (int i = 0; i < n; ++i) { cin >> x[i] >> y[i]; x[i] -= x0; y[i] -= y0; } int result = 0; for (int i = 0; i < n; ++i) if (!killed[i]) { for (int j = 0; j < n; ++j) if (x[i] * y[j] - y[i] * x[j] == 0) killed[j] = true; ++result; } cout << result << endl; return 0; }
#include <cstdio> #include <algorithm> #define N 1005 #define fi(a, b, c) for(int a = (b); a < (c); a++) #define fd(a, b, c) for(int a = (b); a > (c); a--) #define FI(a, b, c) for(int a = (b); a <= (c); a++) #define FD(a, b, c) for(int a = (b); a >= (c); a--) #define fe(a, b, c) for(int a = (b); a; a = c[a]) using namespace std; int n, x, y, a[N], b[N], ans; bool u[N]; int main(){ scanf("%d %d %d", &n, &x, &y); fi(i, 0, n) scanf("%d %d", &a[i], &b[i]); fi(i, 0, n) if(!u[i]){ ans++; fi(j, 0, n) if((a[i] - x) * (b[j] - y) == (a[j] - x) * (b[i] - y)) u[j] = 1; } printf("%d\n", ans); scanf("\n"); }
#include <iostream> #include <vector> #include <set> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios::sync_with_stdio(false); int N, X0, Y0; cin >> N >> X0 >> Y0; set< pair<int, int> > S; for (int i = 0; i < N; ++i) { int X, Y; cin >> X >> Y; int dx = X - X0; int dy = Y - Y0; int d = gcd(dx, dy); dx /= d; dy /= d; if (dy < 0) { dy = -dy; dx = -dx; } if (dx == 0) dy = 1; S.insert(make_pair(dx, dy)); } cout << S.size() << "\n"; }
Codeforces Round #291 (Div. 2)
标签:
原文地址:http://www.cnblogs.com/BigBallon/p/4293617.html