编写一函数,输出整数m的全部素数因子 ;例如m=120的因子为 2,2,2,3,5
不懂这道题用函数怎么写啊~
谢谢大家帮个忙 麻烦了啊
[此贴子已经被作者于2007-5-7 16:21:36编辑过]
#include <stdio.h>
#include <conio.h>
const int N=6;
const int M=8;
void main()
{ int a[N][M];
int k=0, m=0, v=1;
int direction = 0; //0:right; 1:down; 2:left; 3:up
while(v<=N*M)
{ a[k][m] = v;
v++;
switch(direction)
{
case 0: m++;
if(m==M-1-k)
direction = 1;
break;
case 1: k++;
if(k==N-1-(M-1-m))
direction = 2;
break;
case 2: m--;
if(m==N-1-k)
direction = 3;
break;
case 3: k--;
if(k==1+m)
direction = 0;
break;
}
}
for(k=0; k<N; k++)
{
for(m=0; m<M; m++)
printf("%3d", a[k][m]);
printf("\n");
}
}
我给出一个比较差的算法你看看,我也初学者,暂时还没想到好的算法呢!
#include <stdio.h>
#include <conio.h>
const int N=6;
const int M=8;
void main()
{ int a[N][M];
int k=0, m=0, v=1;
int direction = 0; //0:right; 1:down; 2:left; 3:up
while(v<=N*M)
{ a[k][m] = v;
v++;
switch(direction)
{
case 0: m++;
if(m==M-1-k)
direction = 1;
break;
case 1: k++;
if(k==N-1-(M-1-m))
direction = 2;
break;
case 2: m--;
if(m==N-1-k)
direction = 3;
break;
case 3: k--;
if(k==1+m)
direction = 0;
break;
}
}
for(k=0; k<N; k++)
{
for(m=0; m<M; m++)
printf("%3d", a[k][m]);
printf("\n");
}
}
不好意思,发错代码了,这个才是的!