1(1次方)+2(2次方)+3(3次方)..+n(n次方)

2009-07-18 7:30 am
如題 我想用C寫出標題的程式
不能使用公式 也不能使用數學函式庫
誰能幫幫我 3Q~~
更新1:

網友大大你好 你的程式執行時閃一個畫面就沒了 怎麼回事呢~"~

更新2:

bean大大你好 感謝你的詳解 我有兩個地方看不懂~"~ power *= n;跟sum += int_pow( j ); 這兩句是什麼意思呢

回答 (3)

2009-07-18 7:50 am
✔ 最佳答案
參考看看
#include <stdio.h>

int main() {
int i = 0;
int j = 0;
int sum = 0;

printf("Please input a number : ");
scanf("%d",&i);
for( j = 0; j < i; i--) {
sum = sum + ( i * i );
}
printf("Sum is : %d\n",sum);

return 0;
}

2009-07-18 00:03:57 補充:
我好像看錯了
sorry

2009-07-18 10:26:05 補充:
早上好
參考看看
#include
int retNum(int i);
int main() {
int i, j, y, z;
int sum = 0;

int retNum(int i) {
int tmp = i;
y = 1;
while( y < i ) {
tmp = tmp * i;
y++;
}
return tmp;
}

2009-07-18 10:26:12 補充:
printf("Please input a number : ");
scanf("%d",&i);
for( j = 0; j < i; i--) {
z = retNum(i);
sum = sum + z;
}
printf("Sum is : %d\n",sum);

return 0;
}

2009-07-18 10:26:39 補充:
報告完畢

2009-07-18 10:27:37 補充:
include stdio.h 不見了,請自加

2009-07-18 13:53:47 補充:
請在最後一個{
上加
system("PAUSE");
謝謝

2009-07-18 13:55:44 補充:
也就是
return 0;
system("PAUSE");
}

2009-07-19 12:49:25 補充:
錯了,是
system("PAUSE");
return 0;
}
2009-07-18 2:10 pm
#include <stdio.h>

int int_pow( int n )
{
int power=n;
int i=1;

for ( ; i < n; i++ ){
power *= n;
}

return power;
}

int main() {
int i = 0;
int j = 0;
int sum = 0;

printf("Please input a number : ");
scanf("%d",&i);
for( j = 1; j <= i; j++) {
sum += int_pow( j );
}
printf("Sum is : %d\n",sum);
return 0;
}





2009-07-19 12:26:28 補充:
power *= n; is effectively the same as:
power = power * n;

sum += int_pow(j); is the same as:
sum = sum + int_pow(j);

2009-07-19 12:29:17 補充:
Operators such as +=, -+, /=, *=, %=, &=, ^=, |=, <<= and >>= are all assignment operators. For better descriptions and examples, try:
"The C Programming Language" by Brain Kernighan and Dennis Ritchie.

2009-07-19 12:36:02 補充:
Operators such as +=, -+, /=, ...

Should read:

Operators such as +=, -=, /=, ...

Sorry about the typo
2009-07-18 11:49 am
不能使用公式 也不能使用數學函式庫...
用遞迴就可以了...次方數另外寫一個迴圈去call就好了...


收錄日期: 2021-04-30 13:37:47
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090717000015KK12901

檢視 Wayback Machine 備份