簡體   English   中英

交叉和計算,任何人都可以解釋代碼嗎?

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

我將從一開始就學習C ++,並且正面臨大學帶來的一些挑戰。 任務是計算叉和,僅使用模和除法運算符。

我在下面有解決方案,但不了解其機制。也許任何人都可以提供一些意見或幫助了解發生了什么情況。

我試圖弄清楚模運算符是如何工作的,並逐步進行了代碼編寫,但是仍然不明白為什么需要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;
}

可以說我的輸入數字是27。交叉和是9

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

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

如何將它們組合在一起,而while循環有什么作用? 感謝幫助。

以防萬一您不確定:

運算符將其左側的數字除以其右側的數字(其操作數 ),然后得到余數。 例如, 49%5 = 4

無論如何,

while循環采用條件語句,並將不斷重復以下括號中的代碼,直到該語句變為false。 在您的代碼中,當輸入不等於零時 ,請執行一些操作。

為了將所有這些結合在一起,在每個循環中,您都將輸入模數乘以10-這將始終返回給定的Base-10數字的最后一位數字。 將其添加到一個連續的總和(crossSum),然后將數字除以10,基本上將數字移一個空格。 while循環確保您執行此操作直到數字結束為止-例如,如果輸入為104323959134,則必須循環12次,直到獲得所有數字為止。

似乎您要添加輸入數字中存在的數字。 讓我們借助一個示例來研究它,讓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 

現在,由於輸入= 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;
}

但仍然不明白為什么需要while語句

實際上, 不需要 (從字面意義上來說),可表示的位數是有限的。

讓我們考慮帶signed char而不是int :然后最大數量為127(提供了8位char )。 因此,您可以執行以下操作:

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

標記的數字構成了求和的和。

請注意,整數除法始終會舍棄小數位,wheras模運算會提供余數,就像您在學校的第一堂數學課一樣:

7 / 3 = 2, remainder 1

因此, % 10會給您確切的最后一位(基數10)數字(最低有效一位),並且/ 10將刪除該數字,以繼續下一次迭代中的下一位數字。

您甚至可以根據不同的基數(例如16;基數2將為您提供二進制表示的1位數字)來計算交叉和。

當我們要重復某些語句直到條件為真時,使用循環 在您的程序中,重復以下語句,直到輸入變為0為止。

  • 檢索輸入的最后一位。 int digit = input % 10;
  • 將以上獲取的數字添加到crosssum crosssum = crosssum + digit;
  • 輸入中刪除最后一位數字。 input = input / 10;

重復上述語句,直到通過將其重復除以10來將輸入變為為止,然后將輸入中的所有數字加到crosssum上

因此,變量crosssum是變量input的數字之和。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM