c语言打印数组元素_C程序打印元素差为0或1的子集数
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:64 6 5 3 3 1Output:3Description:
描述:
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語言打印數組元素
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的c语言打印数组元素_C程序打印元素差为0或1的子集数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个芒果多少钱啊?
- 下一篇: 哈尔滨治免疫性不孕最好的医院推荐