c语言打印数组元素
Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1.
给定一个整数数组,查找并打印可以从数组中选择的最大整数数,以使任意两个选定整数之间的绝对差小于或等于1。
For example, if your array is a = [1,1,2,2,4,4,5,5,5]
例如,如果您的数组是a = [1,1,2,2,4,4,5,5,5]
You can create two subarrays meeting the criterion: [1,1,2,2] and [4,4,5,5,5]. The maximum length subarray has 5 elements.
您可以创建两个满足条件的子数组:[1,1,2,2]和[4,4,5,5,5] 。 最大长度子数组包含5个元素。
Input format:
输入格式:
The first line contains a single integer a, the size of the array b.
The second line contains a space-separated integers b[i].
第一行包含一个整数a ,即数组b的大小。
第二行包含以空格分隔的整数b [i] 。
Output format:
输出格式:
A single integer denoting the maximum number of integers you can choose from the array such that the absolute difference between any two of the chosen integers is <=1.
表示您可以从数组中选择的最大整数数的单个整数,以使任意两个所选整数之间的绝对差为<= 1 。
Constraint:
约束:
2<=a<=100
2 <= a <= 100
Example:
例:
Input:
6
4 6 5 3 3 1
Output:
3
Description:
描述:
We choose the following multiset of integers from the array:{4,3,3}. Each pair in the multiset has an absolute difference <=1(i.e., |4-3|=1 and |3-3|=0 ), So we print the number of chosen integers, 3 - as our answer.
我们从数组中选择以下整数整数集:{4,3,3} 。 多重集中的每一对都有一个绝对差异<= 1(即| 4-3 | = 1和| 3-3 | = 0) ,因此我们打印选择的整数3的数目作为答案。
Solution:
解:
#include <stdio.h>
int main()
{
//a is the length of array and b is the name of array.
int a, t, i, j, count, k;
count = 0;
printf("Enter the size of the array: ");
scanf("%d", &a);
int b[a], d[101];
printf("Enter array elements: ");
for (i = 0; i < a; i++) {
scanf("%d", &b[i]);
}
for (i = 0; i <= 100; i++) {
for (j = 0; j < a; j++) {
if (i == b[j]) {
count = count + 1;
}
}
d[i] = count;
count = 0;
}
t = d[0] + d[1];
for (i = 0; i < 100; i++) {
k = d[i] + d[i + 1];
if (k > t) {
t = k;
}
}
printf("Number of subset: %d", t);
return 0;
}
Output
输出量
Enter the size of the array: 9
Enter array elements: 1 1 2 2 4 4 5 5 5
Number of subset: 5
翻译自: https://www.includehelp.com/c-programs/print-the-number-of-subset-whose-elements-have-difference-0-or-1.aspx
c语言打印数组元素