带有反向数字的指针

pointers with numbers in backwards

本文关键字:指针 数字      更新时间:2023-10-16

我需要在 (0-9( 之间接收不确定数量的整数。使用此数字,向前和向后打印它们,然后擦除角落处的数字。

例:

3 5
1 9 4 6 2 4 4 2 6 4 9 1 5 3 5 1 9 4 6 2 4 4 2 6 4 9 1 5 1 9 4 6 2 4 4 2 6 4 9 1 9 4 6 2 4 4 2 6 4 9 4 6 2 4 4 2 6 4 6 2 4 4 2 6 2 4 4 2 4 4 这是我

到目前为止的代码:

#include <iostream>
using namespace std;
int a;
int p;
int set;
void numberss()
{
for set[](int a=0; a<p; a++) 
} 
int main() 
{ 
cin >> p;
cin >> a;
const int SIZE = p;
int set[] = {a};
int *numPtr;
numPtr = set;
for (int index = 0; index < SIZE; index++)
{
cout << *numPtr << " ";
numPtr++;
}
for (int index = 0; index < SIZE; index++)
{
numPtr--;
cout << *numPtr << " "; 
}
return 0;
}

如果我们忽略错误,您可以一次读取一个数字,并为输出的第一行形成一个字符串。形成字符串将涉及将反向副本附加到原始副本。形成字符串后,可以为第一行输出该字符串。然后用空格字符替换第一个数字,并从后面将字符串缩小两个字符。继续这样做,直到你完成。

这是有效的,因为数字都是个位数。

int main (void)
{
int N;
std::string nums;
std::cin >> N;
for (int i = 0, x; i < N; ++i) {
std::cin >> x;
nums += std::to_string(x) + ' ';
}
nums.append(nums.rbegin() + 1, nums.rend());
for (int i = 0; i < N; ++i) {
std::cout << nums << 'n';
nums[2*i] = ' ';
nums.resize(nums.size()-2);
}
}

演示

您的代码不起作用,因为您没有从用户的输入中读取所有数字,而只是读取计数和第一个数字。 此外,您没有循环足够的时间来以三角形方式输出数字,您只是输出三角形的第一行。

试试这个:

#include <iostream>
#include <vector>
using namespace std;
int main() 
{ 
int p;
vector<int> set;
cin >> p;
set.resize(p);
for (int i = 0; i < p; ++i)
cin >> set[i];
for (int index = 0; index < p; index++)
{
int *numPtr = &set[index];
for (int i = 0; i < index; ++i)
cout << "  ";           
for (int i = index; i < p; ++i)
cout << *numPtr++ << " ";
for (int i = index; i < p; i++)
cout << *--numPtr << " "; 
cout << endl;
}
return 0;
}

现场演示


话虽如此,这里有一种更C++和更少 C 的替代方法,通过使用迭代器而不是指针,并使用 STL 算法。 此外,在使用用户输入之前,应始终对其进行验证:

#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <limits>
#include <iterator>
#include <cstdint>
using uint16vec = std::vector<uint16_t>; // there is no operator>> for uint8_t...
int main()
{
size_t count = 0;
std::cin >> count;
uint16vec set;
set.reserve(count);
for (size_t i = 0; i < count; ++i)
{
uint16vec::value_type num;
while (!((std::cin >> num) && (num <= 9)))
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::cin.clear();
std::cout << "Enter a valid number 0..9!" << std::endl;
}
set.push_back(num);
}
auto begin = set.begin(), end = set.end();
auto rbegin = set.rbegin(), rend = set.rend();
auto out = std::ostream_iterator<uint16vec::value_type>(std::cout, " ");
std::cout << std::setfill(' ');
for (size_t i = 0; i < count; ++i)
{
std::cout << std::setw((i*2)+1);
std::copy(begin++, end, out);
std::copy(rbegin, rend--, out);
std::cout << std::endl;
}
return 0;
}

现场演示

试一试,伙计。我已经评论了它,所以你可以跟着看。它允许使用数字以外的字符,因为这似乎不是您的作业的要求,因此您可以对这些字符进行排序和过滤。它按预期打印,所以这里希望这就是您正在寻找的内容,以开始寻找正确答案。

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void pop_front(vector<char>& _vector)
{
_vector.front() = move(_vector.back());
_vector.pop_back();
}
int main() 
{ 
vector<char> characterList;
string consoleInput;
cin >> consoleInput;
//Initialize by pushing everyting from the console into our vector.
for (char c : consoleInput)
{
characterList.push_back(c);
}
//After adding from the console, now we'll do it in reverse.
for (int i = consoleInput.length() - 1; i > -1; i--)
{
characterList.push_back(consoleInput[i]);
}
//Print where we are currently up to for display purposes.
for (char c : characterList)
{
cout << c;
}
// Newline.
cout << "n";
//Now let's start to chop it down. It'll take the same iterations as the console input, because we're doubling down.
for (int i = 0; i < consoleInput.length() - 1; i++)
{
characterList.erase(characterList.begin());
characterList.pop_back();
for (char c : characterList)
{
cout << c;
}
cout << "n";
}
return 0;
}