C编程模式 [英] C programming pattern

查看:93
本文介绍了C编程模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I needed code for this pattern.

1 2
2 3
3 4
4 5

I tried it , but was not able to make it.





我的尝试:





What I have tried:

#include<stdio.h>

void main() {
    int i,j;
 
    for(i=1; i<=5; i++){
        for(int j=i;j<=5;j++)
        {
     printf("%d ",j);

        
}
printf("\n");
    }
    

}

推荐答案

你应该检查你的循环。尝试

You should review your loops. Try
#include <stdio.h>

int main()
{
  int i;

  for (i=1; i<=4; i++)
  {
    int j;
    for (j=0;j<=1;j++)
    {
      printf("%d ",(i+j));
    }
    printf("\n");
  }

  return 0;
}


学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include<stdio.h>

void main() {
  int i,j;
  for(i=1; i<=5; i++){
    for(int j=i;j<=5;j++)
    {
      printf("%d ",j);
    }
    printf("\n");
  }
}



专业程序员的编辑器具有此功能以及其他功能,例如括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]

-----

学习分析问题,开始简单

此代码将打印第一列:


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
Learn to analyze problems, start simple
This code will print the first column:

#include<stdio.h>

void main() {
  int i;
  for(i=1; i<=4; i++){
    printf("%d ",i);
    printf("\n");
  }
}



然后添加第二列


then add the second column

#include<stdio.h>

void main() {
  int i;
  for(i=1; i<=4; i++){
    printf("%d ",i); // first column
    printf("%d",i+1);// second column
    printf("\n");
  }
}



- 学习一种或多种分析方法,E.W。Djikstra / N. Wirth Stepwize Refinment / top-Down方法是一个良好的开端。

Structured Programming.pdf [ ^ ]

https://en.wikipedia。 org / wiki / Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]

https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF [ ^ ]


- Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinment/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]


for (int i = 1; i < 5; ++i)
    cout << i << " " << i + 1 << endl;



或者你坚持要有两个循环:


or if you insist on having two loops:

for (int i = 1; i < 5; ++i)
{
    for (int j = i; j < i + 2; ++j)
    {
         cout << j;
         cout << " ";
    }
    cout << endl;
}


这篇关于C编程模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