HDU 5912 Fraction
就是简单的迭代, 当时在看逃学威龙分心了, 果然做题还是要来实验室
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int a[10]; int b[10]; int cases; int gcd( int x, int y ) { return y == 0 ? x : gcd( y, x % y ); } int main() { int t; while( cin >> t ) { cases = 0; while( t-- ) { cases++; int n; cin >> n; for( int i = 1; i <= n; i++ ) { scanf( "%d", a+i ); } for( int i = 1; i <= n; i++ ) { scanf( "%d", b+i ); } int p = b[n]; int q = a[n]; for( int i = n-1; i >= 1; i-- ) { int tp = b[i] * q; int tq = a[i] * q + p; int t = gcd( tp, tq ); tp /= t; tq /= t; p = tp; q = tq; } int t = gcd( p, q ); p /= t; q /= t; printf( "Case #%d: ", cases ); printf( "%d %d\n", p, q ); } } return 0; }
HDU 5914 Triangle
打表, dp, 用一个vector记录上一个拿掉最小边的边的集合, 如果新加入的边和vector的最大的两条边满足三角形, 则dp[i] = dp[i-1] + 1, 否则 dp[i] = dp[i-1], 同时将新边推入vector
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std; vector<int> V = { 1, 2, 3 }; vector<int>::iterator it; vector<int>::iterator it2; int res[22]; bool is_ok( int a, int b, int c ) { return a + b > c; } void table() { res[1] = res[2] = res[3] = 0; for( int i = 4; i <= 20; i++ ) { it = V.end(); it--; it2 = it; it2--; if( is_ok( *it2, *it, i ) ) { res[i] = res[i-1] + 1; } else { V.push_back( i ); res[i] = res[i-1]; } } } int main() { table(); int n; int t; while( ~scanf( "%d", &t ) ) { for( int i = 1; i <= t; i++ ) { scanf( "%d", &n ); printf( "Case #%d: %d\n", i, res[n] ); } } return 0; }
HDU 5916 Harmonic Value Description
将2K, K放在前两个元素, 再保证以后的gcd都是1, 所以依次排列k-1, k-2.......1, k+1, k+2.......跳过2K, 先不贴code, 有点bug(已解决)
bug在于输出的时候最后一个元素后面跟了一个空格, 以后记住输出数组元素之间有空格的时候要在前面加空格, 最后再换行
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; int res[10000+23]; int main() { int t; while( cin >> t ) { for( int i = 1; i <= t; i++ ) { int n, k; cin >> n >> k; res[0] = 2 * k; res[1] = k; int temp = k; int temp2 = k; for( int i = 2; i < n; i++ ) { if( --temp > 0 ) { res[i] = temp; } else { if( ++temp2 == 2 * k ) ++temp2; res[i] = temp2; } } printf( "Case #%d:", i ); for( int i = 0; i < n; i++ ) { cout << " " << res[i]; } cout << endl; } } }
HDU 5918 Sequence 1
正解是KMP, 不会就很无奈了, 用了二重循环强行水过
#include <cstdio> const int maxn = 1e6 + 20; int a[maxn]; int b[maxn]; int main() { int t; int cases = 1; scanf( "%d", &t ); while( t-- ) { int res = 0; int n, m, p; scanf( "%d%d%d", &n, &m, &p ); for( int i = 1; i <= n; i++ ) scanf( "%d", a+i ); for( int j = 1; j <= m; j++ ) scanf( "%d", b+j ); for( int i = 1; i <= n; i++ ) { if( a[i] != b[1] ) continue; int index = 1; for( int j = i; j <= n; j += p ) { if( a[j] == b[index] ) index++; else break; if( index == m+1 ) { res++; break; } } } printf( "Case #%d: %d\n", cases++, res ); } return 0; }
还有就是立个flag, 以后学习一定要来实验室, 今天下午算是废了,今天早上也没起来, 说好的人十我百呢, 别让自己看不起自己, 王朗