我想用随即函数输出1个9*9的矩阵,但是现在矩阵里的数字都是同1个数字。
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int temp=0;
int count=0;
#define N 9
int input(int x)
{
// int i,j;
srand((unsigned)time(NULL));
x=rand()%9+1;
return x;
}
/*int jiancha(int *p)
{
if(temp==*p)
{
input(p);
}
return ;
}*/
void output(int a[][N])
{
int i,j,count=0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%-3d",a[i][j]);
count++;
if(count%9==0)
printf("\n");
}
}
void main()
{
int row,col,ary[N][N];
int p;
// p=&ary[0][0];
for(row=0;row<N;row++)
for(col=0;col<N;col++)
{
p=0;
ary[row][col]=input(p);
// ary[row][col]=jiancha(p);
// p++;
}
output(ary);
}
----------------解决方案--------------------------------------------------------
我想用随即函数输出1个9*9的矩阵,但是现在矩阵里的数字都是同1个数字。
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int temp=0;
int count=0;
#define N 9
int input(int x)
{
// int i,j;
static int y;
y+=1; ``
srand(y);
x=rand()%9+1;
return x;
}
/*int jiancha(int *p)
{
if(temp==*p)
{
input(p);
}
return ;
}*/
void output(int a[][N])
{
int i,j,count=0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%-3d",a[i][j]);
count++;
if(count%9==0)
printf("\n");
}
}
void main()
{
int row,col,ary[N][N];
int p;
// p=&ary[0][0];
for(row=0;row<N;row++)
for(col=0;col<N;col++)
{
p=0;
ary[row][col]=input(p);
// ary[row][col]=jiancha(p);
// p++;
}
output(ary);
}
[此贴子已经被作者于2007-4-24 21:44:31编辑过]
----------------解决方案--------------------------------------------------------
结果还是一样
----------------解决方案--------------------------------------------------------
程序:
TC201 编译出来的....
不知道你用何编译器...
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#include<process.h>
#include<conio.h>
int temp=0;
int count=0;
#define N 9
int input(int x)
{
static int y=5;
y++;
srand(y);
x=rand()%9+1;
return x;
}
/*int jiancha(int *p)
{
if(temp==*p)
{
input(p);
}
return ;
}*/
void output(int a[][N])
{
int i,j,count=0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%-3d",a[i][j]);
count++;
if(count%9==0)
printf("\n");
}
}
int main()
{
int row,col,ary[N][N];
int p;
for(row=0;row<N;row++)
for(col=0;col<N;col++)
{
p=0;
ary[row][col]=input(p);
}
output(ary);
getch();
return ;
}
Dev-c++ 通过...
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int temp=0;
int count=0;
#define N 9
int input(int x) //输入函数
{
x=rand()%9+1;
return x;
}
/*int jiancha(int *p)
{
if(temp==*p)
{
input(p);
}
return ;
}*/
void output(int a[][N])
{
int i,j,count=0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%-3d",a[i][j]);
count++;
if(count%9==0)
printf("\n");
}
}
void main()
{
int row,col,ary[N][N];
int p;
srand((unsigned)time(NULL));//应该放在着。
for(row=0;row<N;row++)
for(col=0;col<N;col++)
{
ary[row][col]=input(p);
}
output(ary);
}
问题解决了,错在生成随即函数放错了地方。
----------------解决方案--------------------------------------------------------