首页 > 后端开发 > C++ > 正文

打印出在范围0-99之间缺失的元素

王林
发布: 2023-09-06 08:13:19
转载
615人浏览过

打印出在范围0-99之间缺失的元素

它将显示用户输入的给定集中缺失的值

Given : array = {88, 105, 3, 2, 200, 0, 10};
Output : 1 4-9 11-87 89-99
登录后复制

算法

START
STEP 1-> Take an array with elements, bool flag[MAX] to Fale, int i, j, n to size of array
Step 2-> Loop For from I to 0 and i<n and i++
   IF array[i] < 100 && array[i]>=0
      Set flag[array[i]]=true
   End IF
Step 3 -> End For Loop
Step 4 -> Loop For from i to 0 and i<MAX and ++i
   IF flag[i] == false
      Set j=i+1
      Loop While j<MAX && flag[j] == false
         Set j++
      End While
      If j=i+1
         Print i
      End IF
   Else
      Print i and j-1
   End Else
   Set i=j
End IF
Step 5 -> End For Loop
STOP
登录后复制

示例

#include <stdio.h>
#define MAX 100
int main(int argc, char const *argv[]) {
   int array[] = {88, 105, 3, 2, 200, 0, 10};
   bool flag[MAX] = { false }; //Initializing all the values of flag as false
   int i, j, n;
   n = sizeof(array)/sizeof(array[0]);
   for (i = 0; i < n; i++) {
      if (array[i] < 100 && array[i]>=0) {
         flag[array[i]] = true; //Making the value of the elements present in an array as true, So missing will remain false
      }
   }
   for (i = 0; i < MAX; ++i) {
      if(flag[i] == false) { //Checking for false values
         j = i+1; //Giving the value of the next iteration
         while(j<MAX && flag[j] == false) //Checking the value of flag[j] is false
         j++;
         if (j==i+1) //For printing the missing number
            printf("%d</p><p>", i);
         else //For printing the missing range
            printf("%d-%d</p><p>", i, j-1);
         i = j; //Initializing the range's last value to start from that number
      }
   }
   return 0;
}
登录后复制

输出

如果我们运行上面的程序,它将生成以下输出

1
4-9
11-87
89-99
登录后复制

以上就是打印出在范围0-99之间缺失的元素的详细内容,更多请关注php中文网其它相关文章!

全能打印神器
全能打印神器

全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!

下载
相关标签:
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号