C 如何迭代序列:0、1、2、3、4、4、5、6、7、7、8、7、8、8、9…;不使用递归?
我想知道如何用C语言生成这个数字序列C 如何迭代序列:0、1、2、3、4、4、5、6、7、7、8、7、8、8、9…;不使用递归?,c,sequence,series,C,Sequence,Series,我想知道如何用C语言生成这个数字序列 0, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 8, 9 … 序列是通过形成一个三角形的数字生成的,如下所示: 0 1 2 3 4 5 6 7 8 9 ... 序列中的下两个数字如下所示: 下一个号码就在下面 “下一个到下一个”位于右侧的一个位置 如何遍历这个数字三角形,得到C中的序列 意思是 0替换为1和2 1替换为3和4 将2替换为4和5 0 |\ 1 2 |\|\ 3 4 5 |\|\|\ 6 7 8 9 Series -&g
0, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 8, 9 …
序列是通过形成一个三角形的数字生成的,如下所示:
0
1 2
3 4 5
6 7 8 9 ...
序列中的下两个数字如下所示:
0
|\
1 2
|\|\
3 4 5
|\|\|\
6 7 8 9
Series -> 0, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 7, 8, 8, 9 ........
意思是
I.求解0
0 leads to 1 and 2
0 -> 1 - 2
0 1 2
II。解决1和2的问题
1 leads to 3 and 4
1 -> 3 - 4
0 1 2 3 4
2 leads to 4 and 5
2 -> 4 - 5
0 1 2 3 4 4 5
III.解决3,4,4,5问题
3 leads to 6 and 7
3 -> 6 - 7
0 1 2 3 4 4 5 6 7
4 leads to 7 and 8
4 -> 7 - 8
0 1 2 3 4 4 5 6 7 7 8
4 leads to 7 and 8
4 -> 7 - 8
0 1 2 3 4 4 5 6 7 7 8 7 8
5 leads to 8 and 9
5 -> 8 - 9
0 1 2 3 4 4 5 6 7 7 8 7 8 8 9
我为没有正确解释而道歉。希望这次我能解释清楚。我想(根据描述)你的顺序应该是正确的
0
1 2
3 4 4 5
6 7 7 8 8 9
10 11 11 12 12 13 13 14
等等
您可以使用如下代码使用它:
int nextRowStart = 0;
int nextRowSize = 1;
for (int curr = 0; /*put some ending condition here*/; curr++)
{
yield(curr)
if (curr != nextRowStart - 1 && curr != nextRowStart)
yield(curr);
if (curr == nextRowStart)
{
nextRowStart += nextRowSize;
nextRowSize++;
}
}
void yield(int x)
{
printf("%d ", x);
}
对于更改后的问题,可以递归地执行新的问题 这是C#中的解决方案:
IEnumerable生成(int级)
{
如果(级别==0)
{
收益率为0;
屈服断裂;
}
int diff=电平;
foreach(生成中的int n(级别-1))
{
收益率n+diff;
收益率n+diff+1;
}
}
var结果=可枚举。范围(0,maxLevel)。选择多个(生成);
要把它翻译成中文需要一些时间
C解决方案:
void Generate(int level, int* resultsize, int** result)
{
if (level == 0)
{
*result = (int*)malloc(sizeof(int));
(*result)[0] = 0;
*resultsize = 1;
}
else
{
int recResultSize;
int* recResult;
Generate(level - 1, &recResultSize, &recResult);
*resultsize = recResultSize * 2;
*result = (int*)malloc(*resultsize * sizeof(int));
for (int i = 0; i < recResultSize; i++)
{
(*result)[2*i] = recResult[i] + level;
(*result)[2*i + 1] = recResult[i] + level + 1;
}
free(recResult);
}
}
void生成(int-level,int*resultsize,int**result)
{
如果(级别==0)
{
*结果=(int*)malloc(sizeof(int));
(*结果)[0]=0;
*结果大小=1;
}
其他的
{
int-resultsize;
int*result;
生成(级别-1,&recResultSize,&recResult);
*resultsize=resultsize*2;
*结果=(int*)malloc(*resultsize*sizeof(int));
对于(int i=0;i
这是给出准确结果并解决问题的代码。当@Lundin要求我发布代码时,我得到了这个结果,我再次尝试,我成功了。谢谢各位
#include<stdio.h>
int in;
int main(){
int ik, it, icount = 0, ih, temp, ig = 1;
int aisum[100];
aisum[0] = 0;
scanf("%d",&in);
printf("0\n");
it = 1;ih = 0;temp = 2;
for(icount = 0,ig = 1; icount <= in; icount++){
for(ik = 0; ik<2; ik++){
aisum[ig] = aisum[icount] + it + ik ;
printf("%d ",aisum[ig]);
ig++;
}
if(aisum[icount] == ih){
printf("\n");
it++;
ih += temp;
temp++;
}
}
return 0;
}
/*Input the number of elements to be processed*/
/*icount will account for new elements to be formed like if we look
at pascal triangle
0 will form 1 and 2
1 will form 3 and 4
*/
/*ig will account for array indices*/
/*it will account for the periodic addition */
/*ih checks for the codnition for it to increement
0
1 2
3 4 5
it will be increemented at 0, 2, 5 ...
*/
#包括
int-in;
int main(){
int-ik,it,icount=0,ih,temp,ig=1;
国际货币基金组织[100];
aisum[0]=0;
扫描频率(“%d”和“&in”);
printf(“0\n”);
它=1;ih=0;温度=2;
对于(icount=0,ig=1;icount我能给出的从三角形下方到所需顺序的最简单解是
0
1 2
3 4 5
6789
打印每行的开始和结束节点和打印中心元素各2次
对于每一行,起点将等于前一个终点+1
结束将等于结束+1+计数
每行的计数将增加1
CPP计划:
#包括
使用名称空间std;
int main()
{
int开始=1,结束=2,计数=1;
根据你的解释,标题中的系列不是太多了吗?为什么6,7,7,8,9
,而不是6,7,7,8,8,9
?下一行是什么?10,11,11,12,11,12,12,12,12,12,12,12,13,13,14或10,11,12,12,12,12,13,13,13,14,(粗体的区别)?我的答案将生成第一个,但你的描述也符合第二个。请再次调查我编辑过的问题。希望这次可以理解…你到目前为止尝试了什么?请发布你的代码。请再次调查我编辑过的问题。希望这次可以理解…@结论:刚刚为你的案例添加了C#解决方案,我将尝试将其映射到C代码。我已经通过递归对其进行了评估,如果可以使用循环以非递归方式完成,这将非常有用。Thanks@Lusion:嗯,非递归解决方案需要研究序列的属性,而递归解决方案直接对应于您的描述——这就是为什么它更简单:)查看我的非递归解决方案。这应该很好,我看到的唯一问题是aisum
数组的静态分配。您实际上可以预计算其所需大小(考虑到每行大小是以前大小的两倍,它是2^maxlevel
)并使用malloc
进行分配。
void Generate(int level, int* resultsize, int** result)
{
if (level == 0)
{
*result = (int*)malloc(sizeof(int));
(*result)[0] = 0;
*resultsize = 1;
}
else
{
int recResultSize;
int* recResult;
Generate(level - 1, &recResultSize, &recResult);
*resultsize = recResultSize * 2;
*result = (int*)malloc(*resultsize * sizeof(int));
for (int i = 0; i < recResultSize; i++)
{
(*result)[2*i] = recResult[i] + level;
(*result)[2*i + 1] = recResult[i] + level + 1;
}
free(recResult);
}
}
#include<stdio.h>
int in;
int main(){
int ik, it, icount = 0, ih, temp, ig = 1;
int aisum[100];
aisum[0] = 0;
scanf("%d",&in);
printf("0\n");
it = 1;ih = 0;temp = 2;
for(icount = 0,ig = 1; icount <= in; icount++){
for(ik = 0; ik<2; ik++){
aisum[ig] = aisum[icount] + it + ik ;
printf("%d ",aisum[ig]);
ig++;
}
if(aisum[icount] == ih){
printf("\n");
it++;
ih += temp;
temp++;
}
}
return 0;
}
/*Input the number of elements to be processed*/
/*icount will account for new elements to be formed like if we look
at pascal triangle
0 will form 1 and 2
1 will form 3 and 4
*/
/*ig will account for array indices*/
/*it will account for the periodic addition */
/*ih checks for the codnition for it to increement
0
1 2
3 4 5
it will be increemented at 0, 2, 5 ...
*/
#include<iostream>
using namespace std;
int main()
{
int start=1,end=2,count=1;
cout<<0<<" ";
while(count<5)
{
cout<<start<<" ";
for(int i=start+1;i<end;i++)
{
cout<<i<<" "<<i<<" ";
}
cout<<end<<" ";
count++;
start=end+1;
end=start+count;
}
return 0;
}