C++ C++;分离一个整数并求和';一半
我想要一些逻辑甚至代码来向我展示。问题就如标题所示。示例: 我输入n=1234; 我把n分为12和34,求和1和2,然后求和3和4在两个变量中。 然后我比较变量。我知道如何分离int,我知道如何比较它们,但我没有求和的逻辑。我如何将这两半相加?或者我怎么才能把这两半分开? 编辑:C++ C++;分离一个整数并求和';一半,c++,C++,我想要一些逻辑甚至代码来向我展示。问题就如标题所示。示例: 我输入n=1234; 我把n分为12和34,求和1和2,然后求和3和4在两个变量中。 然后我比较变量。我知道如何分离int,我知道如何比较它们,但我没有求和的逻辑。我如何将这两半相加?或者我怎么才能把这两半分开? 编辑: #include <iostream> #include <stack> #include <string> using namespace std; int getInput(
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int getInput(string prompt)
{
int val;
cout << prompt;
cin >> val;
return val < 0 ? -val : val;
}
int main(int argc, char** argv)
{
int num = getInput("Enter a number: ");
cout << "Original Number: " << num << endl;
stack<int> digits;
int sum = 0;
while(num > 0)
{
digits.push(num % 10);
sum += digits.top();
num = num / 10;
}
while(digits.size() > 0)
{
cout << digits.top() << " ";
digits.pop();
}
cout << endl << "Sum of digits is " << sum << endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int getInput(字符串提示)
{
int-val;
cout>val;
返回值<0?-val:val;
}
int main(int argc,字符**argv)
{
int num=getInput(“输入一个数字:”);
cout假设我们取了一个整数1234,并分成两部分:12和34:
int part_1 = 12;
int part_2 = 34;
要求和或相加,我们可以使用加法运算符:
int sum = part_1 + part_2;
cout << "Sum is: " << sum << endl;
我们可以获得字符串的长度:
const std::string text_number = "1234";
const unsigned int length = text_number.length();
要将字符串一分为二,我们将长度一分为二,然后将一半字符复制到一个字符串,另一半复制到另一个字符串
const unsigned int half_length = length / 2;
std::string part1;
std::string part2;
unsigned int index;
for (index = 0; index < half_length; ++index)
{
part1 += text_number[index];
}
for (; index < length; ++index)
{
part2 += text_number[index];
}
最后,它们可以总结为:
int sum = first_half + second_half;
缺少很多错误处理,包括检查数字的#是否为偶数,但我认为你会明白这一点:
#include <iostream>
#include <cmath>
int main() {
std::cout << "Enter your number: " << std::endl;
int i = 0;
std::cin >> i;
int digits = (int) ceil( log10(i) );
int modulo = pow(10, digits >> 1);
std::pair<int, int> parts = { i / modulo,
i % modulo };
std::cout << parts.first << " " << parts.second << std::endl;
int sum = parts.first + parts.second;
std::cout << sum;
}
#包括
#包括
int main(){
std::cout i;
整数位数=(int)ceil(log10(i));
整数模=功率(10,数字>>1);
std::pair parts={i/模,
i%模};
std::cout这就是我想到的。我改进了Thomas的答案,得出了一个结论。这可能是一个沉重的方法,但嘿,这不会是专业的摊牌。谢谢你的方法以及lorro,谢谢你的反馈。祝你好运
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string text_number;
std::cout << "Enter Number: ";
std::cin>>text_number;
const unsigned int length = text_number.length();
const unsigned int half_length = length / 2;
std::string part1;
std::string part2;
unsigned int index;
for (index = 0; index < half_length; ++index)
{
part1 += text_number[index];
}
for (; index < length; ++index)
{
part2 += text_number[index];
}
std::istringstream stream1(part1);
std::istringstream stream2(part2);
int first_half;
int second_half;
stream1 >> first_half;
stream2 >> second_half;
int S = 0,S1 =0;
while(first_half>0)
{
int digit = first_half % 10;
first_half /= 10;
S += digit;
}
while(second_half>0)
{
int digit = second_half % 10;
second_half /= 10;
S1 += digit;
}
if(S > S1)
{
std::cout <<"First half half sum:" << S;
}else {
std::cout <<"second half sum:" << S1;
}
return 0;
}
#包括
#包括
#包括
int main()
{
std::字符串文本\u编号;
std::cout>text\U编号;
const unsigned int length=text_number.length();
const unsigned int half_length=length/2;
std::字符串部分1;
字符串部分2;
无符号整数索引;
对于(索引=0;索引>上半年;
stream2>>下半段;
int S=0,S1=0;
而(前半部分>0)
{
整数位数=前半部分%10;
前半部分/=10;
S+=数字;
}
而(后半部分>0)
{
整数位数=第二个半%10;
后半部分/=10;
S1+=数字;
}
如果(S>S1)
{
std::难道我们不是一个网站,你可以为你的家庭作业获得免费代码。请提供你迄今为止尝试过的内容!请查看你的评论。它可读吗?请用代码编辑你的帖子,并与你评论中的代码进行比较。请编辑问题,并将代码放入itI中。我是新的,不知道如何。我已经在阅读了。可以。对不起f或者不方便现在甚至都不考虑代码。首先用纸和铅笔开始解决问题。一旦你用它解决了问题,然后将其转换为伪代码。然后将其转换为真实代码。我很好。谢谢。我知道如何改进代码。非常感谢你向我展示这个方法。看起来是正确的,但为什么要引入昂贵的代码t->string->int转换和循环,当你可以通过ceil(log10(number))
获得数字的#时?@loro:log10()
函数也很昂贵。你必须选择你的毒药。:-)
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string text_number;
std::cout << "Enter Number: ";
std::cin>>text_number;
const unsigned int length = text_number.length();
const unsigned int half_length = length / 2;
std::string part1;
std::string part2;
unsigned int index;
for (index = 0; index < half_length; ++index)
{
part1 += text_number[index];
}
for (; index < length; ++index)
{
part2 += text_number[index];
}
std::istringstream stream1(part1);
std::istringstream stream2(part2);
int first_half;
int second_half;
stream1 >> first_half;
stream2 >> second_half;
int S = 0,S1 =0;
while(first_half>0)
{
int digit = first_half % 10;
first_half /= 10;
S += digit;
}
while(second_half>0)
{
int digit = second_half % 10;
second_half /= 10;
S1 += digit;
}
if(S > S1)
{
std::cout <<"First half half sum:" << S;
}else {
std::cout <<"second half sum:" << S1;
}
return 0;
}