Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴一下我的代码(或许对因处理边界而烦恼的朋友们有帮助)#include <stdio.h> #include <string.h> __int64 getnum (int i, char c) { if (c == 'p') return ((__int64)1) << i; else return -(((__int64)1) << i); } int main () { int T, l, i, weight[100], ok; __int64 n, tmp; char pn[100], ans[100]; scanf ("%d", &T); while (T--) { scanf ("%d%s%I64d", &l, pn, &n); tmp = 0; memset (ans, '0', sizeof (ans[0]) * l); ans[l] = 0; ok = 1; for (i = 0; i < l; i++) { ans[l - i - 1] = '0'; __int64 t = getnum (i, pn[l - i - 1]); if (((tmp + t) >> i & 1) == (n >> i & 1)) { if (((tmp >> 2) + (t >> 2)) >> 62 > 0) { ok = 0; break; } ans[l - i - 1] = '1'; tmp += t; } if (tmp == n) break; } if (tmp == n && ok && !(l == 64 && ans[0] == '1')) printf ("%s\n", ans); else printf ("Impossible\n"); } } //处理边界的方法是我在发呆的时候想到的... Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator