进来看看下面的程序有什么错误
我想用一个数组存储26个不同的数(随机产生) ,然后输出数组,一下是程序:
看了很长时间看不错错在哪?????
#include "stdafx.h "
#include <iomanip>
using namespace std;
bool have(int a[],int b);
int m;
void main()
{
int res[26];//存储26个数的数组
for(int i=0;i <26;i++)
{
int x=rand()%26;
if(have(res,x))//判断随即到的数是否在res中
{
m=i;
i--;//如果在德话,相当于这次循环没进行,i--
}
else
res[i]=x;
}
for(int i=0;i <26;i++)
{
cout < <setw(5) < <res[i];
if((1+i)%5==0)
cout < <endl;
}
cout < <endl;
}
bool have(int a[],int b)
{
for(int i=0;i <m;i++)
if(a[i]==b)
return true;
return false;
}
[解决办法]
for(int i=0;i <26;i++, m++) //m!!
{
int x=rand()%26;
if(have(res,x))//判断随即到的数是否在res中
{
m--; //m!!!
i--;//如果在德话,相当于这次循环没进行,i--
}
......
[解决办法]
其实不需要 m ,
把 i 传递进去就可以了:
bool have(int a[],int b, int len);
int main()
{
int res[26];//存储26个数的数组
for(int i=0;i <26;i++)
{
int x=rand()%26;
if(have(res,x, i))//判断随即到的数是否在res中
{
i--;//如果在德话,相当于这次循环没进行,i--
}
else
res[i]=x;
}
for(int i=0;i <26;i++)
{
cout < <setw(5) < <res[i];
if((1+i)%5==0)
cout < <endl;
}
cout < <endl;
system( "pause ");
return 0;
}
bool have(int a[],int b, int len)
{
for(int i=0;i <len;i++)
if(a[i]==b)
return true;
return false;
}
进来看看下面的程序有什么东东
进来看看下面的程序有什么错误我想用一个数组存储26个不同的数(随机产生),然后输出数组,一下是程序:看了很
