發新話題
打印

懇請老師協助指正程式撰寫的錯誤-用迴圈跑三次方程式

複製內容到剪貼板
代碼:
int s=0,t=0,u=0,v=0;w=0;
w 前面的逗號,誤打成分號了。
複製內容到剪貼板
代碼:
                    s = (int)(pow(a,3));
                    t = (int)(pow(b,3));
                    u = (int)(pow(c,3));
                    v = (int)s+t+u;
                    w = (int)(pow(d,3));
在 a,b,c,d 前面都加一個 (float) 吧。變成
複製內容到剪貼板
代碼:
                    s = (int)(pow((float)a,3));
                    t = (int)(pow((float)b,3));
                    u = (int)(pow((float)c,3));
                    v = (int)s+t+u;
                    w = (int)(pow((float)d,3));

多喝水。

TOP

引用:
原帖由 ksjeng 於 2010-2-12 12:23 AM 發表
老師晚安
謝謝您撥冗指正
還有一個小問題
底下這一行程式說它是錯誤的
x                      std::cout  
在第一行加上
複製內容到剪貼板
代碼:
#include <cstdlib>

多喝水。

TOP

我執行出來的結果是
複製內容到剪貼板
代碼:
0000
0011
0022
0033
0044
0055
0066
0077
0088
0099
0101
0202
0303
0404
0505
0606
0707
0808
0909
1001
1689
1869
2002
3003
3456
3546
4004
4356
4536
5005
5346
5436
6006
6189
6819
7007
8008
8169
8619
9009
那些有兩個變數值是零的顯然會成立,

除了  \(\displaystyle 3^3+4^3+5^3=216=6^3,\)

似乎還有一個  \(\displaystyle 1^3+6^3+8^3=729=9^3\) 也會成立。

多喝水。

TOP

複製內容到剪貼板
代碼:
#include <cstdlib>
#include <iostream>
#include <cmath>

int main(int argc, char *argv[])
{
    int a=0,b=0,c=0,d=0;
    int s=0,t=0,u=0,v=0,w=0;
    for (a=0;a<10;a++)
        for (b=0;b<10;b++)
            for (c=0;c<10;c++)
                for (d=0;d<10;d++)
                {
                    s = (int)(pow((float)a,3));
                    t = (int)(pow((float)b,3));
                    u = (int)(pow((float)c,3));
                    v = (int)s+t+u;
                    w = (int)(pow((float)d,3));
                    if (v==w)
                       std::cout << a << b << c << d << "\n";
                }
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
我是套用你寫的程式而已(如上),

執行結果的確如之前回覆,有 3456 這一組。

IDE 與編譯器使用的是 Dev C++ 4.9.9.2 with Mingw/GCC 3.4.2

我想應該就是你所指的 D++4.9.9.2 。

下載網址:http://www.bloodshed.net/dev/devcpp.html

何以結果不同,這我就不知道了。

多喝水。

TOP

剛剛仔仔細看了一下,

在標頭檔後,我的檔案裡還有一句,
複製內容到剪貼板
代碼:
using namespace std;
看來有可能是差在這裡,呵。

多喝水。

TOP

1. 沒有建立 project?(不知是否會有影響)

2. 資料夾有中文名稱?(不知是否會有影響)

3. 為什麼你是用 .c 而非 .cpp,難不成開新專案時你是選 〝C Project〞 而非 〝C++ Project〞?(不知是否會有影響)

可以新建一個 C++ 的 Project,然後放在路徑沒有中文的資料夾,再踹踹看囉。

多喝水。

TOP

發新話題