當(dāng)前位置:高考知識(shí)網(wǎng) > 招聘筆試題 > 正文
以下是我的第二種寫法,用空間換了點(diǎn)時(shí)間,用C++語(yǔ)言寫的:
1. include
1. include
using namespace std;
class Rand4{
private:
int a[10];
int getNum(int index){
while(a[index]==-1){
++index;
index %= 10;
}
int ret = a[index];
a[index] = -1;
return ret;
};
void init(){
for(int i=0;i<10;++i){
a[i] = i;
}
};
public:
int Next(){
init();
int ret = (rand()%10) *1000;
int index = 100;
int time = 3;
while(time>0){
ret += getNum((rand()%10))*index;
index /= 10;
--time;
}
return ret;
};
};
int main(int argc, char *argv[])
{
Rand4 r4;
cout<
cout<
cout<
system("PAUSE");
return 0;
}
此法的時(shí)間復(fù)雜度為正宗的O(n),空間開(kāi)銷(不包含調(diào)用庫(kù)函數(shù)的)就比前一個(gè)例子多了些.
此法在n取m的隨機(jī)計(jì)算中,m從0一直到趨于n, 效率都是不變的.
其他的高效方法暫時(shí)還沒(méi)有想出.
金山職業(yè)技術(shù)學(xué)院對(duì)比四川汽車職業(yè)技術(shù)學(xué)院哪個(gè)好 附分..
時(shí)間:2025-05-22 09:08:12成都銀杏酒店管理學(xué)院在重慶高考招生計(jì)劃人數(shù)和專業(yè)代..
時(shí)間:2025-05-22 09:05:01四川上山東理工大學(xué)多少分 分?jǐn)?shù)線及排名
時(shí)間:2025-05-22 09:01:18江西高考理科533分排名多少 排名多少位次
時(shí)間:2025-05-22 08:57:21大連東軟信息學(xué)院對(duì)比甘肅民族師范學(xué)院哪個(gè)好 附分?jǐn)?shù)線..
時(shí)間:2025-05-22 08:53:39廣東高考455至460分左右物理可以上什么大學(xué)
時(shí)間:2025-05-22 08:49:52