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语言打印数组元素

c语言打印数组元素_C程序打印元素差为0或1的子集数相关推荐

  1. ascii非打印控制字符表_C程序打印ASCII表/图表

    ascii非打印控制字符表 什么是ASCII码? (What are ASCII Codes?) ASCII stands for American Standard Code for Informa ...

  2. 若只有4KB内存可用,该如何打印数组中所有重复的元素

    2019独角兽企业重金招聘Python工程师标准>>> /**  * 功能:给定一个数组,包含1到N的整数,N最大为32000,数组可能含有重复的值,且N的取值不定.  * 若只有4 ...

  3. C语言在数组中找到最大的元素(附完整源码)

    C语言在数组中找到最大的元素 C语言在数组中找到最大的元素完整源码(定义,实现,main函数测试) C语言在数组中找到最大的元素完整源码(定义,实现,main函数测试) #include <io ...

  4. python打印空心长方形_Python程序打印空心半菱形星形图案

    给出一个整数N,任务是打印空心半菱形图案.示例:输出:## ## ## ## ## ##输入:7## ## ## ## ## ## ## #### ## ## ## #下半部分:对于下半部分,使用迭代 ...

  5. c++ 数组换行_C语言的数组的构建与打印

    在许多程序中,数组很重要.数组可以作为一种存储多个相关项的便利方式. 数组(array)是按顺序存储的一系列类型相同的值,如10个char类型的字符或15个int类型的值.整个数组有一个数组名,通过整 ...

  6. java如何打印数组的值,Java打印数组元素的值

    本篇文章帮大家学习java打印数组元素的值,包含了Java打印数组元素的值使用方法.操作技巧.实例演示和注意事项,有一定的学习价值,大家可以用来参考. 以下实例演示了如何通过重载 MainClass ...

  7. c 指针打印变量_C程序打印不同类型的指针变量的大小。

    c 指针打印变量 Any type of pointer variable takes the same memory bytes in the memory, because they are us ...

  8. php打印倒立金字塔,编写程序打印*字符形成的等腰三角形倒立金字塔图形 ******* ***** *** *...

    导航:网站首页 > 编写程序打印*字符形成的等腰三角形倒立金字塔图形 ******* ***** *** * 编写程序打印*字符形成的等腰三角形倒立金字塔图形 ******* ***** *** ...

  9. python 打印数组第一行_打印数组中的第一个值而不是只打印第一个字母?(Python)...

    我使用Python将SQLite3数据库中的数据追加到数组中.现在我有了一个数组,我正试图以如下格式打印附加数据:print "Team: " + new_array[0][0] ...

最新文章

  1. c 普通的文本变成注释文本的快捷键_2019年IntelliJ IDEA快捷键终极大全,绝版!速度收藏!...
  2. Nginx 出现504 Gateway Time-out的解决方法
  3. HashMap的实现与优化
  4. 使用grep -v时候,想去除多个pattern
  5. jQuery常用的全局方法源码
  6. vue-cli项目布署问题解决:空白页、静态资源文件404错误、refrenceError:promise未定义(部分浏览器不支持ES6语法)
  7. ipython 模块搜索路径
  8. java图像在背景图移动_java – 在Swing中移动背景图像
  9. Facebook的体系结构分析---外文转载
  10. html strong标签 无法渲染,javascript – 当我动态设置内容时,TinyMCE无法使用chrome
  11. 使cmd窗口不自动关闭
  12. java day17 【线程、同步】
  13. 关于Cocos2d-x中使用完Blink动作后精灵突然消失的问题的解决
  14. 简单的关闭iframe窗口和获取父页面元素
  15. 专利检索常用的16个网站
  16. mysql中部门表和员工表_数据库 员工表和部门表
  17. 实现跨word文档的格式刷,两个word间格式刷
  18. 幸福是什么?怎么得到幸福?
  19. 微信群裂变引流效果怎么样?微信社群引流怎么操作?
  20. java鬼混笔记:springboot 5、springboot的Scheduled定时器:fixedDelay和fixedRate区别

热门文章

  1. box-shadow技巧分享
  2. Vue表格中,对数据进行转换、处理
  3. [ZJJOI2013]K大数查询 整体二分
  4. python函数-函数进阶
  5. 爬虫框架:scrapy
  6. jquery-文档操作
  7. my-innodb-heavy-4g.cnf
  8. java 18 - 6 TreeMap嵌套使用
  9. 单元测试中Assert类的用法
  10. HDU 2836 Traversal 简单DP + 树状数组