請問有冇高手可以幫我將以下的指示轉成c++程式

2007-04-09 1:36 am
Step 1:Declare an integer constant N=5, k and HASH.
Step 2:Declare an one-dimentional array of character ORIGINAL[N].
Step 3:k=1
Step 4:As long as k<51, repeat steps 7 to 9.
Step 5: Accept a character from the keyboard.
Step 6: Assign the character to ORIGINAL(k), the K th element ot ORIGINAL.
Step 7: Increment k by 1.
Step 8:Initialise the variable HASH to be 0.
Step 9:Calculate HASH by the following formula:
HASH=1*ORIGINAL(1)+2*ORIGINAL(2)+...+5*ORIGINAL(5)

回答 (1)

2007-04-09 2:55 pm
✔ 最佳答案
唔知你係咪打錯或者打漏野,我覺得唔會成功咁run喎,你個k一開始係咪應該係0?step 4係咪 k < n,唔係 k < 51?

不過無論如何,希望下面幫到你:

const int n = 5;
int k, hash;
char c;
char original[n];
k = 1;
while (k < 51) {
scanf (" %c " ,&c);
original[k-1] = c;
k++;
hash = 0;
int i;
for (i = 0; i < 5; i++) {
hash += i * original[ i ];
}
}

2007-04-09 06:58:54 補充:
我覺得你既step number有錯,如果冇估錯,個for loop應該要響個while loop出面


收錄日期: 2021-04-25 19:54:31
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070408000051KK03163

檢視 Wayback Machine 備份