首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
first print all numbers in given sequence, then all characters and other symbols using Array
如何首先打印给定序列中的所有数字,然后打印所有字符和字母 在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"。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- Int Array[]打印不好