Advertisement
xT30x

Untitled

Jan 30th, 2023
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int v[1001];
  8.     //v[i] =0;
  9.     for(int i=1; i<=1000; i++){
  10.         v[i] =0;
  11.     }
  12.  
  13.     int n;
  14.     cin>>n;
  15.     for(int i=0; i<n; i++){
  16.         int x;
  17.         cin>>x;
  18.         v[x]=1;
  19.     }
  20.     int s =-1;
  21.     int e =-1;
  22.     for(int i=1; i<=1000; i++){
  23.         if(v[i]==1){
  24.             if(s==-1){
  25.                 s =i;
  26.                 e =i;
  27.             }
  28.             else{
  29.                 e=i;
  30.             }
  31.         }
  32.         else{
  33.             if(s !=-1){
  34.                 if(s==e){
  35.                     cout<<s<<" ";
  36.                 }else if(e-s==1){
  37.                     cout<<s<<" "<<e<<" ";
  38.                 }
  39.                 else{
  40.                     cout<<s<<"-"<<e<<" ";
  41.                 }
  42.                 s=-1;
  43.                 e=-1;
  44.             }
  45.         }
  46.     }
  47.     if(s !=-1){
  48.         if(s==e){
  49.             cout<<s<<" ";
  50.         }
  51.         else{
  52.             cout<<s<<"-"<<e<<" ";
  53.         }
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement