求区间最大值---找出下面“输入数据及格式”中所描述的输入数据文件中最大重叠区间的大小
2014-06-19 17:34
351 查看
题目描述:请编写程序,找出下面“输入数据及格式”中所描述的输入数据文件中最大重叠区间的大小。
对一个正整数 n ,如果n在数据文件中某行的两个正整数(假设为A和B)之间,即A<=n<=B或A>=n>=B ,则 n 属于该行;
如果 n 同时属于行i和j ,则i和j有重叠区间;重叠区间的大小是同时属于行i和j的整数个数。
例如,行(10 20)和(12 25)的重叠区间为 [12 20] ,其大小为9,行(20 10)和( 20 30 )的重叠区间大小为 1 。
实现起来不难
对一个正整数 n ,如果n在数据文件中某行的两个正整数(假设为A和B)之间,即A<=n<=B或A>=n>=B ,则 n 属于该行;
如果 n 同时属于行i和j ,则i和j有重叠区间;重叠区间的大小是同时属于行i和j的整数个数。
例如,行(10 20)和(12 25)的重叠区间为 [12 20] ,其大小为9,行(20 10)和( 20 30 )的重叠区间大小为 1 。
实现起来不难
#include<stdio.h> void swap( int *a, int *b) { int temp=*a; *a=*b; *b=temp; } void find(int first_one[], int second_one[]) { //先使它们变成正规的区间表达式 if(first_one[1]<first_one[0]) swap(first_one,first_one+1); if(second_one[1]<second_one[0]) swap(second_one,second_one+1); //如果其中一个区间的最大值小于另一个区间的最小值,显然是没有交集的 if(first_one[0]>second_one[1] || second_one[0]>first_one[1]) { printf("0\n"); } else { //寻找两个区间右边值较小,左值较大的数 if(first_one[1]<second_one[1]) { if(first_one[0]>second_one[0]) { printf("%d \n", first_one[1]-first_one[0]+1); } else { printf("%d \n",first_one[1]-second_one[0]+1); } } else { if(first_one[0]>second_one[0]) { printf("%d \n", second_one[1]-first_one[0]+1); } else { printf("%d \n",second_one[1]-second_one[0]+1); } } } } int main() { int first_one[2]; int second_one[2]; scanf("%d%d",&first_one[0],&first_one[1]); scanf("%d%d",&second_one[0],&second_one[1]); find( first_one, second_one); getchar(); return 0; }
相关文章推荐
- linux -- 找出linux下面文件大小超过某个数值的所有文件并输出列表
- 数据文件头块保留大小、ROWID、数据文件最大大小等数据库限制的说明
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 求最大重叠区间大小
- 最大重叠区间大小
- DBF的文件格式和C语言描述,表文件由头记录及数据记录组成。
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 数据文件最大大小
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- Linux文件系统1:Ext2EXT2格式 块大小为1024字节的话,单一文件最大容量是16GB,块大小为4096字节的话,单一文件最大容量为2TB
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】
- 第13周-项目1-小玩文件-用键盘输入文件名,统计输出类似下面的数据
- 157 求最大重叠区间大小
- 【表空间支持的最大数据文件大小的算法】【数据库限制】【数据文件文件头保留数据块数】