简体   繁体   English

交叉和计算,任何人都可以解释代码吗?

[英]Cross sum calculation, Can anyone explain the code please?

i'm going to learn C++ at the very beginning and struggling with some challenges from university. 我将从一开始就学习C ++,并且正面临大学带来的一些挑战。 The task was to calculate the cross sum and to use modulo and divided operators only. 任务是计算叉和,仅使用模和除法运算符。

I have the solution below, but do not understand the mechanism.. Maybe anyone could provide some advice, or help to understand, whats going on. 我在下面有解决方案,但不了解其机制。也许任何人都可以提供一些意见或帮助了解发生了什么情况。

I tried to figure out how the modulo operator works, and go through the code step by step, but still dont understand why theres need of the while statement. 我试图弄清楚模运算符是如何工作的,并逐步进行了代码编写,但是仍然不明白为什么需要while语句。

#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;
}

Lets say my input number is 27. cross sum is 9 可以说我的输入数字是27。交叉和是9

frist step: crossSum = crossSum + (input'27' % 10 ) // 0 + (modulo10 of 27 = 7) = 7 第一步: crossSum = crossSum + (input'27' % 10 ) // 0 + (modulo10 of 27 = 7) = 7

next step: input = input '27' / 10 // (27 / 10) = 2.7; Integer=2 ? 下一步: input = input '27' / 10 // (27 / 10) = 2.7; Integer=2 ? input = input '27' / 10 // (27 / 10) = 2.7; Integer=2 ?

how to bring them together, and what does the while loop do? 如何将它们组合在一起,而while循环有什么作用? Thanks for help. 感谢帮助。

Just in case you're not sure: 以防万一您不确定:

The modulo operator , or % , divides the number to its left by the number to its right (its operands ), and gives the remainder. 运算符将其左侧的数字除以其右侧的数字(其操作数 ),然后得到余数。 As an example, 49 % 5 = 4 . 例如, 49%5 = 4

Anyway, 无论如何,

The while loop takes a conditional statement, and will do the code in the following brackets over and over until that statement becomes false. while循环采用条件语句,并将不断重复以下括号中的代码,直到该语句变为false。 In your code, while the input is not equal to zero , do some stuff. 在您的代码中,当输入不等于零时 ,请执行一些操作。

To bring all of this together, every loop, you modulo your input by 10 - this will always return the last digit of a given Base-10 number. 为了将所有这些结合在一起,在每个循环中,您都将输入模数乘以10-这将始终返回给定的Base-10数字的最后一位数字。 You add this onto a running sum (crossSum), and then divide the number by 10, basically moving the digits over by one space. 将其添加到一个连续的总和(crossSum),然后将数字除以10,基本上将数字移一个空格。 The while loop makes sure that you do this until the number is done - for example, if the input is 104323959134, it has to loop 12 times until it's got all of the digits. while循环确保您执行此操作直到数字结束为止-例如,如果输入为104323959134,则必须循环12次,直到获得所有数字为止。

It seems that you are adding the digits present in the input number. 似乎您要添加输入数字中存在的数字。 Let's go through it with the help of an example, let input = 154. 让我们借助一个示例来研究它,让input = 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 

Now the while loop will not be executed since input = 0. Keep a habit of dry running through your code. 现在,由于输入= 0,将不会执行while循环。请养成在代码中空运行的习惯。

#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;
}

but still dont understand why theres need of the while statement 但仍然不明白为什么需要while语句

Actually, there isn't need (in literal sense) for, number of digits being representable is limited. 实际上, 不需要 (从字面意义上来说),可表示的位数是有限的。

Lets consider signed char instead of int : maximum number gets 127 then (8-bit char provided). 让我们考虑带signed char而不是int :然后最大数量为127(提供了8位char )。 So you could do: 因此,您可以执行以下操作:

crossSum = number % 10 + number / 10 % 10 + number / 100;

Same for int, but as that number is larger, you'd need 10 summands (32-bit int provided)... And: You'd always calculate the 10 summands, even for number 1, where actually all nine upper summands are equal to 0 anyway. 与int相同,但是随着数量的增加,您将需要10个被加数(提供了32位int)...并且:您始终会计算10个被加数,即使对于数字1,实际上所有九个高个被加数都是无论如何等于0。

The while loop simplifies the matter: As long as there are yet digits left, the number is unequal to 0, so you continue, and as soon as no digits are left (number == 0), you stop iteration: while循环简化了问题:只要还剩下数字,数字就不等于0,因此您继续操作,并且一旦没有数字(数字== 0),就停止迭代:

123 -> 12 -> 1 -> 0 // iteration stops, even if data type is able
  ^     ^    ^      // to store more digits

Marked digits form the summands for the cross sum. 标记的数字构成了求和的和。

Be aware that integer division always drops the decimal places, wheras modulo operation delivers the remainder, just as in your very first math lessons in school: 请注意,整数除法始终会舍弃小数位,wheras模运算会提供余数,就像您在学校的第一堂数学课一样:

7 / 3 = 2, remainder 1

So % 10 will give you exactly the last (base 10) digit (the least significant one), and / 10 will drop this digit afterwards, to go on with next digit in next iteration. 因此, % 10会给您确切的最后一位(基数10)数字(最低有效一位),并且/ 10将删除该数字,以继续下一次迭代中的下一位数字。

You even could calculate the cross sum according to different bases (eg 16; base 2 would give you the number of 1-bits in binary representation). 您甚至可以根据不同的基数(例如16;基数2将为您提供二进制表示的1位数字)来计算交叉和。

Loop is used when we want to repeat some statements until a condition is true. 当我们要重复某些语句直到条件为真时,使用循环 In your program, the following statements are repeated till the input becomes 0 . 在您的程序中,重复以下语句,直到输入变为0为止。

  • Retrieve the last digit of the input . 检索输入的最后一位。 ( int digit = input % 10; ) int digit = input % 10;
  • Add the above retrieved digit to crosssum . 将以上获取的数字添加到crosssum ( crosssum = crosssum + digit; ) crosssum = crosssum + digit;
  • Remove the last digit from the input . 输入中删除最后一位数字。 ( input = input / 10; ) input = input / 10;

The above statements are repeated till the input becomes zero by repeatedly dividing it by 10. And all the digits in input are added to crosssum . 重复上述语句,直到通过将其重复除以10来将输入变为为止,然后将输入中的所有数字加到crosssum上

Hence, the variable crosssum is the sum of the digits of the variable input . 因此,变量crosssum是变量input的数字之和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM