We need to read A, B, K. Then print all integers between A and B inclusive that are either within the K smallest integers (i.e., from A to A+K-1, but must not exceed B) or within the K largest integers (i.e., from B-K+1 to B, but not less than A). However careful: the condition says "Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers." So we consider the set S = {A, A+1, ..., B}. The K smallest integers: the first K numbers from the start (if total count N = B-A+1 >= K). If N < K? Actually K can be up to 100 but B-A+1 could be less than K. In such case, all numbers are both in the K smallest and K largest? But the problem says "within the K smallest integers or within the K largest integers". If N <= K, then all numbers are both in the K smallest and K largest, but they should be printed. But there might be duplicates if we take both sets? We should output each integer only once, in ascending order. So we need to take union of two sets: from low = A to min(A+K-1, B) and from high = max(B-K+1, A) to B. However when N < K, then the low range covers all numbers (A to B) and high range also covers all numbers (A to B). So union is all numbers. So we can just take the union.
We need to print in ascending order. So one approach: compute the set of numbers from low range and high range, combine, sort, then output. Since K <= 100, the total numbers in union is at most 2K, but if ranges overlap, it's less. So it's small.
But we must handle the case where total numbers less than K: e.g., A=2 B=9 K=100 => N=8, K> N, then low range: A to min(A+100-1, B) = 2 to 9 => all numbers; high range: max(B-100+1, A) to B = max(9-99=-90, 2) = 2 to 9 => all numbers. So union = all numbers.
So algorithm: read A, B, K. low_start = A, low_end = min(A+K-1, B). high_start = max(B-K+1, A), high_end = B. Then collect numbers from low_start to low_end inclusive, and from high_start to high_end inclusive into a set (or combine and sort unique). But since they are integer ranges, we can simply merge them while ensuring ascending order without duplicates.
Simpler: Create a vector, add all numbers from low_start to low_end, and all from high_start to high_end, then sort and unique. Because the ranges are small (max 200 numbers). That's fine.
But careful: The high range might be overlapping or included in low range. For example, when N is small. That's okay.
We can also do: iterate i from A to B; if i <= A+K-1 or i >= B-K+1, output i. But B-A+1 could be up to 10^9, so iterating over all numbers would be too slow. So we must use the set approach because K is small.
Thus we can generate the two ranges and merge.
Edge Cases: A=1, B=1, K=1: low_range = 1 to 1, high_range = 1 to 1, union = 1, output 1.
A=1, B=100, K=10: low_range = 1-10, high_range = 91-100, union 1-10 and 91-100. Output numbers in ascending order: 1..10 then 91..100.
Implementation: In C++, we can use set or vector. Since they are small, vector works.
Pseudo:
int A, B, K;
cin >> A >> B >> K;
int low_end = min(A+K-1, B);
int high_start = max(B-K+1, A);
vector ans;
for (int i = A; i <= low_end; i++) ans.push_back(i);
for (int i = high_start; i <= B; i++) ans.push_back(i);
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
for (int x : ans) cout << x << endl;
But note: When N <= K, low_end = B, high_start = A, so the first loop adds all numbers from A to B, second loop adds again all numbers from A to B, but after sort+unique duplicates removed. That's okay but we could avoid adding duplicates by checking condition or using set. But it's fine because K max 100 and N maybe up to 10^9? Wait N can be huge but in that case low_range size = K (100) and high_range size = K (100) but they might overlap. So ans size <= 200. But if N <= K, N could be up to 10^9? But N <= 10^9, but if K is up to 100 and N can be huge, then N <= K only when B-A+1 <= 100, which is small. So it's fine.
But careful: If N is huge (like 10^9) but K is 100, then low_range is 100 numbers, high_range is 100 numbers, total at most 200. So okay.
Thus the solution works.
We need to output each number on separate line.
Now check examples:
Example 1: A=3, B=8, K=2. N=6. low_end = min(3+2-1=4, 8)=4 => numbers 3,4. high_start = max(8-2+1=7, 3)=7 => numbers 7,8. Union => 3,4,7,8. Sorted => 3,4,7,8. Output matches.
Example 2: A=4, B=8, K=3. low_end = min(4+3-1=6,8)=6 => 4,5,6. high_start = max(8-3+1=6,4)=6 => numbers 6,7,8. Union => 4,5,6,6,7,8 => after unique => 4,5,6,7,8. Output matches.
Example 3: A=2, B=9, K=100. low_end = min(2+100-1=101,9)=9 => all numbers from 2 to 9. high_start = max(9-100+1=-90,2)=2 => all numbers from 2 to 9. Union => all numbers 2..9. Output matches.
Thus solution is correct.
But there might be a nuance: "Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers." Could also be interpreted that if the total count is less than 2K, there might be overlap. And we need to print each integer only once. So union is correct.
Implementation details: Use long long? A, B up to 1e9, so A+K-1 could be up to 1e9+99, fits in 32-bit signed? 1e9+99 is about 1,000,000,099 < 2^31-1 (2,147,483,647) so int works. But to avoid overflow, we can use long long just in case. But not needed. However B-K+1 might be negative? But we take max with A, so safe.
Now output: each number on separate line.
Thus solution is trivial.
We'll produce code accordingly.
But problem statement: "Print all the integers that satisfies the condition above in ascending order." So we need to print each integer on separate line.
Ok.
Now think about possible pitfalls: Input format: "A B K" separated by spaces.
Edge case: K could be 0? But constraints say 1 <= K, so K >= 1.
Ok.
Thus final answer.
We'll write in C++.
We'll include , , .
int main() {
int A, B, K;
std::cin >> A >> B >> K;
int low_end = std::min(A + K - 1, B);
int high_start = std::max(B - K + 1, A);
std::vector ans;
for (int i = A; i <= low_end; i++) ans.push_back(i);
for (int i = high_start; i <= B; i++) ans.push_back(i);
std::sort(ans.begin(), ans.end());
ans.erase(std::unique(ans.begin(), ans.end()), ans.end());
for (int x : ans) std::cout << x << '\n';
return 0;
}
That's it.
But we can also avoid sorting by generating merged list, but it's easier this way.
We can also output directly: if low_end < high_start-1 (i.e., no overlap), we can print low range then high range. But then if low_end >= high_start (overlap), we need to avoid duplicate. It's simple to just use set/vector sort.
Thus final answer.
I'll provide explanation and code. We need to output all numbers in the interval [A, B] that are among the K smallest or among the K largest numbers of that interval.
Since K ≤ 100, the answer set contains at most 200 numbers.
We compute the two ranges:
- The K smallest: from
A to min(A + K - 1, B).
- The K largest: from
max(B - K + 1, A) to B.
Collect all numbers from these two ranges, remove duplicates, sort and output.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int A, B, K;
std::cin >> A >> B >> K;
int low_end = std::min(A + K - 1, B);
int high_start = std::max(B - K + 1, A);
std::vector<int> ans;
for (int i = A; i <= low_end; ++i) ans.push_back(i);
for (int i = high_start; i <= B; ++i) ans.push_back(i);
std::sort(ans.begin(), ans.end());
ans.erase(std::unique(ans.begin(), ans.end()), ans.end());
for (int x : ans) std::cout << x << '\n';
return 0;
}