要用到数组相关的知识
C语言上机作业
急急急
代码:
#include "stdio.h"
int fun(int c)
{
int step = 0;
int pf = 1;
int nstep = 0;
while(step < c)
{
printf("第%d项,数字是%d \n",step + 1,pf);
step++;
nstep++;
if(pf == nstep)
{
pf=pf+2;
nstep = 0;
}
}
return 0;
}
void main()
{
int xs = 0;
printf("请用户输入项数:\n");
scanf("%d",&xs);
fun(xs);
}
演示效果截图:
上面便于理解
下面是根据你题面来做的
代码:
#include "stdio.h"
int fun(int c)
{
int step = 0;
int pf = 1;
int nstep = 0;
while(step < c)
{
printf("%d ",pf);
step++;
nstep++;
if(pf == nstep)
{
pf=pf+2;
nstep = 0;
}
}
return 0;
}
void main()
{
int xs = 0;
printf("请用户输入项数:\n");
scanf("%d",&xs);
fun(xs);
}
演示效果截图:
如果当前回答解决了你的问题,还请帮忙点一个采纳。
你的采纳使我们的回答的动力,麻烦了。
代码如下:如有帮助,请采纳一下,谢谢。
#include <stdio.h>
void main()
{
int i,n=40,sum = 0;
for (i = 1;;i++)
{
sum += (2*i -1);
if (sum >= n)
{
printf("%d\n",2*i-1);
break;
}
}
}