竹園論壇

標題: 請各位大大幫我debug [打印本頁]

作者: Panda_Liu    時間: 2014-8-12 16:04
標題: 請各位大大幫我debug
本帖最後由 Panda_Liu 於 2014-8-13 20:50 編輯

這個選號機每次都在跑到10組前就停止了
求各位學長幫我debug優化



[C++] 純文本查看 復制代碼
#include<iostream>
#include<ctime>
#include<windows.h>
using namespace std;
int main()
{
    int result,lol[5],rein=0,times,m=1;
    cout<<"LOL抽獎---幸運電腦選號機"<<endl<<"請輸入你要的組數"<<endl;
    cin>>times;
    system("cls");
    cout<<endl<<"選號結果如下"<<"共"<<times<<"組"<<"---祝您得到全角色及造型"<<endl<<endl;
    srand(time(NULL));
    while(times--)
    {
        for(int i=0;i<5;i++)
        {
            result=rand()%39+1;
            for(int n=0;n<i;n++)
            {
                if(lol[n]==result)
                {
                    rein=1;
                    break;
                }
            }
            if(rein==1)
                i--;
            lol=result;
            Sleep(10);
        }
        cout<<"第"<<m<<"組. ";
        for(int j=0;j<5;j++)
        {
            cout<<lol[j]<<" ";
            Sleep(200);
        }
        cout<<endl<<endl;
        m++;
    }        
        system("pause");
    return 0;  
}


By Sylveon 請愛用代碼插入的功能






電腦選號.zip

627 Bytes, 下載次數: 1

怪怪的會莫名停止


作者: domen111    時間: 2014-8-12 16:32
我並沒有仔細研究你的程式碼,所以不太知道rein這個變數是做什麼用的
每次只要執行到第22行rein被設為1,27行的i--會造成無窮迴圈
作者: Panda_Liu    時間: 2014-8-12 16:58
本帖最後由 Panda_Liu 於 2014-8-12 17:07 編輯
domen111 發表於 2014-8-12 16:32
我並沒有仔細研究你的程式碼,所以不太知道rein這個變數是做什麼用的
每次只要執行到第22行rein被設為1,27 ...

rein是用來檢查選出來的數字有沒有重複的if(lol[n]==result)
                {
                rein=1;
                break;
                }
這一段是檢查有沒有重複 有的話把rein設為1
                if(rein==1)
                i--;
                lol=result;
                Sleep(10);
如果有重複 則i--(重跑這一次) 這樣 他會跑到沒有重複為止(我把大括號省掉了)
如果沒有就存起來 輸出
那個Sleep別管他



作者: Sylveon    時間: 2014-8-12 17:12
C++ 沒有極限
[C++] 純文本查看 復制代碼
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<algorithm>

using namespace std;
//定義 MAX_SIZE 為 39
#define MAX_SIZE 39

int main()
{
        //初始化亂數  
        srand(time(NULL));
        //存放編號
        int card[MAX_SIZE];
        //先依照順序放好
        for(int i=0 ; i<MAX_SIZE ; ++i)
        {
                card=i+1;
        }
        //C++ 內建打亂 in algorithm
        random_shuffle(card,card+MAX_SIZE);
       
        cout<<"C++ 內建打亂 : "<<endl;
        for(int i=0; i<MAX_SIZE ;++i)
        {
                cout<<card<<' ';
        }
        cout<<endl;
       
}





歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2