c语言打印数组元素_C程序打印元素差为0或1的子集数
c語(yǔ)言打印數(shù)組元素
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.
給定一個(gè)整數(shù)數(shù)組,查找并打印可以從數(shù)組中選擇的最大整數(shù)數(shù),以使任意兩個(gè)選定整數(shù)之間的絕對(duì)差小于或等于1。
For example, if your array is a = [1,1,2,2,4,4,5,5,5]
例如,如果您的數(shù)組是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.
您可以創(chuàng)建兩個(gè)滿足條件的子數(shù)組:[1,1,2,2]和[4,4,5,5,5] 。 最大長(zhǎng)度子數(shù)組包含5個(gè)元素。
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].
第一行包含一個(gè)整數(shù)a ,即數(shù)組b的大小。
第二行包含以空格分隔的整數(shù)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.
表示您可以從數(shù)組中選擇的最大整數(shù)數(shù)的單個(gè)整數(shù),以使任意兩個(gè)所選整數(shù)之間的絕對(duì)差為<= 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.
我們從數(shù)組中選擇以下整數(shù)整數(shù)集:{4,3,3} 。 多重集中的每一對(duì)都有一個(gè)絕對(duì)差異<= 1(即| 4-3 | = 1和| 3-3 | = 0) ,因此我們打印選擇的整數(shù)3的數(shù)目作為答案。
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語(yǔ)言打印數(shù)組元素
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的c语言打印数组元素_C程序打印元素差为0或1的子集数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一个芒果多少钱啊?
- 下一篇: 抛硬币正面期望_如果抛硬币,正面的数量多