【问题标题】:first print all numbers in given sequence, then all characters and other symbols using Array首先打印给定序列中的所有数字,然后使用 Array 打印所有字符和其他符号
【发布时间】:2020-06-22 23:21:09
【问题描述】:

如何先打印给定序列中的所有数字,然后打印所有字符和字母 在 C++ 中只使用数组而不是字符串???

举例

intput: 5 (size of array) --> a 1 2 r 4

output: 1 2 4 a r

input: 10 --> a s d @ # $ 7 8 9 1 0

output: 7 8 9 1 0 a s d @ # $
#include <iostream>
 
int main()
{
    int n, c, j = 0, m = 0, a = 0;
    char arr[1000];
        std::cin >> n; 
        for (int i = 0; i < n; ++i)
            {
                std::cin >> arr[i];
                std::cout << arr[i] << " ";
            }
}

【问题讨论】:

  • 你的意思是数字表示数字吗?
  • 注意:查看作为标准库的一部分供您使用的惊人的辅助函数数组。示例isdigitisalpha。特别注意笔记。不要尝试自己动手,因为您时不时会遇到一个字母表不按顺序排列的系统,从而使像ch &gt;= 'a' &amp;&amp; ch &lt;='z' 这样的测试非常失败。如“哈哈哈。太有趣了。谢谢,s。”

标签: c++ arrays algorithm


【解决方案1】:

如果您现在想坚持使用for 循环,只需使用cctype 标头中的一些帮助程序来清理您的代码:

#include <cctype>

for (int i = 0; i < n; i++) {
    if (std::isdigit(arr[i]))
        std::cout << (char)arr[i] << ' ';
}
for (int i = 0; i < n; i++) {
    if (std::isalpha(arr[i]))
        std::cout << (char)arr[i] << ' ';
}
for (int i = 0; i < n; i++) {
    if (!std::isdigit(arr[i]) && !std::isalpha(arr[i]))
        std::cout << (char)arr[i] << ' ';
}

尽管如此,这段代码肯定不是最优的,正如您可以从其他人使用std::partition 的更优雅的解决方案中看到的那样。一旦您对自己的技能更有信心,请考虑尝试一种算法方法:标准库有many optionsstd::partition 在这里运行良好,但您也可以尝试使用排序或删除算法。所有这些函数需要的是您的数据范围(在您的情况下,&amp;arr[0]arr 是范围的开始,arr + n 是结束),通常是一些函数来指定您希望数据如何分区,排序等。

