for (int row = 0;row<3;row++){ for (int i = 0; i < 5 - row; i++) { if (i == row || i == (4 - row)) { System.out.print("v"); } else { System.out.print(" "); } } System.out.print("\n"); }
#include<iostream> using namespace std; void sout(int n) { //当前层数的输出长度 int len = 2*n - 1; //每行输出最后一个V的下标 int last = len - 1; //每行输出第一个v的下标 int begin = 0; for (int i = n; i > 0; --i) { for(int j = 0;j < len;j++) { if (j == last || j == begin){ cout << "v"; } else { cout << " "; } } --last; ++begin; cout<<endl; } } int main() { sout(3); return 0; }
public class Main{
public static void main(String[] args){
sout(3);
}
public static void sout(int n){ //n为层数 int len = 2*n - 1; //len为层的元素个数 int head = 0; //每层第一个元素的下标 int rear = len-1; //没层最后一个元素的下标 for(int i=n; i>0; i--){ //i为没层层数的下标 for(int j=0; j<len; j++){ //j为没层元素个数的下标 if(j==head || j==rear){ System.out.print("v"); }else{ System.out.print(" "); } } head++; rear--; System.out.println(); } }
}
//java public class Main{ public static void main(String[] args){ System.out.println("v v\n v v\n v"); } }
#python print("v v") print(" v v") print(" v") ''' print("v v\n v v\n v") '''
//C #include<stdio.h> int main() { printf("v v\n"); printf(" v v\n"); printf(" v"); } /* #include<stdio.h> int main() { printf("v v\n v v\n v"); } */
public class Main { public static void main(String[] args) { //输入层数,打印 Main.Solution(3); } static void Solution(int n) { //每一行v和空格的总数=最高层数+(最高层数-1) int count = n + (n - 1); int head = 1; int end = count; // 控制打印的层数 for (int i = n; i > 0; i--) { // 循环遍历输出当前层的个数和空格 for (int j = 1; j <= count; j++) { if (j == head || j == end) { System.out.print("v"); } else { System.out.print(" "); } } System.out.print("\n"); ++head; --end; } } }
int main(){ int loop = 0; scanf ("%d",&loop); for (int i = 0,j = loop*2; i <= j; i++,j--){ // 这里乘二是因为夹逼相当于将 loop / 2 // 所以想要输出正确行数的话就需要 *2 for (int k = 0; k <= j; k++){ if ( k == i && k == j){ // 当他俩相遇时,输出 V 并换行 printf("v\n"); } else if (k == i){ printf("v"); } else if (k == j){ printf("v\n"); } else { printf(" "); } } } return 0; }