仅使用一个变量创建此序列
Create This Sequence Using Only One Variable
如何用 C 编写代码来输出这个序列?
10, 1, 9, 2, 8, 3, 7, 4, 6, 5
...简单!
#include <stdio.h>
int main(){
int f = 10, s = 1;
while (s <= 5)
printf("%d %d ",f--,s++);
getch();
}
有很多方法可以做到这一点,但您可能已经注意到,它使用了两个变量。
那么如何仅使用一个变量重新创建它?
这是您使用单个变量的代码:
int main()
{
int s = 1;
while (s <= 5)
{
printf("%d %d ", (11-s), s);
s++;
}
printf("\n");
}
const int MAX = 10;
for(int i=0; i<MAX/2; i++)
{
printf("%d, %d, ", MAX-i, i+1);
}
如何用 C 编写代码来输出这个序列?
10, 1, 9, 2, 8, 3, 7, 4, 6, 5
...简单!
#include <stdio.h>
int main(){
int f = 10, s = 1;
while (s <= 5)
printf("%d %d ",f--,s++);
getch();
}
有很多方法可以做到这一点,但您可能已经注意到,它使用了两个变量。
那么如何仅使用一个变量重新创建它?
这是您使用单个变量的代码:
int main()
{
int s = 1;
while (s <= 5)
{
printf("%d %d ", (11-s), s);
s++;
}
printf("\n");
}
const int MAX = 10;
for(int i=0; i<MAX/2; i++)
{
printf("%d, %d, ", MAX-i, i+1);
}