[問題] 十進位轉二進位

看板C_and_CPP (C/C++)作者 (中肯56)時間14年前 (2011/03/09 22:03), 編輯推噓4(405)
留言9則, 7人參與, 最新討論串5/5 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) c++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 這是我寫的一個十進位轉二進位的程式,小的數字都還ok正常但是只要輸入一個 超大的數執行完通常都沒有結果,我覺得是不是因為陣列的容量太小? 有沒有辦法可以解決 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) #include<iostream> #include<cstdlib> using namespace std; void tentotwo(int,int[]); void tentotwo(int num,int matrix[]){ int i,count; count=0; while(num>=1){ matrix[count]=num%2; num=num/2; count++; } count--; for(i=count;i>=0;i--){ cout<<matrix[i]<<""; } } int main(void){ int num; int *matrix=new int[5000000]; cout<<"輸入一個數字"<<endl; cin>>num; tentotwo(num,matrix); system("pause"); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.204.250.31

03/09 22:04, , 1F
你知道你的可以放幾位數嗎?
03/09 22:04, 1F
我輸入1000000000都還OK在多一點就不行 ※ 編輯: wuwu66 來自: 123.204.250.31 (03/09 22:07)

03/09 22:18, , 2F
大數運算:http://0rz.tw/ba3Xx
03/09 22:18, 2F

03/09 22:18, , 3F
輸入1000000000, 那count等於多少?
03/09 22:18, 3F
29 ※ 編輯: wuwu66 來自: 123.204.250.31 (03/09 22:23)

03/09 22:57, , 4F
十進位轉二進位直接右位移就出來了吧,不用除那麼辛苦啦
03/09 22:57, 4F

03/09 23:00, , 5F
new int[5000000] 也太扯了
03/09 23:00, 5F

03/09 23:03, , 6F
我是想說 數字可以到幾位數啦呵呵
03/09 23:03, 6F

03/09 23:52, , 7F
用shift和AND邏輯就可以,不過輸入的整數有上限。
03/09 23:52, 7F

03/10 01:01, , 8F
電腦裡的東西都是用二進位存阿
03/10 01:01, 8F

03/14 18:39, , 9F
long long -> 2^63 int 2^31....
03/14 18:39, 9F
文章代碼(AID): #1DTuYrae (C_and_CPP)
文章代碼(AID): #1DTuYrae (C_and_CPP)