【问题标题】:How to design looping number sequence(increment)? [closed]如何设计循环数列(增量)? [关闭]
【发布时间】:2013-10-09 22:58:21
【问题描述】:

我想编写一个程序来打印从头到尾指定负增量的数字序列。

输入格式:

我放一行由三个整数组成:开始、负增量和结束序列。 (例如:扫描 9 -2 3

输出格式:

输出从开始到结束的数字序列,每行打印。以换行符结束输出。(它将是: 9 7 5 3

解决办法如下:

 #include<stdio.h>
int main(void)
{
  int start,step,end,i;
  scanf("%d %d %d",&start,&step,&end);
  for(i=start;i>=end;i+=step)
    printf("%d ",i)
  printf("\n");
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。问题要求至少表明尝试解决发布的问题。请发布您的尝试并说明您遇到的问题
  • 你不是说应该是9 7 5 3吗?

标签: c loops increment


【解决方案1】:
#include<stdio.h>
int main(void)
{
  int start,step,end,i;
  scanf("%d %d %d",&start,&step,&end);
  for(i=start;i>=end;i+=step)
    printf("%d ",i)
  printf("\n");
}

【讨论】:

    【解决方案2】:
    int start,end,inc;
    scanf("%d %d %d",&start,&inc,&end);
    
    for(;start>=end;start-=inc)
         printf("%d ",start);
    

    【讨论】:

    • 这不是问题的答案,虽然 OP 标记为 c,但它是 c++
    • 抱歉,现在是 C :D @Pankrates
    猜你喜欢
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多