看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) OS 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) Xcode 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 為什麼輸入n,m無法讀入陣列 餵入的資料(Input): 18 6 預期的正確結果(Expected Output): 18 things is taken 6 at a time is 18564 exactly 錯誤結果(Wrong Output): 18564 things is taken 18564 at a time is 18564 exactly 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) // // main.cpp // sam // // Created by sam on 2019/9/3. // Copyright ꤠ2019 sam. All rights reserved. // #include<iostream> #include <stdio.h> using namespace std; long int lev(long int num){ long int a=1; for(int i=1;i<=num;i++){ a=a*i; } return a; } int main(){ int i=0,count=0; int n,m,k[i],l[i]; long int s[i]; long int num=1; while(cin>>n>>m){ if(n==0||m==0){ break; } k[i]=n; l[i]=m; if(m<(n/2)){ m=n-m; } for(long int i=n;i>m;i--){ num=num*i; } m=n-m; long int c=num/(lev(m)); s[i]=c; i++; count++; } for(int i=0;i<count;i++){ printf("%d things taken %d at a time is %ld exactly.\n",l[i],l[i],s[i]); } } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.237.61.190 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1574425290.A.84F.html
Lipraxde: 你在宣告陣列的時候 i = 0,然後輸出是不是有一個要是 11/22 20:34
Lipraxde: k[i] 啊? 11/22 20:34
zo6596001: int i =0; 然後下一行又 k[i]; 11/23 00:45
zo6596001: 仔細看一下 i 好像又重複宣告... 11/23 01:42
LPH66: 拿變數宣告陣列大小時變數的值會使用當下的值 11/23 02:23
LPH66: 而不會隨著後續更新而更新 11/23 02:23
LPH66: 比較簡單的解法是估計最大可能陣列大小先行留空 11/23 02:24
zo6596001: C語言記憶體的大小全部都要自己宣告 11/23 08:53
zo6596001: k[i]的意思其實是 宣告0個記憶體位置給k 11/23 08:54
zo6596001: 至於為什麼編得過、跑得了,就要去看產生的指令碼了 11/23 08:58
Lipraxde: num 這個變數看起來也怪怪的,是不是該放在 while 裡? 11/23 13:59