偶然原因开始了PAT之旅
前前后后用了3天时间刷完了PAT Basic(35题)
现在记录下从零单排的过程,总的来说题目很简单
当然有个别题目的坑点还是比较给力的,准确的说,题目的数据很给力
纯模拟题,是偶数除以2,奇数(3n+1)除以2,到1结束
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n, step; void scan( int& x ) { char c; while( c = getchar(), c < '0' || c > '9' ); x = c - '0'; while( c = getchar(), c >= '0' && c <= '9' ) x = x * 10 + c - '0'; } int main() { scan( n ); step = 0; while( n != 1 ) { if( n & 1 ) n = ( n * 3 + 1 ) >> 1; else n >>= 1; step++; } printf( "%d\n", step ); return 0; }
把一个数串各个数位上的数加起来,然后用给定格式输出
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char str[110]; int i, j; int res, flag; char du[15][5] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; void out( int x ) { if( x > 9 ) out( x/10 ); if( flag ) putchar( ' ' ); else flag = 1; printf( "%s", du[ x % 10 ] ); } int main() { gets(str); res = flag = 0; for( i = 0; str[i]; ++i ) res += str[i] - '0'; out( res ); return 0; }
xPyTz 满足X*Y=Z 的PAT串即为正确答案,否则为错误的串
如 AAPAAPAAAA 为正确的串
#include <cstdio> #include <cstring> using namespace std; int n; char s[ 110 ]; bool check( char* str ) { int len = strlen( str ); if( len < 3 ) return false; int cl = 0, cm = 0, cr = 0, cnt = 0; int fa = 0, fp = 0, ft = 0; for( int i = 0; i < len; ++i ) { if( str[i] == 'A' ) fa++; else if( str[i] == 'P' ) fp++; else if( str[i] == 'T' ) ft++; else return false; } if( fp != 1 || ft != 1 ) return false; while( str[ cnt++ ] != 'P' ) { cl++; if( cnt >= len ) return false; } while( str[ cnt++ ] != 'T' ) { cm++; if( cnt >= len ) return false; } while( str[ cnt++ ] == 'A' ) { cr++; if( cnt == len ) break; } if( cl * cm == cr ) return true; else return false; } int main() { scanf( "%d", &n ); while( n-- ) { scanf( "%s", s ); if( check( s )) puts( "YES" ); else puts( "NO" ); } return 0; }
简单排序
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct score { char name[15]; char id[15]; int score; }S[1000]; int cmp( score a, score b ) { if( a.score == b.score ) return strcmp( a.id , b.id ); return a.score > b.score; } int n; int main() { scanf( "%d", &n ); for( int i = 0; i < n; ++i ) scanf( "%s %s %d", &S[i].name, &S[i].id, &S[i].score ); sort( S, S + n, cmp ); printf( "%s %s\n%s %s\n", S[0].name, S[0].id, S[n-1].name, S[n-1].id ); return 0; }
Hash标记一下,over
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int H[1000000], n, x, flag, num[200] ; int main() { memset( H, 0 , sizeof( H ) ); scanf( "%d", &n ); for( int i = 0; i < n; ++i ) { scanf( "%d", &num[i] ); x = num[i]; while( x != 1 ) { if( x & 1 ) x = ( x * 3 + 1 ) >> 1; else x >>= 1; H[ x ] = 1; } } sort( num, num + n ); for( int i = n-1; i >= 0; --i ) { if( !H[ num[i] ] ) { if( flag ) putchar( ' ' ); else flag = 1; printf( "%d", num[i] ); } } putchar( '\n' ); return 0; }
题目要换格式你就照着换格式就好了
#include <cstdio> #include <cstring> using namespace std; int n, x, y, z; void doit() { scanf( "%d", &n ); x = n / 100, y = n % 100 /10 , z = n % 10; if( x ) while( x-- ) putchar( 'B' ); if( y ) while( y-- ) putchar( 'S' ); if( z ) { for( int i = 1; i <= z ; ++i ) printf( "%d", i ); } putchar( '\n' ); } int main() { doit(); return 0; }
打表+递推
#include <cstdio> #include <cstring> #include <cmath> using namespace std; #define MAXN 100000 int vis[ MAXN + 10 ], prime[ MAXN + 10 ], n; int d[ MAXN + 10], H[ MAXN + 10 ], res[ MAXN + 10 ], cnt; void init() { int m = sqrt( MAXN + 0.5 ); for( int i = 2; i <= m; ++i ) if( !vis[ i ] ) for( int j = i * i; j <= MAXN; j += i ) vis[ j ] = 1; cnt = 0; for( int i = 2; i <= MAXN; ++i ) { if( !vis[i] ) { prime[ cnt ] = i; H[ i ] = cnt; cnt++; } } for( int i = 1; i < cnt; ++i ) { if( prime[i] - prime[i-1] == 2 ) d[ i ] = 1; } for( int i = 1; i < cnt; ++i ) { res[i] = res[i-1] + d[i]; } } int main() { init(); scanf( "%d", &n ); while( vis[n] ) n--; printf( "%d\n", res[H[n]] ); return 0; }
简单水题,连接一下然后取模就是了
#include <cstdio> #include <cstring> using namespace std; int x[222], n, m; int main() { scanf( "%d %d", &n, &m ); for( int i = 0; i < n; ++i ) { scanf( "%d", &x[i] ); x[i+n] = x[i]; } int s = m % n; for( int i = n - s, j = 0; j < n; ++i, ++j ) { if( j != 0 ) putchar( ' ' ); printf( "%d", x[i] ); } return 0; }
那就说一次反话咯
#include <cstdio> #include <cstring> using namespace std; char str[88][88]; int main() { int ct = 0; while( ~scanf( "%s", str[ct++] ) ); printf( "%s", str[ ct - 2 ] ); for( int i = ct - 3; i >= 0; --i ) { printf( " %s", str[i] ); } putchar( '\n' ); return 0; }
简单求导,注意特判
#include <cstdio> #include <cstring> using namespace std; int s[1010][2], cnt = 0, x, y; int main() { while( ~scanf( "%d %d", &x, &y ) && y ) { s[cnt][0] = x; s[cnt++][1] = y; } if( cnt == 0 ) puts( "0 0" ); else { for( int i = 0; i < cnt; ++i ) { s[i][0] *= s[i][1]; s[i][1]--; if( i == 0 ) printf( "%d %d", s[i][0], s[i][1] ); else printf( " %d %d", s[i][0], s[i][1] ); } putchar( '\n' ); } return 0; }
水题。直接用long long 免除后顾之忧
#include <cstdio> #include <cstring> using namespace std; typedef long long LL; LL A, B, C; int n; int main() { scanf( "%d", &n ); for( int i = 1; i <= n; ++i ) { scanf( "%lld %lld %lld", &A, &B, &C ); printf( "Case #%d: ", i ); if( A + B > C ) puts( "true" ); else puts( "false" ); } return 0; }
按题意分类,有个double注意一下
#include <cstdio> #include <cstring> using namespace std; int x[1010], n; int res[5], flag[5]; double sum; int t = 1; void check( int key ) { if( key % 5 == 0 && key % 2 == 0 ) res[0] += key, flag[0] = 1; if( key % 5 == 1 ) { res[1] = res[1] + key * t; t = -t; flag[1] = 1; } if( key % 5 == 2 ) res[2]++, flag[2] = 1; if( key % 5 == 3 ) { res[3]++; sum += key; flag[3] = 1; } if( key % 5 == 4 ) if( key > res[4] ) res[4] = key, flag[4] = 1; } int main() { scanf( "%d", &n ); for( int i = 0; i < n; ++i ) { scanf( "%d", &x[i] ); check( x[i] ); } for(int i = 0; i < 5; ++i ) { if( i != 0 ) putchar( ' ' ); if( flag[i] ) { if( i == 3 ) printf( "%.1f", sum/res[i] ); else printf( "%d", res[i] ); } else putchar( 'N' ); } return 0; }
打表然后输出,注意第i的含义,是第i个素数,所有表打大一点
#include <cstdio> #include <cstring> #include <cmath> using namespace std; #define MAXN 2000000 int vis[ MAXN + 10 ], prime[ MAXN + 10 ]; int m, n, flag = 0, cnt, line = 0; void init() { int m = sqrt( MAXN + 0.5 ); for( int i = 2; i <= m; ++i ) if( !vis[ i ] ) for( int j = i * i; j <= MAXN; j += i ) vis[ j ] = 1; cnt = 0; for( int i = 2; i <= MAXN; ++i ) if( !vis[i] ) prime[ ++cnt ] = i; } int main() { init(); scanf( "%d %d", &n, &m ); for( int i = n; i <= m; ++i ) { if( line == 10 ) { putchar( '\n' ); line = 1; flag = 0; } else line++; if( flag ) putchar( ' ' ); else flag = 1; printf( "%d", prime[i] ); } putchar( '\n' ); }
注意先看清楚题目,然后按照题目意思来,注意输出格式
#include <cstdio> #include <cstring> using namespace std; char str[4][62]; int DAY, HH, MM; int i, j, ct = 0; char day[7][4] = {"MON","TUE","WED","THU","FRI","SAT","SUN"}; int main() { for( i = 0; i < 4; ++i ) scanf( "%s", str[i] ); for( i = 0; str[0][i] && str[1][i]; ++i ) { if( (str[0][i] <= 'G' && str[0][i] >= 'A' ) && ( str[1][i] == str[0][i] ) ) { DAY = str[1][i] - 'A'; break; } } for( i = i + 1; str[0][i] && str[1][i]; ++i ) { if( ( str[0][i] <= 'N' && str[0][i] >= 'A' ) || ( str[0][i] <= '9' && str[0][i] >= '0' ) ) { if( str[1][i] == str[0][i] ) { HH = str[1][i]; break; } } } for( i = 0; str[2][i] && str[3][i]; ++i ) { if( ( str[2][i] <= 'z' && str[2][i] >= 'a' ) || ( str[2][i] <= 'Z' && str[2][i] >= 'A' ) ) { if( str[2][i] == str[3][i] ) { MM = i; break; } } } printf( "%s ",day[ DAY ] ); if( HH > '9') printf( "%d", HH - 'A' + 10 ); else printf( "0%d", HH - '0' ); if( MM >= 10 ) printf( ":%d\n", MM ); else printf( ":0%d\n", MM ); return 0; }
分个类,排个序
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define MAXN 100000 struct node { int id; int ds; int cs; }; bool cmp( node x, node y ) { if( y.ds + y.cs == x.ds + x.cs ) { if( x.ds == y.ds ) return x.id < y.id; return x.ds > y.ds; } return x.ds + x.cs > y.ds + y.cs; } node A[MAXN], B[MAXN], C[MAXN], D[MAXN]; int N, L, H; int x, y, z, ct = 0; int ca, cb, cc, cd; int main() { scanf( "%d %d %d", &N, &L, &H ); ca = cb = cc = cd = 0; for( int i = 0; i < N; ++i ) { scanf( "%d %d %d", &x, &y, &z ); if( y < L || z < L ) continue; if( y >= H && z >= H ) { A[ca].id = x; A[ca].ds = y; A[ca++].cs = z; } else if( y >= H && z < H ) { B[cb].id = x; B[cb].ds = y; B[cb++].cs = z; } else if( y < H && z < H && y >= z ) { C[cc].id = x; C[cc].ds = y; C[cc++].cs = z; } else { D[cd].id = x; D[cd].ds = y; D[cd++].cs = z; } ct++; } sort( A, A + ca, cmp ); sort( B, B + cb, cmp ); sort( C, C + cc, cmp ); sort( D, D + cd, cmp ); printf( "%d\n", ct ); for( int i = 0 ; i < ca; ++i ) printf( "%d %d %d\n", A[i].id, A[i].ds, A[i].cs ); for( int i = 0 ; i < cb; ++i ) printf( "%d %d %d\n", B[i].id, B[i].ds, B[i].cs ); for( int i = 0 ; i < cc; ++i ) printf( "%d %d %d\n", C[i].id, C[i].ds, C[i].cs ); for( int i = 0 ; i < cd; ++i ) printf( "%d %d %d\n", D[i].id, D[i].ds, D[i].cs ); return 0; }
简单水题
#include <cstdio> #include <cstring> using namespace std; char s1[20], s2[20]; int a, b, x, y; int main() { scanf( "%s %d %s %d", s1, &a, s2, &b ); x = y = 0; for( int i = 0 ; s1[i]; ++i ) { if( s1[i] - '0' == a ) x = x * 10 + a; } for( int i = 0 ; s2[i]; ++i ) { if( s2[i] - '0' == b ) y = y * 10 + b; } printf( "%d\n", x + y ); return 0; }
模拟除法
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s[1010]; char r[1010]; int v, m, ct, i, len; int main() { scanf( "%s %d", s, &m ); v = ct = 0; len = strlen( s ); while( v < m ) { v = v*10 + s[i] - '0'; i++; if( i >= len ) { printf( "0 %d", v ); return 0; } } for( ; i < len; ++i ) { r[ct++] = v/m + '0'; v = v%m; v = v*10 + s[i] - '0'; } r[ct++] = v/m + '0'; v = v%m; printf( "%s %d", r, v ); return 0; }
模拟题,注意标记
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char A, B; int n; int W[2], E; struct node { int ct; char c; bool operator < ( const node b ) const { if( ct == b.ct ) return c < b.c; return ct > b.ct; } }NA[3], NB[3]; int main() { NA[0].c = NB[0].c = 'C'; NA[1].c = NB[1].c = 'J'; NA[2].c = NB[2].c = 'B'; scanf( "%d", &n ); while( n-- ) { getchar(); scanf( "%c %c", &A, &B ); if( A == B ) E++; else if( A == 'C' && B == 'J' ) W[0]++, NA[0].ct++; else if( A == 'J' && B == 'B' ) W[0]++, NA[1].ct++; else if( A == 'B' && B == 'C' ) W[0]++, NA[2].ct++; else if( B == 'C' && A == 'J' ) W[1]++, NB[0].ct++; else if( B == 'J' && A == 'B' ) W[1]++, NB[1].ct++; else if( B == 'B' && A == 'C' ) W[1]++, NB[2].ct++; } sort( NA, NA + 3 ); sort( NB, NB + 3 ); printf( "%d %d %d\n", W[0], E, W[1] ); printf( "%d %d %d\n", W[1], E, W[0] ); printf( "%c %c\n", NA[0].c, NB[0].c ); return 0; }
简单模拟,注意字符串和数值的转换
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s1[5], s2[5], tp[5]; int x, y, z, num; void tran( int x, char tar[]) { tar[0] = x/1000 + '0', x %= 1000; tar[1] = x/100 + '0', x %= 100; tar[2] = x/10 + '0'; x %= 10; tar[3] = x + '0'; } void rev( char src[], char tar[] ) { int i, j; for( i = 3, j = 0; i >= 0; --i, ++j ) tar[j] = src[i]; } bool cmp( char x, char y ) { return x > y; } int main() { scanf( "%d", &num ); tran( num, s1 ); if( s1[0] == s1[1] && s1[1] == s1[2] && s1[2] == s1[3] ) printf( "%s - %s = 0000\n", s1, s1 ); else { while( true ) { sort( s1, s1 + 4, cmp ); rev( s1, s2 ); z = x = y = 0; for( int i = 0; i < 4; ++i ) { x = x * 10 + s1[i] - '0'; y = y * 10 + s2[i] - '0'; } z = x - y; tran( z, tp ); printf( "%s - %s = %s\n", s1, s2, tp ); if( z == 6174 ) { break; } else { tran( z, s1 ); rev( s1, s2 ); } } } return 0; }
简单贪心
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct node { double tot; double val; double bit; }E[1010]; bool cmp( node x, node y ) { return x.bit > y.bit; } int n, m; double res; int main() { scanf( "%d %d", &n, &m ); for( int i = 0; i < n; ++i ) scanf( "%lf", &E[i].tot ); for( int i = 0; i < n; ++i ) { scanf( "%lf", &E[i].val ); E[i].bit = E[i].val / E[i].tot; } sort( E, E + n, cmp ); res = 0; for( int i = 0; i < n; ++i ) { if( E[i].tot < m ) { m -= E[i].tot; res += E[i].val; } else { res += E[i].bit * m; break; } } printf( "%.2f\n", res ); return 0; }
水题
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int D[10]; char str[1010]; int main() { scanf( "%s", str ); for( int i = 0; str[i]; ++i ) D[ str[i] - '0' ]++; for( int i = 0; i < 10; ++i ) { if( D[i] != 0 ) printf( "%d:%d\n", i, D[i] ); } return 0; }
进制转换,特判下0
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int A, B, C, D, ct = 0; char res[ 100 ]; int main() { scanf( "%d %d %d", &A, &B, &D ); C = A + B; while( C ) { res[ct++] = C % D + '0'; C /= D; } if( ct == 0 ) putchar( '0' ); else { for( int i = ct-1; i >=0 ; --i ) printf( "%c", res[i] ); putchar( '\n' ); } return 0; }
先找不是0的最小数,然后再由小到大排列
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int nc[10], t; int main() { for( int i = 0; i < 10 ; ++i ) scanf( "%d", &nc[i] ); t = 1; while( nc[t] == 0 ) t++; printf( "%d", t ); nc[t]--; for( int i = 0; i < 10; ++i ) { for( int j = 0; j < nc[i]; ++j ) printf( "%d", i ); } putchar( '\n' ); return 0; }
模拟,这题我代码写得有点撮
#include <cstdio> #include <cstring> char str[ 10010 ]; char res[ 10010 ]; int main() { scanf( "%s", str ); if( str[0] == '-' ) putchar( '-' ); int t = strlen( str ) - 1; int flag = 0; while( str[t] != 'E' ) t--; int ct = 0 ; for( int i = t + 2; str[i]; ++i ) { ct = ct * 10 + str[i] - '0'; } if( str[t+1] == '-' ) flag = 1; if( flag ) { if( ct > 0 ) { printf( "0." ); for( int i = 1; i < ct; ++i ) printf( "0" ); for( int i = 1; i < t; ++i ) { if( str[i] != '.' ) printf( "%c", str[i] ); } } else { printf( "%c.", str[1] ); for( int i = 2; i < t; ++i ) { if( str[i] != '.' ) printf( "%c", str[i] ); } } } else { int len = t - 3; if( ct >= len ) { for( int i = 1; i < t; ++i ) { if( str[i] != '.' ) printf( "%c", str[i] ); } for( int i = 0; i < ct - len; ++i ) printf( "0" ); } else if( ct == 0 ) { for( int i = 1; i < t; ++i ) printf( "%c", str[i] ); } else { for( int i = 1; i < t; ++i ) { if( str[i] != '.' ) printf( "%c", str[i] ); if( i == t - ct ) printf( "." ); } } } putchar( '\n' ); return 0; }
35道题目里面最难的3题之一,模拟链表
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct node { int addr; int data; int next; }D[ 100010 ], LIST[ 100010 ]; int start, n, k, add; int main() { scanf( "%d %d %d", &start, &n, &k ); for( int i = 0; i < n; ++i ) { scanf( "%d", &add ); D[add].addr = add; scanf( "%d %d", &D[add].data, &D[add].next ); } int ct = 0; while( start != -1 ) { LIST[ct].addr = D[start].addr; LIST[ct].data = D[start].data; LIST[ct].next = D[start].next; start = D[start].next; ct++; } int be = 0, ed = k - 1; while( ed < ct ) { while( ed > be ) { LIST[ed].next = LIST[ed - 1].addr; printf( "%05d %d ", LIST[ed].addr, LIST[ed].data ); if( LIST[ed].next != -1 ) printf( "%05d\n", LIST[ed].next ); else printf( "-1\n" ); ed--; } if( be + 2 * k - 1 < ct ) LIST[be].next = LIST[be + 2 * k - 1].addr; else if( be + k == ct ) LIST[be].next = -1; else LIST[be].next = LIST[be + k].addr; printf( "%05d %d ", LIST[be].addr, LIST[be].data ); if( LIST[be].next != -1 ) printf( "%05d\n", LIST[be].next ); else printf( "%d\n", LIST[be].next ); be += k; ed = be + k - 1; } while( be < ct ) { printf( "%05d %d ", LIST[be].addr, LIST[be].data ); if( LIST[be].next != -1 ) printf( "%05d\n", LIST[be].next ); else printf( "-1\n" ); be++; } return 0; }
四舍五入一下,然后按格式输出即可
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int C1, C2, t; int H, M, S; int main() { scanf( "%d %d", &C1, &C2 ); t = (double)(C2 - C1)/100 + 0.5; H = t / 3600; t %= 3600; M = t/60; t %= 60; S = t; if( H < 10 ) printf( "0%d:", H ); else printf( "%d:", H ); if( M < 10 ) printf( "0%d:", M ); else printf( "%d:", M ); if( S < 10 ) printf( "0%d\n", S ); else printf( "%d\n", S ); return 0; }
简单题,但是注意输出的格式,容易格式错误
#include <cstdio> #include <cstring> #include <cmath> using namespace std; char ch; int n, res, line; int main() { scanf( "%d %c", &n, &ch ); line = sqrt( (n + 1) / 2 ); res = n - line * line * 2 + 1; for( int i = line; i > 0; --i ) { for( int k = line - i; k > 0; --k ) putchar( ' ' ); for( int j = 2*i-1; j > 0; --j ) putchar( ch ); putchar( '\n' ); } for( int i = 2; i <= line ; ++i ) { for( int k = line - i; k > 0; --k ) putchar( ' ' ); for( int j = 2*i-1; j > 0; --j ) putchar( ch ); putchar( '\n' ); } printf( "%d\n", res ); return 0; }
此题略坑,,如果没有人,只输出0,注意特判
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct people { char name[6]; int year; int mon; int day; }P[100010]; bool cmp( people x, people y ) { if( x.year == y.year ) { if( x.mon == y.mon ) return x.day < y.day; else return x. mon < y.mon; } else return x. year < y.year; } char s[6]; int y, m, d, n, ct = 0; int main() { scanf( "%d", &n ); for( int i = 0; i < n; ++i ) { scanf( "%s %d/%d/%d", s, &y, &m, &d ); if( y >= 2015 ) continue; else if( y == 2014 && m > 9 ) continue; else if( y == 2014 && m == 9 && d > 6 ) continue; else if( y < 1814 ) continue; else if( y == 1814 && m < 9 ) continue; else if( y == 1814 && m == 9 && d < 6 ) continue; else { strcpy( P[ct].name, s ); P[ct].year = y; P[ct].mon = m; P[ct++].day = d; } } sort( P, P + ct, cmp ); if( ct ) printf( "%d %s %s\n", ct, P[0].name, P[ct-1].name ); else printf( "%d\n", ct ); return 0; }
注意大小写的hash标记
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s1[100], s2[100]; int H[ 256 ], ct = 0; void check() { if( !H[ s1[ct] ] ) { if( s1[ct] >= 'A' && s1[ct] <= 'Z' ) { putchar( s1[ct] ); H[ s1[ct] ] = 1; H[ s1[ct] - 'A' + 'a' ] = 1; } else if( s1[ct] >= 'a' && s1[ct] <= 'z' ) { putchar( s1[ct] - 'a' + 'A' ); H[ s1[ct] - 'a' + 'A' ] = 1; H[ s1[ct] ] = 1; } else { putchar( s1[ct] ); H[ s1[ct] ] = 1; } } } int main() { scanf( "%s %s", s1, s2 ); for( int i = 0; s2[i]; ++i ) { for( ; s1[ct]; ++ct ) { if( s1[ct] != s2[i] ) { check(); } else { ct++; break; } } } while( s1[ct] ) { check(); ct++; } putchar( '\n' ); return 0; }
注意优化,小心超时,注意数据范围
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; long long x[ 100010 ], M, p; int n, flag = 0, t, maxx = 0; int main() { scanf( "%d %lld", &n, &p ); for( int i = 0; i < n; ++i ) scanf( "%lld", &x[i] ); sort( x, x + n ); for( int i = 0; i < n; ++i ) { for( int j = i + maxx - 1; j < n; ++j ) { if( j < 0 ) continue; if( x[i]*p < x[j] ) break; if( j - i + 1 > maxx ) maxx = j - i + 1; } } printf( "%d\n", maxx ); return 0; }
按题意模拟即可
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n, sum = 0, ct = 0, flag = 0; char s[20]; char M[15] = {"10X98765432"}; int V[20] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; int main() { scanf( "%d", &n ); for( int i = 0; i < n; ++i ) { scanf( "%s", s ); flag = sum = 0; for( int i = 0; i < 17; ++i ) { if( s[i] > '9' || s[i] < '0' ) { flag = 1; break; } sum += ( s[i] - '0' )*V[i]; } if( flag ) { puts( s ); } else { sum %= 11; if( M[sum] == s[17] ) ct++; else puts( s ); } } if( ct == n ) puts( "All passed" ); }
简单水题
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int x[ 100010 ], n; int H[ 100010 ], sch, sco, res = 0, tot = 0; int main() { scanf( "%d", &n ); for( int i = 0; i < n; ++i ) { scanf( "%d %d", &sch, &sco ); x[ sch ] += sco; if( !H[ sch] ) { tot++; H[ sch ] = 1; } } for( int i = 1; i <= tot; ++i ) { if( res < x[i] ) { res = x[i]; sch = i; } } printf( "%d %d\n", sch, x[sch] ); return 0; }
这题需要用gets(),有空格,貌似题目没有说清楚啊。。。。这个是坑
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; char s1[ 256 ], s2[ 100010 ]; int H[ 256 ], shift = 0; int main() { gets( s1 ); gets( s2 ); for( int i = 0; s1[i]; ++i ) { if( s1[i] >= 'A' && s1[i] <= 'Z' ) { H[ s1[i] ] = 1; H[ s1[i] - 'A' + 'a' ] = 1; } else if( s1[i] == '+' ) { shift = 1; } else { H[ s1[i] ] = 1; } } for( int i = 0; s2[i]; ++i ) { if( s2[i] >= 'A' && s2[i] <= 'Z' && shift ) continue; if( H[ s2[i] ] ) continue; putchar( s2[i] ); } putchar( '\n' ); return 0; }
注意细节。先写好框架然后再调bug
题目保证正确的输出中没有超过整型范围的整数。但是不一定计算过程中没有超过,要小
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; LL a1, a2, b1, b2, A, B; LL ABS( LL x ) { if( x < 0 ) return -x; return x; } LL gcd( LL a, LL b) { return b == 0 ? a : gcd( b, a % b ); } void out( LL x, LL y ) { LL GCD = gcd( x, y ); x /= GCD; y /= GCD; if( x * y < 0 ) { printf( "(" ); if( ( ( x / y >= 1 ) || ( x / y <= -1 ) ) && ( x % y == 0 ) ) printf( "%lld", x / y ); else if( x / y >= 1 || x / y <= -1 ) printf( "%lld %lld/%lld", x / y, ABS( x % y ), ABS( y ) ); else printf( "%lld/%lld", -ABS( x ) , ABS( y ) ); printf( ")" ); } else { if( x == 0 ) printf( "0" ); else if( ( ( x / y >= 1 ) || ( x / y <= -1 ) ) && ( x % y == 0 ) ) printf( "%lld", x / y ); else if( x / y >= 1 || x / y <= -1 ) printf( "%lld %lld/%lld", x / y, x % y, y ); else printf( "%lld/%lld", x , y ); } } void calc( LL a1, LL b1, LL a2, LL b2, char op ) { switch( op ) { case '+': A = a1 * b2 + a2 * b1; B = b1 * b2; out( a1, b1 ); printf( " + " ); out( a2, b2 ); printf( " = " ); out( A, B ); putchar( '\n' ); break; case '-': A = a1 * b2 - a2 * b1; B = b1 * b2; out( a1, b1 ); printf( " - " ); out( a2, b2 ); printf( " = " ); out( A, B ); putchar( '\n' ); break; case '*': A = a1 * a2; B = b1 * b2; out( a1, b1 ); printf( " * " ); out( a2, b2 ); printf( " = " ); out( A, B ); putchar( '\n' ); break; case '/': A = a1 * b2; B = a2 * b1; out( a1, b1 ); printf( " / " ); if( a2 == 0 ) { printf( "0 = Inf\n"); } else { out( a2, b2 ); printf( " = " ); out( A, B ); putchar( '\n' ); } break; } } int main() { scanf( "%lld/%lld %lld/%lld", &a1, &b1, &a2, &b2 ); calc( a1, b1, a2, b2, '+' ); calc( a1, b1, a2, b2, '-' ); calc( a1, b1, a2, b2, '*' ); calc( a1, b1, a2, b2, '/' ); return 0; }
排序排一轮就比一次。
#include <cstdio> #include <cstring> using namespace std; int x1 [ 110 ], x2[ 110 ], tar[ 110 ], buf[ 220 ], n; void scan( int& x ) { char c; while( c = getchar(), c < '0' || c > '9' ); x = c - '0'; while( c = getchar(), c >= '0' && c <= '9' ) x = x * 10 + c - '0'; } void Merge_Sort( int arr[], int n ) { int flag = 0, j; int step, lbe, led, rbe, red; for( step = 1; step < n; step <<= 1 ) { for( lbe = 0; lbe < n - step; lbe = red + 1 ) { rbe = lbe + step; led = rbe - 1; red = led + step; if( red > n - 1 ) red = n - 1; int k = 0; memset( buf, 0, sizeof(buf) ); while( lbe <= led && rbe <= red ) { if( arr[lbe] < arr[rbe] ) buf[k++] = arr[lbe++]; else buf[k++] = arr[rbe++]; } while( lbe <= led ) buf[k++] = arr[lbe++]; while( rbe <= red ) buf[k++] = arr[rbe++]; while( k > 0 ) arr[ --rbe ] = buf[ --k ]; } if( flag ) { printf( "Merge Sort\n" ); for( j = 0; j < n; ++j ) { if( j != 0 ) putchar( ' ' ); printf( "%d", arr[j] ); } putchar( '\n' ); return; } for( j = 0; j < n; ++j ) { if( arr[j] != tar[j] ) break; } if( j == n ) flag = 1; } } bool Insert_Sort( int arr[], int n ) { int b, e, t, i, flag = 0; for( b = 1; b < n; ++b ) { t = arr[b]; e = b; while( e > 0 && arr[ e - 1 ] > t ) { arr[ e ] = arr[ e - 1 ]; --e; } arr[ e ] = t; for( i = 0; i < n; ++i ) { if( arr[i] != tar[i] ) break; } if( flag ) { printf( "Insertion Sort\n" ); for( i = 0; i < n; ++ i ) { if( i != 0 ) putchar( ' ' ); printf( "%d", arr[i] ); } putchar( '\n' ); return true; } if( i == n ) flag = 1; } return false; } int main() { scan( n ); for( int i = 0; i < n; ++i ) { scan( x1[i] ); x2[i] = x1[i]; } for( int i = 0; i < n; ++i ) scan( tar[i] ); if( !Insert_Sort( x1, n ) ) Merge_Sort( x2, n ); return 0; }