題意:將一個含K(K爲偶數)個數字的整數Z割分爲A和B兩部分,若Z能被A*B整除,則輸出Yes,不然輸出No。ios
分析:當A*B爲0的時候,不能被Z整除,輸出No。不然會出現浮點錯誤。優化
#include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<map> #include<iostream> #include<vector> #include<set> #include<cmath> using namespace std; char s[20]; int main(){ int N; scanf("%d", &N); while(N--){ scanf("%s", s); int len = strlen(s); int A = 0; for(int i = 0; i < len / 2; ++i){ A = A * 10 + s[i] - '0'; } int B = 0; for(int i = len / 2; i < len; ++i){ B = B * 10 + s[i] - '0'; } int C = A * B; if(C == 0){ printf("No\n"); continue; } int x = atoi(s); if(x % C == 0) printf("Yes\n"); else printf("No\n"); } return 0; }
1133 Splitting A Linked List(25 分)ui
題意:給定一個鏈表,將鏈表從新排序,在不打亂原鏈表相對順序的前提下,小於0的在最前面,其次是0~K,最後是大於K的數。spa
分析:blog
一、3次遍歷可實現鏈表重排。排序
二、map映射value和pre或suc的關係會超時,因此,以pre爲結點定義結構體,組織鏈表的重排,從而進行優化。遞歸
#include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<map> #include<iostream> #include<vector> #include<set> #include<cmath> using namespace std; const int MAXN = 100000 + 10; struct Node{ int pre, value, suc; }num[MAXN]; vector<int> old, ans; int main(){ int N, K, head, pre, value, suc; scanf("%d%d%d", &head, &N, &K); for(int i = 0; i < N; ++i){ scanf("%d%d%d", &pre, &value, &suc); num[pre].pre = pre; num[pre].value = value; num[pre].suc = suc; } while(head != -1){ old.push_back(head); head = num[head].suc; } int len = old.size(); for(int i = 0; i < len; ++i){ if(num[old[i]].value < 0) ans.push_back(old[i]); } for(int i = 0; i < len; ++i){ if(num[old[i]].value >= 0 && num[old[i]].value <= K) ans.push_back(old[i]); } for(int i = 0; i < len; ++i){ if(num[old[i]].value > K) ans.push_back(old[i]); } for(int i = 0; i < len - 1; ++i){ printf("%05d %d %05d\n", ans[i], num[ans[i]].value, ans[i + 1]); } printf("%05d %d -1\n", ans[len - 1], num[ans[len - 1]].value); return 0; }
題意:vertex cover是指圖中一些點的集合,使得圖中每一條邊的兩個點中都至少有一個點在該點集中。給定點的集合,判斷是否爲vertex cover。string
分析:按題意模擬便可。it
#include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<map> #include<iostream> #include<vector> #include<set> #include<cmath> using namespace std; const int MAXN = 10000 + 10; int N, M; bool vis[MAXN]; struct Edge{ int u, v; void read(){ scanf("%d%d", &u, &v); } }num[MAXN]; int main(){ scanf("%d%d", &N, &M); for(int i = 0; i < M; ++i){ num[i].read(); } int K; scanf("%d", &K); while(K--){ int n, x; scanf("%d", &n); memset(vis, false, sizeof vis); while(n--){ scanf("%d", &x); vis[x] = true; } bool ok = true; for(int i = 0; i < M; ++i){ if(vis[num[i].u] || vis[num[i].v]) continue; ok = false; break; } if(ok) printf("Yes\n"); else printf("No\n"); } return 0; }
1135 Is It A Red-Black Tree(30 分io
題意:給定一棵二叉搜索樹的前序遍歷序列,判斷其是否爲一棵紅黑樹。
分析:
一、紅黑樹是一棵平衡的二叉搜索樹,知足如下條件:
(1)全部結點不是紅色就是黑色;
(2)根結點是黑色;
(3)每個爲NULL的葉子結點是黑色;
(4)若是某結點是紅色,其左右子結點都是黑色;
(5)對於每一個結點,其到全部後代葉子結點通過的黑色結點數相同;
二、根據給定的前序遍歷序列,結合二叉搜索樹的定義能夠建樹。
三、遞歸檢查條件4。
四、同理,遞歸統計左右子樹的黑色結點數,來檢查條件5。
#include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<map> #include<iostream> #include<vector> #include<set> #include<cmath> using namespace std; const int MAXN = 30 + 10; struct Node{ Node *left, *right; int value; }; struct Node *root; bool ok; void build(Node* &r, int x){ if(r == NULL){ r = (Node*)malloc(sizeof(Node)); r -> value = x; r -> left = r -> right = NULL; return; } if(abs(x) < abs(r -> value)){ build(r -> left, x); } else{ build(r -> right, x); } } void judge_RedNode(Node* r){ if(!ok) return; if(r -> left != NULL){ if(r -> value < 0 && r -> left -> value < 0){ ok = false; return; } else judge_RedNode(r -> left); } if(r -> right != NULL){ if(r -> value < 0 && r -> right -> value < 0){ ok = false; return; } else judge_RedNode(r -> right); } } int judge_BlackNode(Node* r){ int leftcnt, rightcnt; if(!ok) return -1; if(r == NULL) return 1; leftcnt = judge_BlackNode(r -> left); rightcnt = judge_BlackNode(r -> right); if(leftcnt != rightcnt){ ok = false; return -1; } else{ if(r -> value > 0) ++leftcnt; } return leftcnt; } int main(){ int K; scanf("%d", &K); while(K--){ root = NULL; int N, x; scanf("%d", &N); while(N--){ scanf("%d", &x); build(root, x); } ok = true; judge_RedNode(root); judge_BlackNode(root); if(root -> value < 0 || !ok){ printf("No\n"); } else{ printf("Yes\n"); } } return 0; }