1065 A+B and C (64bit) (20 point(s))
Given three integers A, B and C in [−263,263], you are supposed to tell whether A+B>C.
Input Specification:
The first line of the input gives the positive number of test cases, T (≤10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.
Output Specification:
For each test case, output in one line Case #X: true
if A+B>C, or Case #X: false
otherwise, where X is the case number (starting from 1).
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
题目大意:给三个数abc,如果a+b>c就输出yes,当然没那么简单,因为A+B的结果即使用long double都会溢出。
解题思路:两种思路吧,第一种考试实现不现实,第二种简单。第一种就是模拟大数相加了,没什么意思的就是手摸,考试时候写写很浪费时间。第二种就比较简单了,利用题目的特点。abc都是在63位范围内的,如果a+b溢出了,根据linux的特性,溢出的值返回负数,那么很简单了两个正数相加肯定比c大了,如果两个负数,变正了,那同理比c小了,如果没有溢出,那不用说了。
代码如下:
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
int main() {
int T;
scanf("%d", &T);
for (int i = 1; i<=T;i++) {
long long int a, b, c;
scanf("%lld %lld %lld", &a, &b, &c);
long long int temp = a + b;
if (a > 0 && b > 0 && temp <= 0) {
printf("Case #%d: true\n", i);
}
else if (a < 0 && b < 0 && temp >= 0) {
printf("Case #%d: false\n", i);
}
else if (temp > c) {
printf("Case #%d: true\n", i);
}
else {
printf("Case #%d: false\n", i);
}
}
return 0;
}
1067 Sort with Swap(0, i) (25 point(s))
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order. But what if Swap(0, *)
is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:
Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}
Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.
Input Specification:
Each input file contains one test case, which gives a positive N (≤105) followed by a permutation sequence of {0, 1, ..., N−1}. All the numbers in a line are separated by a space.
Output Specification:
For each case, simply print in a line the minimum number of swaps need to sort the given permutation.
Sample Input:
10
3 5 7 2 6 4 9 0 8 1
Sample Output:
9
题目大意:一开始没看懂,后来发现是描述了一个交换规则,一个从0.。。N-1的乱序数组,交换的规则就是每次把0和值等于它的下标的值进行交换,直到交换到0的下标是0.
解题思路:一种是纯数组吧,绕一点,下面是代码。还一种应该搞两个map双向的记录然后写个while,记录一下次数就行了。
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
int a[100005];
void swap(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int res = 0;
for (int i = 0; i < n; i++) {
if (a[i] != i) {
swap(a[0], a[i]);
if (i != 0)
res++;
while (a[0] != 0) {
swap(a[0], a[a[0]]);
res++;
}
}
}
printf("%d\n", res);
return 0;
}