如何以相反的顺序打印出整数的各个数字C ++
How to print out individual digits of an integer in reverse order c++
我有一个作业指示:
Write a function called PrintDigits that takes in a single integer and prints out the individual digits of that integer in reverse order (1s digit first, then 10s digit, then 100s digit and so on), one per line, and returns the number of digits printed. This should work for positive or negative integers
Then write a main function that asks the user for 2 integers, prints out the digits of those two integers (using the PrintDigits function that you wrote), and tells the user which integer has the most digits and what the difference in digits is. You may assume the user input is two integers.
但我的问题是我是否应该使用数组。我知道使用数组你可以挑出单个数字并以相反的顺序打印它们,但我不确定这是否适用于分解单个整数。我试过使用数组,但我认为这不再是正确的选择。这是我的代码:
#include <iostream>
using namespace std;
void PrintDigits(int[], int);
int main()
{
int int1[8], int2[8];
cout << "Please input two integers: ";
cin >> int1[8] >> int2[8];
cout << "\n\nDigits of " << int1[8];
cout << PrintDigits(int1, 8);
return 0;
}
void PrintDigits (int arr[], int size)
{
for (int i=0; i < size; i++)
cout << arr[i] << '/n';
}
有没有其他方法可以在没有数组的情况下获得这些结果?
最简单的方法是使用 string
。然后你可以只读入一个恰好是数字的字符串,然后倒序输出:
std::string reverse;
for(size_t i = (line.size() - 1); i >= 0; --i)
{
reverse += line[i];
}
只要line
是一个字符串或者char*
,你就可以这样做,你只需要使用string
而不是int
您可以使用数组,但不是必须的。这道题是经典的递归入门练习。
不用说您可以选择使用循环而不是递归,但我认为递归很优雅
#include <iostream>
using namespace std;
void func(int val) {
int new_val = val / 10;
int dig = val % 10;
cout << dig << " ";
if (new_val > 0) {
func(new_val);
}
}
int main() {
func(4321);
return 0;
}
说明
用%
运算符提取最右边的数字。打印出来。如果其他数字代表的数字大于0,则重复该过程。
这输出:1 2 3 4
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int const number = ::rand();
for (int iterator = number; number; number /= 10)
::printf("[%i]", number % 10);
return EXIT_SUCCESS;
}
这通常对我有用。如果您想将数字转换为字符串,请将 number % 10
更改为 "0123456789"[number % 10]
或 '0' + (number % 10)
.
请注意,它仍然是相反的。
我有一个作业指示:
Write a function called PrintDigits that takes in a single integer and prints out the individual digits of that integer in reverse order (1s digit first, then 10s digit, then 100s digit and so on), one per line, and returns the number of digits printed. This should work for positive or negative integers Then write a main function that asks the user for 2 integers, prints out the digits of those two integers (using the PrintDigits function that you wrote), and tells the user which integer has the most digits and what the difference in digits is. You may assume the user input is two integers.
但我的问题是我是否应该使用数组。我知道使用数组你可以挑出单个数字并以相反的顺序打印它们,但我不确定这是否适用于分解单个整数。我试过使用数组,但我认为这不再是正确的选择。这是我的代码:
#include <iostream>
using namespace std;
void PrintDigits(int[], int);
int main()
{
int int1[8], int2[8];
cout << "Please input two integers: ";
cin >> int1[8] >> int2[8];
cout << "\n\nDigits of " << int1[8];
cout << PrintDigits(int1, 8);
return 0;
}
void PrintDigits (int arr[], int size)
{
for (int i=0; i < size; i++)
cout << arr[i] << '/n';
}
有没有其他方法可以在没有数组的情况下获得这些结果?
最简单的方法是使用 string
。然后你可以只读入一个恰好是数字的字符串,然后倒序输出:
std::string reverse;
for(size_t i = (line.size() - 1); i >= 0; --i)
{
reverse += line[i];
}
只要line
是一个字符串或者char*
,你就可以这样做,你只需要使用string
而不是int
您可以使用数组,但不是必须的。这道题是经典的递归入门练习。
不用说您可以选择使用循环而不是递归,但我认为递归很优雅
#include <iostream>
using namespace std;
void func(int val) {
int new_val = val / 10;
int dig = val % 10;
cout << dig << " ";
if (new_val > 0) {
func(new_val);
}
}
int main() {
func(4321);
return 0;
}
说明
用%
运算符提取最右边的数字。打印出来。如果其他数字代表的数字大于0,则重复该过程。
这输出:1 2 3 4
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int const number = ::rand();
for (int iterator = number; number; number /= 10)
::printf("[%i]", number % 10);
return EXIT_SUCCESS;
}
这通常对我有用。如果您想将数字转换为字符串,请将 number % 10
更改为 "0123456789"[number % 10]
或 '0' + (number % 10)
.
请注意,它仍然是相反的。