简体   繁体   English

如何使用for循环在c ++中制作单词三角形

[英]How to make word triangle in c++ with for loop

how can I make this type of triangle: where the size of triangle n = 5;我怎样才能制作这种类型的三角形:三角形的大小 n = 5;

Five Four Three Two One
Five Four Three Two
Five Four Three
Five Four
Five

Here's what I've tried:这是我尝试过的:

int n;
cout<<"Enter Size of triangle: ";
cin>>n;

for(int i = n; i >= 1; i--){
    for(int j = n; j >= i; j--){
        cout<<j<<" ";
    }
    cout<<endl;
}
for(int i = n; i >= 1; i--){
    for(int j = n; j >= i; j--){
        switch(j) {
            case 0: cout <<"zero"<<; break;
            case 1: cout <<"one"<<; break;
            case 2: cout <<"two"<<; break;
            case 3: cout <<"three"<<; break;
        }
    }
    cout<<endl;
}

I suppose you can get the loop right yourself, so what is left is map numbers to strings.我想你可以自己得到循环,所以剩下的是将数字映射到字符串。 There is no automatic way to do that, but you have to define this mapping yourself.没有自动方法可以做到这一点,但您必须自己定义此映射。 Perhaps the easiest is to use a也许最简单的方法是使用

std::vector<std::string> numbers{ "One", "Two", "Three", "Four", "put" ,"more", "numbers", "here" };

And then instead of printing the number j you print numbers[j] .然后不是打印数字j而是打印numbers[j] Just take care of indexing: Vector uses zero-based indexing, but the first element is "One" .只需注意索引:Vector 使用从零开始的索引,但第一个元素是"One"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM