c programme factor c++

2010-10-23 10:12 am
用 visual studio
我想搵 17 的 factor
請問要打d咩code ?
更新1:

仍然吾識打.....

回答 (2)

2010-10-25 8:00 pm
✔ 最佳答案
因為沒 visual studio, 所以, 以下 code 只可以作參考:
#include <stdio.h>int main () {
int num = 17; /* the number wants to find its factors */
int count = 0; /* count of number of factor */
int factor[255]; /* use for store a limit number of factors */
/* if use a stack or link list may be stored */
/* unlimited number of factors */
int i; /* counter index */

/* find the factor(s) of num and store in the factor[] array */
for (i=1; i<=num; i++) {
if ( num % i == 0 ) {
factor[count] = i;
count++;
}
}

/* print the result */
printf("The factor(s) of %d is/are:\n", num);
for (i=0; i<count; i++) {
printf("%d ", factor[i]);
}

return 0;
}

以下是 output screen, compiled with Dev-C++ 4.9.9.2:
圖片參考:http://imgcld.yimg.com/8/n/HA00458611/o/701010230173313873379410.jpg
參考: myself
2010-10-23 10:38 am
int num = 17;
用for由int i = 1開始一個一個地除,如果餘數等於0(num % i == 0),把除數(i)和商(num / i)記錄,計到開方num為止

得到所有的除數和商就是答案

注:因為17是質數,只有兩個因數(factor)1和17


收錄日期: 2021-04-29 18:29:22
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20101023000051KK01733

檢視 Wayback Machine 備份