1140 Look-and-say Sequence (20 point(s))
Look-and-say sequence is a sequence of integers as the following:
D, D1, D111, D113, D11231, D112213111, ...
where D
is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D
in the 1st number, and hence it is D1
; the 2nd number consists of one D
(corresponding to D1
) and one 1 (corresponding to 11), therefore the 3rd number is D111
; or since the 4th number is D113
, it consists of one D
, two 1's, and one 3, so the next number must be D11231
. This definition works for D
= 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D
.
Input Specification:
Each input file contains one test case, which gives D
(in [0, 9]) and a positive integer N (≤ 40), separated by a space.
Output Specification:
Print in a line the Nth number in a look-and-say sequence of D
.
Sample Input:
1 8
Sample Output:
1123123111
题目大意:模拟一个递归的过程。
解题思路:用一个数组存每次的结果,最后更新一下就好了
#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;
vector<int> num;
int main()
{
int D;
int N;
scanf("%d %d", &D, &N);
num.push_back(D);
for (int i = 0; i < N - 1; i++) {
vector<int> t;
int count = 0;
int a = num[0];
for (int j = 0; j < num.size(); j++) {
if (num[j] == a)count++;
else {
t.push_back(a);
t.push_back(count);
count = 1;
a = num[j];
}
if (j == num.size() - 1) {
t.push_back(a);
t.push_back(count);
}
}
num = t;
}
for (int i = 0; i < num.size(); i++) {
cout << num[i];
}
cout << endl;
return 0;
}
1144 The Missing Number (20 point(s))
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤105). Then N integers are given in the next line, separated by spaces. All the numbers are in the range of int.
Output Specification:
Print in a line the smallest positive integer that is missing from the input list.
Sample Input:
10
5 -25 9 6 1 3 4 2 5 17
Sample Output:
7
题目大意:找出缺少的最小的正整数。
解题思路:很简单,从1开始每次去找数组里面有没有这个数就行了,常规的遍历肯定超时,所以用二分优化一下就好了。
#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;
vector<int> res;
int main()
{
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
int tmp;
scanf("%d", &tmp);
res.push_back(tmp);
}
sort(res.begin(), res.end());
int q;
for (int i = 1;; i++) {
if (!binary_search(res.begin(), res.end(), i)) {
q = i;
break;
}
}
printf("%d\n", q);
return 0;
}