首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号

first print all numbers in given sequence, then all characters and other symbols using Array

本文关键字:打印 Array 字符 符号 其他 数字 定顺序 然后      更新时间:2023-10-16

如何首先打印给定序列中的所有数字,然后打印所有字符和字母 在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] << " ";
}
}

如果您现在想坚持使用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的更优雅的解决方案中看到的那样。一旦您对自己的技能更有信心,请考虑尝试算法方法:标准库有很多选择。std::partition在这里确实运行良好,但您也可以尝试排序或删除算法。所有这些函数都需要数据的范围(在您的情况下,&arr[0]arr是范围的开始,arr + n是结束(,通常是一些函数来指定您希望如何对数据进行分区、排序等。

您可以先对数组进行分区,将所有数字放在开头:

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, " "));

看来你的意思是以下几点。也就是说,使用标准算法 std::p artition 对输入的序列应用两次可以轻松完成分配。Ot 如果要将符号的顺序保留在同一类别中,则可以使用算法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 @ # $ 
```
#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] << " ";   
}
#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"。