【讨论】:

    【解决方案2】:

    你可以先partition数组把所有的数字放在开头:

    auto nums = std::stable_partition(arr, arr + n, 
                                      [](unsigned char c) { return std::isdigit(c); });
    

    然后对数组的剩余部分进行分区得到字符:

    std::stable_partition(nums, arr + n, 
                          [](unsigned char c) { return std::isalpha(c); });
    

    然后将所有内容打印出来:

    std::copy(arr, arr + n, std::ostream_iterator<char>(std::cout, " "));
    

    【讨论】:

    • 我是C++初学者,需要用简单的方法解决问题))
    • 目前可能看起来不像,但使用算法解决这些问题的最简单方法。按照答案中的链接,并查看一些示例。一旦你理解了算法的作用,它就会变得很明显。先关注partition,不要太担心copy
    • 可能仍然有汽车修理工更喜欢“仅使用胶带”的解决方案,而不是处理螺栓、螺钉、螺丝刀、扳手和喷灯;) 他们喜欢简单。
    • @BitTickler 是的,当然,我和他们中的一些人一起工作;)但我从来没有真正遇到过一个首先学习算法,然后更喜欢原始循环的人,所以我很高兴将 OP 放在正确的道路上 :)
    【解决方案3】:

    看来您的意思如下。也就是说,使用标准算法 std::partition 可以很容易地完成分配,该算法两次应用于输入的序列。如果您想将符号的顺序保持在同一类别中,您可以使用算法std::stable_partition。只需在下面的程序中将std::partition 替换为std::stable_partition

    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <cctype>
    
    int main() 
    {
        const size_t N = 1000;
        char a[N];
        
        size_t n = 0;
        
        std::cin >> n;
        
        if ( N < n ) n = N;
        
        for ( size_t i = 0; i < n; i++ )
        {
            std::cin >> a[i];
        }
        
        auto it = std::partition( a, a + n, []( unsigned char c ){ return ::isdigit( c ); } );
        std::partition( it, a + n, []( unsigned char c ){ return ::isalpha( c ); } );
        
        for ( size_t i = 0; i < n; i++ )
        {
            std::cout << a[i] << ' ';
        }
        std::cout << '\n';
        
        return 0;
    }
    

    如果进入序列

    10
    a s d @ # $ 7 8 9 1 0
    

    那么程序输出将是

    1 9 8 7 a s d @ $ # 
    

    这是同一个程序,但使用了 std::stable_partition。

    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <cctype>
    
    int main() 
    {
        const size_t N = 1000;
        char a[N];
        
        size_t n = 0;
        
        std::cin >> n;
        
        if ( N < n ) n = N;
        
        for ( size_t i = 0; i < n; i++ )
        {
            std::cin >> a[i];
        }
        
        auto it = std::stable_partition( a, a + n, []( unsigned char c ){ return ::isdigit( c ); } );
        std::stable_partition( it, a + n, []( unsigned char c ){ return ::isalpha( c ); } );
        
        for ( size_t i = 0; i < n; i++ )
        {
            std::cout << a[i] << ' ';
        }
        std::cout << '\n';
        
        return 0;
    }
    

    相同输入序列的程序输出如下所示

    7 8 9 1 a s d @ # $ 
    

    【讨论】:

    • 尝试使用e 作为输入。我记得在 apple][ 上的 applesoft basic 上,它是一个数字,是 1.0E0 的缩写 ;) 但是对于 std::is_digit(),这不应该在这里发生。
    【解决方案4】:
         ```
           #include <iostream>
           int main() {
           int n, c, j = 0, m = 0, a = 0;
           char A[1000], B[1000], C[1000];
           std::cin >> n;
    
           for (int i=0; i<n; ++i)
            {
                std::cin >> A[i];
            }
    
           for (int i = 0; i < n; ++i)
            {
                if(A[i] >= '0' && A[i] <= '9')
                 {
                   B[j] = A[i];
                   j++; 
                   c = j;
                  }    
                else 
                {
                   C[m] = A[i];
                   m++;
                }
            }
    
            for (int i = 0; i < n; ++i) 
        {
          A[i] = B[i];
          }
            for (int i = c; i < n; ++i) 
        {
          A[i] = C[a];
          a++;
        }
            for (int i = 0; i < n; ++i)
         std::cout << A[i] << " ";   
        }
    

    【讨论】:

      【解决方案5】:
      #include <bits/stdc++.h>
      using namespace std;
      
      #include <iostream>
       
      int main()
      {
          int n;
          int arr[1000];
          std::cin >> n; 
          int t[1000];
          int count=0;
          for (int i = 0; i < n; ++i)
              {
                  char temp;
                  cin>>temp;
                  if((int(temp)>=91 && int(temp)<=96) || (int(temp)>=58 && int(temp)<=64)||(int(temp)>=33 && int(temp)<=47)){ 
                      t[count]=int(temp);
                      count++;
                  }
                  else
                  {
                      arr[i]=int(temp);
                  }
              }
              
          sort(arr,arr+n);
          
          for (int i = 0; i < n; ++i){
              cout<<char(arr[i])<<" ";
          }
          for (int i = 0; i < n; ++i){
              cout<<char(t[i])<<" ";
          }
      }
      

      将输入作为“char”并将其类型转换为“int”。如果 int 值表示一个特殊字符,则将其存储在另一个数组中。然后根据整数值执行排序,最后在打印时将其类型转换为“char”再次。

      【讨论】:

        猜你喜欢
        • 2020-12-24
        • 2020-03-29
        • 1970-01-01
        • 2023-03-25
        • 2020-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-05
        相关资源
        最近更新 更多