Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1. Then N lines follow, each describes a node in the format: Address Data Next where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1. Then N lines follow, each describes a node in the format: Address Key Next where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=1000). The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1. Then N lines follow, each describes a node in the format: Address Data Next where Address is the position of the node, Data is an integer in [-105, 105], and Next is the position of the next node. It is guaranteed that the list is not empty.
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
voidspit(node *nodes, int first, int k){ while (first != -1) { if (nodes[first].value < 0) { neg.push_back(nodes[first]); } elseif (nodes[first].value <= k) { lesser.push_back(nodes[first]); } else { greater.push_back(nodes[first]); } first = nodes[first].next; } }
vector<node> ans;
voidmerge(){ ans = neg; for (int i = 0; i < lesser.size(); i++) { ans.push_back(lesser[i]); } for (int i = 0; i < greater.size(); i++) { ans.push_back(greater[i]); } }
voidprintAns(){ int i = 0; for (; i < ans.size() - 1; i++) { printf("%05d %d %05d\n", ans[i].first, ans[i].value, ans[i + 1].first); } printf("%05d %d -1\n", ans[i].first, ans[i].value); }
intmain(){ int first = 0, n = 0, k = 0, temp = 0; scanf("%d %d %d", &first, &n, &k); node nodes[100000]; for (int i = 0; i < n; i++) { scanf("%d", &temp); scanf("%d %d", &nodes[temp].value, &nodes[temp].next); nodes[temp].first = temp; } spit(nodes, first, k); merge(); printAns();
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devided by the product of A and B, as 167334 / (167 x 334) = 3. Given an integer Z, you are supposed to test if it is such an integer.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<= 20). Then N lines follow, each gives an integer Z (10<=Z<=231). It is guaranteed that the number of digits of Z is an even number.
Output Specification:
For each case, print a single line “Yes” if it is such a number, or “No” if not.
The task is simple: given any positive integer N, you are supposed to count the total number of 1’s in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1’s in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (<=230).
Output Specification:
For each test case, print the number of 1’s in one line.
intmain(){ int n = 0, cur = 1, left = 0, right = 0, now = 0, ans = 0; scanf("%d", &n); while (n / cur != 0) { left = n / (cur * 10); right = n % cur; now = n % (cur * 10) / cur; if (now == 0) { ans += left * cur; } elseif (now == 1) { ans += left * cur + right + 1; } else { ans += (left + 1) * cur; } cur *= 10; } printf("%d\n", ans); }
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,…N. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print “The maximum travel distance = X” where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
intfindLower(int cur, int cmax, int davg){ for (int i = cur + 1; i < gas.size() && gas[i].dis - gas[cur].dis <= cmax * davg; i++) { if (gas[cur].price >= gas[i].price) { return i; } } return-1; }
intfindMin(int cur, int cmax, int davg){ int inf = 999999, min = 0; for (int i = cur + 1; i < gas.size() && gas[i].dis - gas[cur].dis <= cmax * davg; i++) { if (gas[i].price < inf) { min = i; inf = gas[i].price; } } if (inf == 999999) return-1; elsereturn min; }
intmain(){ int n = 0, cur = 0; double cmax = 0, d = 0, davg = 0, p = 0, temp = 0, tank = 0, total = 0; scanf("%lf %lf %lf %d", &cmax, &d, &davg, &n);
for (int i = 0; i < n; i++) { scanf("%lf %lf", &p, &temp); if (temp == d) gas.push_back(sta(temp, 0)); else gas.push_back(sta(temp, p)); }
sort(gas.begin(), gas.end(), cmp);
if (gas[0].dis != 0) { printf("The maximum travel distance = 0.00"); return0; }
while (true) { int low = findLower(cur, cmax, davg); if (low == -1) { low = findMin(cur, cmax, davg); if (low == -1) { break; } else { total += (cmax - tank) * gas[cur].price; tank = cmax - (gas[low].dis - gas[cur].dis) * 1.0 / davg; } } else { if ((gas[low].dis - gas[cur].dis) * 1.0 / davg >= tank) { total += gas[cur].price * ((gas[low].dis - gas[cur].dis) * 1.0 / davg - tank); tank = 0; } else { tank -= (gas[low].dis - gas[cur].dis) * 1.0 / davg; } } cur = low; }
structItem { int score, unit, t; char *choose = newchar[6]; };
boolequal(conststruct Item &a, conststruct Item &b){ if (a.t != b.t) returnfalse; for (int i = 0; i < a.t; i++) { if (a.choose[i] != b.choose[i]) returnfalse; } returntrue; }
intmain(){ int n = 0, m = 0; scanf("%d %d", &n, &m); structItem *items = newstruct Item[m]; for (int i = 0; i < m; i++) { scanf("%d %d %d", &items[i].score, &items[i].unit, &items[i].t); for (int j = 0; j < items[i].t; j++) { scanf(" %c", &items[i].choose[j]); } }
int *error = newint[m]; int maxValue = -1; for (int i = 0; i < n; i++) { int score = 0; for (int j = 0; j < m; j++) { structItem s; char c = getchar(); c = getchar(); scanf("%d", &s.t); for (int k = 0; k < s.t; k++) { c = getchar(); scanf("%c", &s.choose[k]); } c = getchar(); if (equal(items[j], s)) score += items[j].score; else { error[j]++; if (error[j] > maxValue) maxValue = error[j]; } } printf("%d\n", score); }
if (maxValue == -1) { printf("Too simple"); } else { printf("%d", maxValue); for (int i = 0; i < m; i++) { if (error[i] == maxValue) { printf(" %d", i + 1); } } } delete[] items; delete[] error; return0; }
intmain(){ char s[100010]; gets(s); int sum = 0; for (int i = 0; i < strlen(s); i++) { if (isupper(s[i])) { sum += s[i] - 'A' + 1; } elseif (islower(s[i])) { sum += s[i] - 'a' + 1; } } int one = 0, zero = 0; while (sum != 0) { one += sum % 2; zero += sum % 2 == 0; sum /= 2; }; printf("%d %d", zero, one); return0; }
intmain(){ int n = 0, sum = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { int t = 0; scanf("%d", &t); sum += (t * 10 + t) * (n - 1); } printf("%d", sum); return0; }
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique. Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first printf in a line “Yes” if the tree is unique, or “No” if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
usingnamespace std; vector<int> ans; int *pre, *post, unique = 1;
intfindFromPre(int x, int l, int r){ for (int i = l; i <= r; i++) { if (x == pre[i]) { return i; } } return-1; }
voidsetIn(int prel, int prer, int postl, int postr){ if (prel == prer) { ans.push_back(pre[prel]); return; } if (pre[prel] == post[postr]) { int x = findFromPre(post[postr - 1], prel + 1, prer); if (x - prel > 1) { setIn(prel + 1, x - 1, postl, postl + x - prel - 2); ans.push_back(post[postr]); setIn(x, prer, postl + x - prel - 2 + 1, postr - 1); } else { unique = 0; ans.push_back(post[postr]); setIn(x, prer, postl + x - prel - 2 + 1, postr - 1); } } }
intmain(){ int n = 0; scanf("%d", &n); pre = newint[n]; post = newint[n]; for (int i = 0; i < n; i++) { scanf("%d", &pre[i]); } for (int i = 0; i < n; i++) { scanf("%d", &post[i]); } setIn(0, n - 1, 0, n - 1); printf("%s\n", unique ? "Yes" : "No"); printf("%d", ans[0]); for (int i = 1; i < ans.size(); i++) { printf(" %d", ans[i]); } printf("\n"); return0; }