485. 最大连续 1 的个数 - 力扣(LeetCode)
#include <stdio.h>
int findMaxConsecutiveOnes(int* nums, int numsSize) {
if (numsSize == 0) return 0; // 如果数组为空,返回0
int maxCount = 0; // 最大连续1的个数
int currentCount = 0; // 当前连续1的个数
for (int i = 0; i < numsSize; i++) {
if (nums[i] == 1) {
// 如果当前元素是1,增加当前连续1的个数
currentCount++;
// 更新最大连续1的个数
if (currentCount > maxCount) {
maxCount = currentCount;
}
} else {
// 如果当前元素是0,重置当前连续1的个数
currentCount = 0;
}
}
return maxCount;
}
int main() {
int nums[] = {1, 1, 0, 1, 1, 1}; // 示例二进制数组
int numsSize = sizeof(nums) / sizeof(nums[0]); // 计算数组的大小
int maxConsecutiveOnes = findMaxConsecutiveOnes(nums, numsSize);
printf("The maximum number of consecutive 1s is: %d\n", maxConsecutiveOnes);
return 0;
}
完全数计算_牛客题霸_牛客网 (nowcoder.com)
#include <stdio.h>
// 函数用于计算一个数的所有真因子之和
int sumOfDivisors(int num) {
int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
int main() {
int n;
printf("Enter the number of test cases: ");
scanf("%d", &n);
for (int t = 0; t < n; t++) {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (sumOfDivisors(num) == num) {
printf("%d is a perfect number.\n", num);
} else {
printf("%d is not a perfect number.\n", num);
}
}
return 0;
}