intmain() { int l, m; // 读入每次的 l, m while (scanf("%d %d", &l, &m) != EOF) { if (l == 0 && m == 0) break; int *tree_num = newint[l + 1]; // 赋初值为 1 // fill(first, last, value); fill(tree_num, tree_num + l + 1, 1); int start, end; // 有树:1 无树:0 while (m--) { scanf("%d %d", &start, &end); for (int i = start; i <= end; i++) { tree_num[i] = 0; } } // 计算剩余树的个数( 1 的个数) int trees = 0; for (int i = 0; i <= l + 1; i++) { if (tree_num[i] == 1) { trees++; } } printf("%d\n", trees); }
system("pause"); return0; }
问题 B: A+B
题目
题目描述
给定两个整数 A 和 B,其表示形式是:从个位开始,每三位数用逗号 ","
隔开。 现在请计算 A+B 的结果,并以正常形式输出。
// char[] 数组转化为 long long 整型 voidsToNum(char s[], longlong &n) { // r为阶数 int i = strlen(s), r = 0; while (i >= 0) { if (s[i] >= '0' && s[i] <= '9') { n += (s[i] - '0') * pow(10, r++); } i--; } // 判断负数 if (s[0] == '-') { n = -n; } }
intmain() { char A[15], B[15]; while (scanf("%s %s", A, B) != EOF) { longlong nA = 0, nB = 0; sToNum(A, nA), sToNum(B, nB); printf("%lld\n", nA + nB); }
intmain() { int ouNum = 0, jiNum = 0, n, x; while (scanf("%d", &n) != EOF) { while (n--) { scanf("%d", &x); // 根据奇数、偶数增加相应的值 x % 2 == 0 ? ouNum++ : jiNum++; } ouNum > jiNum ? printf("NO\n") : printf("YES\n"); }
system("pause"); return0; }
问题 E: Shortest Distance (20)
英文题,orz。😭
题目
题目描述
The task is really simple: given N exits on a highway which forms a
simple cycle, you are supposed to tell the shortest distance between any
pair of exits.
翻译:
很简单的任务:给出了一个有 N
个出口的高速公路,形成一个简单的环路,你应该给出任何一对出口之间的最短距离。
输入
Each input file contains one test case. For each case, the first line
contains an integer N (in [3, 105]), followed by N integer distances
\(D_1\)\(D_2\) ... \(D_N\), where \(D_i\) is the distance between the i-th and
the (i+1)-st exits, and \(D_N\) is
between the N-th and the 1st exits. All the numbers in a line are
separated by a space. The second line gives a positive integer M
(<=104), with M lines follow, each contains a pair of exit numbers,
provided that the exits are numbered from 1 to N. It is guaranteed that
the total round trip distance is no more than 107.
翻译:
每个输入文件包含一个测试用例。对于每种情况,第一行包含整数 N( 3
<= N <= 105 ),后跟 N 个整数 \(D_1\)\(D_2\) ... \(D_N\),其中 \(D_i\) 是第 i 个和(i +
1)出口之间的距离,\(D_N\) 位于第 N
和第 1 个出口之间。行中的所有数字都由空格分隔。第二行给出了正整数M( M
<= 104),其中 M 行随后,每个线条包含一对出口号,规定出口是从 1 到 N
编号。保证总往返距离不超过107。
输出
For each test case, print your results in M lines, each contains the
shortest distance between the corresponding given pair of exits.
翻译:
对于每个测试用例,打印在 M
行中的结果,每个结果包含相应给定对出口之间的最短距离。
样例输入
1 2 3 4 5
5 124149 3 1 3 2 5 4 1
样例输出
1 2 3
3 10 7
题解
思路
首先需要先把英文的题目看懂:
题目的意思是:
第一行输入:第一个数表示有 N 条路,并在之后给出 N
个数表示N条道的路径长度,这些路会形成一个环。
intmain() { int n, m, start, end; scanf("%d", &n); int *a = newint[n]; longlong sum = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } scanf("%d", &m); while (m--) { scanf("%d %d", &start, &end); int temp = 0; // 需要交换一下 if (start > end) { swap(start, end); } for (int i = start - 1; i < end - 1; i++) { temp += a[i]; } temp > sum - temp ? printf("%d\n", sum - temp) : printf("%d\n", temp); }
intmain() { longlong A, B, C; int n, i = 1; scanf("%d", &n); while (n--) { scanf("%lld %lld %lld", &A, &B, &C); A + B > C ? printf("Case #%d: true\n", i++) : printf("Case #%d: false\n", i++); }