Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
100560 包老师 22相差2的数的个数 C++ Wrong Answer 1 MS 200 KB 997 2024-12-08 13:19:03

Tests(0/2):


Code:

#include <stdio.h> int main() { int n; scanf("%d", &n); // 读取n的值 int array[n]; // 创建数组存储数列 for (int i = 0; i < n; i++) { scanf("%d", &array[i]); // 读取数列 } int maxCount = 0; // 记录最大相差2以内的数的个数 int currentCount = 1; // 当前相差2以内的数的个数,至少为1 // 遍历数列,找出相差2以内的数的个数 for (int i = 0; i < n - 1; i++) { if (array[i + 1] - array[i] <= 2) { currentCount++; // 如果当前数与下一个数相差2以内,计数增加 } else { if (currentCount > maxCount) { maxCount = currentCount; // 更新最大值 } currentCount = 1; // 重置当前计数 } } // 检查最后一个区间 if (currentCount > maxCount) { maxCount = currentCount; } printf("%d\n", maxCount); // 输出结果 return 0; }


Run Info:

------Input------
10 80 79 78 77 65 55 54 54 50 49
------Answer-----
3
------Your output-----
10