问题描述
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");
}
}
推荐答案
#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;
}
#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");
}
}
专业程序员的编辑器具有此功能以及其他功能,例如括号匹配和语法高亮。
[]
[]
-----
学习分析问题,开始简单
此代码将打印第一列:
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方法是一个良好的开端。
[]
[]
[]
[]
[]
- 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编程模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!