在c++中按顺序打印数字的数字
Print the digits of a number in sequential order in C++
嗨,我想做一个函数,打印任何正数的数字顺序,然后添加所有的数字。例如:
If a user inputs 123, the program should output
1
2
3
6
我知道如何按反向顺序打印,也知道如何将数字相加,但如果输入可以是任意大小的整数,我想不出一种按顺序打印的方法。
代码
#include <iostream>
#include <string>
int main() {
std::cout << "Please enter a number: ";
std::string data;
std::getline(std::cin, data);
int total = 0;
for (char c : data) {
int current = std::stoi(std::string(1, c));
std::cout << current << std::endl;
total += current;
}
std::cout << total << std::endl;
}
运行示例[7:45pm][wlynch@watermelon /tmp] ./foo
Please enter a number: 123
1
2
3
6
using namespace std;
int print_and_add_digits(int x)
{
std::string str;
int count = 0;
int sum = 0;
// ignore negative numbers
if (x < 0)
{
return -1;
}
// special case for 0. Just print 0 and exit
if (x == 0)
{
cout << 0 << endl;
return 0;
}
while (x > 0)
{
int digit = x % 10;
sum += digit;
str += (char)('0' + digit); // convert each digit to a char and append to string
count++;
x = x / 10;
}
// reverse the string and print each digit on it's own line
str = std::string(str.end(), str.begin());
for (int i = 0; i < count; i++)
{
cout << str[i] << endl;
}
// print and return the sum
cout << sum << std::endl;
return sum;
}
我认为没有任何必要使用类std::string。你可以这样做
#include <iostream>
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int x = 0;
std::cin >> x;
if ( !x ) break;
unsigned int n = 1;
const unsigned int base = 10;
while ( x / n >= base ) n *= base;
unsigned int total = 0;
do
{
total += x /n;
std::cout << x / n << ' ';
x %= n;
} while ( n /= base );
std::cout << "nSum of digits is " << total << std::endl;
}
}
或者您可以按以下方式更改输出
#include <iostream>
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int x = 0;
std::cin >> x;
if ( !x ) break;
unsigned int n = 1;
const unsigned int base = 10;
while ( x / n >= base ) n *= base;
unsigned int total = 0;
do
{
total += x /n;
std::cout << x / n;
x %= n;
} while ( ( n /= base ) && ( std::cout << " + " ) );
std::cout << " = " << total << std::endl << std::endl;
}
}
输出为
Enter a non-negative number (0 - exit): 123456789
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
Enter a non-negative number (0 - exit): 0
也可以写一个递归函数。例如
#include <iostream>
unsigned int PrintDigits( unsigned int x, std::ostream &os = std::cout )
{
const unsigned int base = 10;
unsigned int digit = x % base;
unsigned int total = digit + ( ( x /= base ) ? PrintDigits( x ) : 0 );
os << digit << std::endl;
return total;
}
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int x = 0;
std::cin >> x;
if ( !x ) break;
std::cout << PrintDigits( x ) << std::endl << std::endl; }
}
}
输出为
Enter a non-negative number (0 - exit): 123456789
1
2
3
4
5
6
7
8
9
45
Enter a non-negative number (0 - exit): 0
相关文章:
- 打印数字图案
- 将数字打印成文字
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 我的问题是关于C++中数字和序列的重复以及如何打印它们?
- 编译器始终打印十六进制/八进制数字
- 我想使用哈希图打印非重复的数字;
- 如何在控制台上打印出带有数字 0-99 的简单数组
- 错误含义;以二进制形式打印数字
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- 列中打印数字时如何避免打印垃圾?
- 如何编写一个程序来打印特定基数的前N个数字
- C++ 在地板函数之后以十六进制打印数字
- C 不将重复数字打印到输出
- 阿姆斯特朗数字打印错误
- 用数字打印所有可能的组合
- 将数字打印为字符串类型的数字
- 如何以特定格式将数字打印到字符串的中间
- 如何将矢量中的数字打印到文件中