有人告诉我,写程序,生成唯一的5位数字(例如12345是唯一的,11234不)
我有写下面的代码产生独特的5位数号码
#include <iostream>
#include <stdlib.h>
#include <map>
using namespace std;
using std::rand;
const int k=99999-10234;
bool unique(int t){
map<int,int>my;
map<int,int>::iterator it;
while (t!=0){
++my[t%10];
t/=10;
}
for (it=my.begin();it!=my.end();it++){
if (it->second>0) {
return false;
}
}
return true;
}
int main(){
int m=0;
m= 10234+ rand()%k;
if (unique(m)){
cout<<" unique number was generated =:"<<m<<" ";
}
else{
do{
m=10234+rand()%k;
if (unique(m)){
cout<<"unique number was generated"<<m<<" ";
break;
}
} while(!unique(m));
}
return 0;
}
,但它不告诉我任何输出请帮我在我的代码中有什么不好?
需要'家庭作业'标签吗? – 2010-08-05 11:39:32