利用二进制文件保存10个100以内的随机整数,要求保存的所有值各不相同

来源:百度知道 编辑:UC知道 时间:2024/06/21 06:05:30

该程序仅限于100以内的十进制整数。
已运行过。
#include<iostream>
#include<vector>
#include<fstream>
#include<time.h>
using namespace std;
void main()
{
vector<int> s;
char *str=new char[];
ofstream out1("二进制整数.txt");
ofstream out2("十进制整数.txt");
srand((unsigned)time(NULL));
s.push_back(rand()%100);
out2<<s[0]<<endl;
for(;s.size()<10;)
{
int k=rand()%100;
for(int j=0;j<s.size();j++)
{
if(k==s[j])
break;
}
if(j==s.size())
{
s.push_back(k);
out2<<s[j]<<endl;
}
}
for(int m=0;m<s.size();m++)
{
itoa(s[m],str,2);
out1<<str<<endl;
}
cout<<"你要的数据已存入程序所在目录。。。"<<endl;
}