请用C语言实现Dima Vatrushin is a math teacher at school. He was sent on vacation for n days for his good work. Dima has long dreamed of going to a ski resort, so he wants to allocate several consecutive days and go skiing. Since the vacation requires careful preparation, he will only go for at least k days. You are given an array a containing the weather forecast at the resort. That is, on the i -th day, the temperature will be ai degrees. Dima was born in Siberia, so he can go on vacation only if the temperature does not rise above q degrees throughout the vacation. Unfortunately, Dima was so absorbed in abstract algebra that he forgot how to count. He asks you to help him and count the number of ways to choose vacation dates at the resort.
时间: 2024-02-14 15:11:03 浏览: 153
Dijstra算法C语言实现
4星 · 用户满意度95%
以下是使用C语言实现的代码:
```c
#include <stdio.h>
int main() {
int n, k, q;
scanf("%d %d %d", &n, &k, &q);
int a[n], i, j, cnt = 0, ans = 0;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i <= n-k; i++) {
cnt = 0;
for (j = i; j < i+k; j++) {
if (a[j] > q) break;
cnt++;
}
if (cnt == k) ans++;
}
printf("%d", ans);
return 0;
}
```
这个程序首先读取输入的三个整数 n、k 和 q。然后,它读取一个包含 n 个整数的数组 a,表示每天的气温。接下来,程序遍历所有可能的连续 k 天的时间段,并计算其中的天数是否都不高于 q 摄氏度。如果是,则将答案计数器增加 1。最后,程序输出答案。
注意:本程序仅供参考,可能有一些特殊情况没有考虑到,请谨慎使用。
阅读全文