交叉和计算,有人能解释一下代码吗? 我将在一开始就学习C++,并努力应对大学的挑战。 任务是计算交叉和,并仅使用模和除法运算符
我有下面的解决方案,但不了解其机制。。 也许任何人都可以提供一些建议,或者帮助理解正在发生的事情 我试图弄清楚模运算符是如何工作的,并一步一步地检查代码,但仍然不明白为什么需要while语句交叉和计算,有人能解释一下代码吗? 我将在一开始就学习C++,并努力应对大学的挑战。 任务是计算交叉和,并仅使用模和除法运算符,c++,C++,我有下面的解决方案,但不了解其机制。。 也许任何人都可以提供一些建议,或者帮助理解正在发生的事情 我试图弄清楚模运算符是如何工作的,并一步一步地检查代码,但仍然不明白为什么需要while语句 #include <iostream> using namespace std; int main() { int input; int crossSum = 0; cout << "Number please: " << endl; cin &g
#include <iostream>
using namespace std;
int main()
{
int input;
int crossSum = 0;
cout << "Number please: " << endl;
cin >> input;
while (input != 0)
{
crossSum = crossSum + input % 10;
input = input / 10;
}
cout << crossSum << endl;
system ("pause");
return 0;
}
#包括
使用名称空间std;
int main()
{
int输入;
int交叉和=0;
不能输入;
while(输入!=0)
{
交叉和=交叉和+输入%10;
输入=输入/10;
}
cout似乎您正在添加输入数字中的数字。让我们通过一个示例来了解它,让输入=154
Iteration1
crossSum= 0 + 154%10 = 4
Input = 154/10= 15
Iteration2
crossSum = 4 + 15%10 = 9
Input = 15/10 = 1
Iteration3
crossSum = 9 + 1%10 = 10
Input = 1/10 = 0
现在,由于输入=0,将不会执行while循环。请保持干练地运行代码的习惯。似乎您正在添加输入编号中的数字。让我们借助一个示例,让输入=154,来完成它
Iteration1
crossSum= 0 + 154%10 = 4
Input = 154/10= 15
Iteration2
crossSum = 4 + 15%10 = 9
Input = 15/10 = 1
Iteration3
crossSum = 9 + 1%10 = 10
Input = 1/10 = 0
#include <iostream>
using namespace std;
int main()
{
int input;
int crossSum = 0;
cout << "Number please: " << endl;
cin >> input;
while (input != 0) // while your input is not 0
{
// means that when you have 123 and want to have the crosssum
// you first add 3 then 2 then 1
// mod 10 just gives you the most right digit
// example: 123 % 10 => 3
// 541 % 10 => 1 etc.
// crosssum means: crosssum(123) = 1 + 2 + 3
// so you need a mechanism to extract each digit
crossSum = crossSum + input % 10; // you add the LAST digit to your crosssum
// to make the number smaller (or move all digits one to the right)
// you divide it by 10 at some point the number will be 0 and the iteration
// will stop then.
input = input / 10;
}
cout << crossSum << endl;
system ("pause");
return 0;
}
现在,由于input=0,while循环将不会执行。请保持干练地运行代码的习惯。\include
#include <iostream>
using namespace std;
int main()
{
int input;
int crossSum = 0;
cout << "Number please: " << endl;
cin >> input;
while (input != 0) // while your input is not 0
{
// means that when you have 123 and want to have the crosssum
// you first add 3 then 2 then 1
// mod 10 just gives you the most right digit
// example: 123 % 10 => 3
// 541 % 10 => 1 etc.
// crosssum means: crosssum(123) = 1 + 2 + 3
// so you need a mechanism to extract each digit
crossSum = crossSum + input % 10; // you add the LAST digit to your crosssum
// to make the number smaller (or move all digits one to the right)
// you divide it by 10 at some point the number will be 0 and the iteration
// will stop then.
input = input / 10;
}
cout << crossSum << endl;
system ("pause");
return 0;
}
使用名称空间std;
int main()
{
int输入;
int交叉和=0;
不能输入;
while(input!=0)//当您的输入不是0时
{
//这意味着当你有123并且想要得到交叉和
//先加3,再加2,再加1
//mod 10只提供最正确的数字
//示例:123%10=>3
//541%10=>1等。
//交叉和是指:交叉和(123)=1+2+3
//因此,您需要一种机制来提取每个数字
交叉和=交叉和+输入%10;//将最后一位数字添加到交叉和中
//使数字变小(或将所有数字向右移动一位)
//你在某一点将它除以10,数字将是0,迭代
//我会停下来的。
输入=输入/10;
}
不能包含
使用名称空间std;
int main()
{
int输入;
int交叉和=0;
不能输入;
while(input!=0)//当您的输入不是0时
{
//这意味着当你有123并且想要得到交叉和
//先加3,再加2,再加1
//mod 10只提供最正确的数字
//示例:123%10=>3
//541%10=>1等。
//交叉和是指:交叉和(123)=1+2+3
//因此,您需要一种机制来提取每个数字
交叉和=交叉和+输入%10;//将最后一位数字添加到交叉和中
//使数字变小(或将所有数字向右移动一位)
//你在某一点将它除以10,数字将是0,迭代
//我会停下来的。
输入=输入/10;
}
不能以防你不确定:
模运算或%将其左侧的数字除以其右侧的数字(其操作数),并给出余数。例如,49%5=4
反正
while循环接受一个条件语句,并将反复执行以下括号中的代码,直到该语句变为false。在您的代码中,输入不等于零时,请执行一些操作
为了把所有这些结合在一起,每个循环,你把你的输入模化10-这将总是返回给定的10进制数的最后一位。你把它加到一个运行和(交叉和)上,然后将数字除以10,基本上将数字移动一个空格。while循环确保在数字完成之前执行此操作-例如,如果输入为104323959134,它必须循环12次,直到获得所有数字。以防您不确定:
模运算或%将其左侧的数字除以其右侧的数字(其操作数),并给出余数。例如,49%5=4
反正
while循环接受一个条件语句,并将反复执行以下括号中的代码,直到该语句变为false。在您的代码中,输入不等于零时,请执行一些操作
为了把所有这些结合在一起,每个循环,你把你的输入模化10-这将总是返回给定的10进制数的最后一位。你把它加到一个运行和(交叉和)上,然后将数字除以10,基本上将数字移动一个空格。while循环确保在数字完成之前执行此操作-例如,如果输入为104323959134,则必须循环12次,直到获得所有数字
但还是不明白为什么需要while语句
实际上,不需要(从字面意义上讲),因为可表示的位数是有限的
让我们考虑<代码>符号char <代码>,而不是<代码> int >代码>:最大数字为127(8-位<代码> char < /c> >)。因此,您可以这样做:
crossSum = number % 10 + number / 10 % 10 + number / 100;
对于int也是一样,但由于这个数字更大,所以需要10个求和(提供32位int)……而且:您总是要计算10个求和,即使是对于数字1,实际上所有九个上求和都等于0
while循环简化了这一问题:只要还有数字,数字就不等于0,因此您继续,一旦没有数字剩下(数字==0),您就停止迭代:
123 -> 12 -> 1 -> 0 // iteration stops, even if data type is able
^ ^ ^ // to store more digits
标记的数字构成交叉和的总和
请注意,整数除法总是会删除小数位数,而模运算则会提供余数,就像在学校的第一堂数学课上一样:
7 / 3 = 2, remainder 1
因此,%10
将准确地给出最后一位(以10为基数)数字(最低有效位),然后/10
将删除此数字,以继续下一个itera中的下一位数字