我在將這個作業問題轉換爲僞代碼時遇到了一些麻煩。我想知道這個邏輯是否正確。這個僞代碼的邏輯是否正確?
問題:
編寫一個程序,打印如下內容:1,3,5,7,9,11,13,15,17,19,在這之前用戶輸入的值。該程序不應該打印每行超過10個數字。打印的最後一行可能小於10.除了第一個數字和最後一個數字之外,應該有一個「,」將所有數字分開。每行上的最後一個數字後面應該有一個「。」。在退出之前,還應打印所有元素的總和:計算項目總數並輸入結果。例如,如果用戶輸入76的輸出應該如下 程序應儘可能的樣品運行如下:
輸入數(> = 1和< = 100):76 你的序列是
1,3,5,7,9,11,13,15,17,19。 21,23,25,27,29,31,33,35,37,39。 41,43,45,47 ,49,51,53,55,57,59,61,63 ,65,67,69,71,73,75
上述元素的總和1444。
我嘗試:
Algorithm ElementsSequenceSeries
// declare variables
Var num, count, odd, sum, arrayOne, arrayTwo, arrayThree, arrayFour, arrayFive
// initialize variables
Count = 0; sum = 0; odd=0; arrayOne = 0; arrayTwo = 0; arrayThree = 0; arrayFour = 0; arrayFive = 0;
<BeginAlg>
Print (Input 1<= num <= 100);
Count = count + 1;
Odd = (count*2)–1; // create a list of odd numbers up to a user-defined value less than 100
If (num > odd)
Print(Odd);
// For the indices 0 to 8, place a comma after each element of the array.
For (num <=20 and num> odd) and (arrayOne[9]= {0,1,2,3,4,5,6,7,8})
Print (arrayOne[]= {0,1,2,3,4,5,6,7,8} + 「,」);
EndFor
// For the index 9, place a fullstop after it.
For (num <=20 && num> odd) and (array One[]= 9)
Print arrayOne[]= 9 + 「.」)
EndFor
For (num <=40 and num> odd) and (arrayTwo[]= {0,1,2,3,4,5,6,7,8})
Print (arrayTwo[]= {0,1,2,3,4,5,6,7,8} + 「,」);
EndFor
For (num <=40 and num> odd) and (arrayTwo[]= 9)
Print arrayTwo[]= 9 + 「.」)
EndFor
For (num <=60 and num> odd) and (arrayThree[]={0,1,2,3,4,5,6,7,8})
Print (arrayThree[]= {0,1,2,3,4,5,6,7,8} + 「,」);
EndFor
For (num <=60 and num> odd) and (arrayThree[]= 9)
Print arrayThree[]= 9 + 「.」)
EndFor
For (num <=80 and num> odd) and (arrayFour[]={0,1,2,3,4,5,6,7,8})
Print (arrayFour []= {0,1,2,3,4,5,6,7,8} + 「,」);
EndFor
For (num <=80 and num> odd) and (arrayFour []= 9)
Print arrayFour []= 9 + 「.」)
EndFor
For (num <=100 and num> odd) and (arrayFive[]={0,1,2,3,4,5,6,7,8})
Print (arrayFive []= {0,1,2,3,4,5,6,7,8} + 「,」);
EndFor
For (num <=100 and num> odd) and (arrayFive []= length)
Print arrayFive []= length + 「.」);
EndFor
EndIf
Sum = arrayOne, arrayTwo, arrayThree, arrayFour, arrayFive;
Print(sum);
<EndAlg>
我不明白count = 0;請儘快解釋。爲什麼count = 0?我們沒有遞增嗎?印刷品(「n。」)不應該每行有一個?我對編程還不熟悉,請耐心等待。我感謝你的時間和精力。 – TechFool121 2014-10-02 12:25:05
嘿,出現了一個錯誤,我現在修正了它,實際上count是用來跟蹤打印在一行中的元素的數量,因爲它給出的問題是程序應該打印小於或者小於max。連續10個元素,將計數重置爲0,它變爲10(10個元素爲0-9)。 – abhinash 2014-10-02 12:52:42
嗨「n」用於存儲用戶輸入的號碼。在整個計劃中它是不變的。因爲我們需要在n之前打印所有的奇數,所以我們需要一個不斷變化的變量,這是由「我」在這裏完成的。在每一個循環之後,它會以2遞增。即,對於第一個循環,它將是1.對於第二個循環,它將是3,直到它小於n。所以我們需要打印我才能完成我們的工作。當循環到期時,我們在n之前打印所有數字,因此需要打印n一次。 – abhinash 2014-10-02 12:55:11