學校作業要改一個抽獎的程式 但自己太爛 不知道要怎麼改 上網找了一些文章還是不知道如何弄 希望能幫忙解的大神可以用C++改
# include <iostream>
using namespace std;
int main(){
//設定變數
int i, j, n, num, t=10, temp;
int lotto[t][8];
srand(time(NULL));
//製作大樂透號碼
for(n=0; n<t; n++){
lotto[n][7]=n+1;
for(i=0; i<7; i++){
temp = rand()%49+1;
for(j=0; j<i; j++){
if(temp == lotto[n][j]){
i--;
break;
}
}
if(temp != lotto[n][j])
lotto[n][i] = temp;
}
}
//排好
for(n=0; n<t; n++){
for (i = 0; i < 5; ++i) {
for (j = 0; j < 5-i; ++j) {
if (lotto[n][j] > lotto[n][j+1]) {
temp = lotto[n][j];
lotto[n][j] = lotto[n][j+1];
lotto[n][j+1] = temp;
}
}
}
}
//打出
for(n=0; n<t; n++){
cout<<endl<<"第"<<n+1<<"期"<<endl;
cout<<"大樂透號碼 : ";
for(i=0; i<6; i++)
cout<<" "<< lotto[n][i];
cout<<endl<<" 特別號 : "<< lotto[n][i];
}
//查詢
cout<<endl<<"請輸入要查詢的期別: ";
cin>>num;
//打出那一列
for(n=0; n<t; n++){
if (lotto[n][7] == num){
cout<<"第"<<num<<"期"<<endl;;
cout<<"大樂透號碼 : ";
for(i=0; i<6; i++)
cout<<" "<< lotto[n][i];
cout<<endl<<" 特別號 : "<<lotto[n][i];
break;
}
}
//如果不行
if(n == t)
cout<<"查無該期對獎號碼 ";
return 0;
}
還有 其實我之前就有寫過一種二元搜尋,能用我之前的方法改嗎(不行也沒關系)?
舊的寫法(這寫法就單純的搜尋):
#include <iostream>
#include <cstdlib>
using namespace std;
int BinarySearch (int data[],int,int);
int main(){
int search, ans;
int data[]={3,7,14,20,23,32,41,44,56,57,73,89,93};
cout<<"原始資料有:\n";
for(int i=0;i<13;i++)
cout<<data[i]<<",";
cout<<endl;
cout<<"請輸入欲搜尋的資料:";
cin>>search;
ans=BinarySearch(data,search,sizeof(data)/sizeof(int));
if(ans<0){
cout<<"找不到"<<search<<"沒有這筆資料"<<endl;
}
else{
cout<<"在第"<<ans+1<<"筆資料找到"<<search<<endl;
}
return 0;
}
int BinarySearch(int data[],int search,int n)
{
int low=0,high=n-1;
while(low<=high){
int mid=(low+high)/2;
if(data[mid]==search){
return mid;
}
else if(data[mid]>search){
high=mid-1;
}
else if (data[mid]<search){
low=mid+1;
}
}
return -1;
}
非常感謝願意看到這裡的大大們
# include <iostream>
using namespace std;
int BinarySearch(int data[][8],int search,int n);
int main(){
//設定變數
int i, j, n, num, t=10, temp;
int lotto[t][8];
srand(time(NULL));
//製作大樂透號碼
for(n=0; n<t; n++){
lotto[n][7]=n+1;
for(i=0; i<7; i++){
temp = rand()%49+1;
for(j=0; j<i; j++){
if(temp == lotto[n][j]){
i--;
break;
}
}
if(temp != lotto[n][j])
lotto[n][i] = temp;
}
}
//排好
for(n=0; n<t; n++){
for (i = 0; i < 5; ++i) {
for (j = 0; j < 5-i; ++j) {
if (lotto[n][j] > lotto[n][j+1]) {
temp = lotto[n][j];
lotto[n][j] = lotto[n][j+1];
lotto[n][j+1] = temp;
}
}
}
}
//打出
for(n=0; n<t; n++){
cout<<endl<<"第"<<n+1<<"期";
cout<<"大樂透號碼 : ";
for(i=0; i<6; i++)
cout<<" "<< lotto[n][i];
cout<<" 特別號 : "<< lotto[n][i];
}
//查詢
cout<<endl<<endl<<"請輸入要查詢的期別: ";
cin>>num;
//打出那一列
int find;
find = BinarySearch(lotto, num, t);
if (find==-1) {
cout<<"查無 "<<num<<" 期對獎號碼";
} else {
cout<<"第 "<<num<<" 期"<<endl;;
cout<<"大樂透號碼 : ";
for(i=0; i<6; i++)
cout<<" "<< lotto[find][i];
cout<<" 特別號 : "<<lotto[find][i];
}
/*
for(n=0; n<t; n++){
if (lotto[n][7] == num){
cout<<"第"<<num<<"期"<<endl;;
cout<<"大樂透號碼 : ";
for(i=0; i<6; i++)
cout<<" "<< lotto[n][i];
cout<<endl<<" 特別號 : "<<lotto[n][i];
break;
}
}
//如果不行
if(n == t)
cout<<"查無該期對獎號碼 ";
*/
return 0;
}
int BinarySearch(int data[][8],int search,int n)
{
int low=0,high=n-1;
while(low<=high){
int mid=(low+high)/2;
if(data[mid][7]==search){
return mid;
}
else if(data[mid][7]>search){
high=mid-1;
}
else if (data[mid][7]<search){
low=mid+1;
}
}
return -1;
}