本文概述
- C ++
- C
- Java
- Python3
- C#
- 的PHP
1
23
456
78910
1112131415
推荐:请在"实践首先, 在继续解决方案之前。以下程序将打印出n条线的弗洛伊德三角形。
C ++
#include <
bits/stdc++.h>
using namespace std;
void printFloydTriangle( int n)
{
int i, j, val = 1;
for (i = 1;
i <
= n;
i++)
{
for (j = 1;
j <
= i;
j++)
cout <
<
val++ <
<
" " ;
cout <
<
endl;
}
} // Driver Code
int main()
{
printFloydTriangle(6);
return 0;
} // This is code is contributed
// by rathbhupendra
C
// Without using a temporary variable and with only one loop
#include<
stdio.h>
void floyd(n){
int i, j=1;
for (i=1;
i<
=(n*(n+1))/2;
i++){
printf ( "%d " , i);
if (i==(j*(j+1))/2){
printf ( "\n" );
j++;
}
}
}int main(){
floyd(6);
}//This code is contributed by Vishal B
Java
// Java program to print
// Floyd's triangle
class GFG
{
static void printFloydTriangle( int n)
{
int i, j, val = 1 ;
for (i = 1 ;
i <
= n;
i++)
{
for (j = 1 ;
j <
= i;
j++)
{
System.out.print(val + " " );
val++;
}
System.out.println();
}
}// Driver Code
public static void main(String[] args)
{
printFloydTriangle( 6 );
}
}
Python3
# Python3 program to print
# Floyd's triangle
def loydTriangle(n):val = 1
for i in range ( 1 , n + 1 ):for j in range ( 1 , i + 1 ):
print (val, end = " " )
val + = 1print ("")loydTriangle( 6 )# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to print
// Floyd's triangle
using System;
class GFG
{
static void printFloydTriangle( int n)
{
int i, j, val = 1;
for (i = 1;
i <
= n;
i++)
{
for (j = 1;
j <
= i;
j++)
{
Console.Write(val + " " );
val++;
}
Console.WriteLine();
}
}// Driver Code
public static void Main()
{
printFloydTriangle(6);
}
}
的PHP
<
?php
// PHP code to print Floyd's Triangle// Function to display Floyd's Triangle
function FloydsTriangle( $n )
{
$val = 1;
// loop for number of lines
for ( $i = 1;
$i <
= $n ;
$i ++)
{
// loop for number of elements
// in each line
for ( $j = 1;
$j <
= $i ;
$j ++)
{
print ( $val . " " );
$val ++;
}
print ( "\n" );
}
}// Driver's Code
$n = 6;
FloydsTriangle( $n );
// This code is contributed by akash7981
?>
输出如下:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
推荐阅读
- 德勤面试经历分享|S3(校园内数据科学资料)
- 多处理和多线程之间有什么区别()
- jQuery如何使用each()方法处理DOM元素()
- PHP如何使用opendir()函数打开目录()
- 系统之家wi8最新系统推荐
- 系统之家台式机win7系统旗舰版最新系统推荐
- 深度技术win732位最新系统推荐
- 深度技术win7纯净版32位gho最新系统推荐
- 系统之家win8.1纯净版64位镜像最新系统推荐