如何使用C ++制作金字塔,使其看起来像我的示例?

时间:2019-05-21 21:29:03

标签: c++

我想用C ++创建这个金字塔,但是我有点卡住了。我可以得到一些帮助来完成我的示例吗?

6*****
*6****
**6***
***6**
****6*
*****6
int num1;
cout<<"please enter a size between 1-9: "<<flush;
cin>>num1;

for(int i = 0; i < num1; i++)
{
    cout <<num1;
    for(int j = 0; j <= i; j++)
    {
        cout<<"*";
    }

    cout << "\n";
}
6*
6**
6***
6****
6*****
6******

3 个答案:

答案 0 :(得分:1)

有两种方法可以解决此问题:

  • 使用2个单独的循环,一个循环显示数字前面的星星,另一个循环显示数字后面的星星,例如:
await ctx.send("What Is Your Name?")
    def check(m):
        return isinstance(m.channel, discord.abc.PrivateChannel) and m.author.id == ctx.author.id
    print("sends user message1")

    name = await bot.wait_for("message", check=check)
    await ctx.send("What Are You Looking For?, If You Wish To Leave This Blank, type None")
    pref = await bot.wait_for("message", check=check)
    if "none" in pref.content.lower():
        pref = "N/A"
    await ctx.send("What Are Your Hobbies?")
    hob = await bot.wait_for("message", check=check)
    await ctx.send("Tell Us A Bit About Yourself!(100 Character Max)")
    bio = await bot.wait_for("message", check=check)
    if len(bio.content) > 100:
        await ctx.send("Your BIO Is Too Long!, 100 Characters Max!, Redo This Command!")
        return

    # Age
    if eighteen in member.roles:
        age = "18"
    if nineteen in member.roles:
        age = "19"
    if twenty in member.roles:
        age = "20"
    if t1 in member.roles:
        age = "21"
    if t2 in member.roles:
        age = "22"
    if t3 in member.roles:
        age = "23"
    if t4 in member.roles:
        age = "24"
    if t5 in member.roles:
        age = "25"
    if t6 in member.roles:
        age = "26"
    if t7 in member.roles:
        age = "27"
    if t8 in member.roles:
        age = "28"
    if t9 in member.roles:
        age = "29"
    if thirty in member.roles:
        age = "30"
    if th1 in member.roles:
        await user.send("What Is Your Age?")
        age = await bot.wait_for("message", check=check)

    #Gender
    if female in member.roles:
        gender = "Female"
    if male in member.roles:
        gender = "Male"
    if tranf in member.roles:
        gender = "Trans-Female"

    if gen in member.roles:
        gender = "GenderFluid"
    if nonb in member.roles:
        gender = "Nonbinary"
    if tranm in member.roles:
        gender = "Trans-Male"
    #Dating Status
    if singl in member.roles:
        stat = "Single"
    if taken in member.roles:
        stat = "Taken"
    if notsea in member.roles:
        stat = "Not Searching"
    if compl in member.roles:
        stat = "It's Complicated"
    if poly in member.roles:
        stat = "Polyamorous"
    if searching in member.roles:
        stat = "searching"
    #sexuality
    if hetero in member.roles:
        orient = "Heterosexual"
    if homo in member.roles:
        orient = "Homosexual"
    if asexual in member.roles:
        orient = "Asexual"
    if demiromant in member.roles:
        orient = "DemiRomantic"
    if demisex in member.roles:
        orient = "Demisexual"
    if bi in member.roles:
        orient = "Bisexual"
    if curious in member.roles:
        orient = "Bi-Curious"
    if aro in member.roles:
        orient = "Aromantic"
    if preff in member.roles:
        orient = "Prefers Female/Feminine"
    if prefm in member.roles:
        orient = "Prefers Male/Masculine"
    if pan in member.roles:
        orient = "Pansexual"

    #Location
    if us in member.roles:
        loc = "United States"
    if latin in member.roles:
        loc = "Latin America"
    if asia in member.roles:
        loc = "Asia"
    if canada in member.roles:
        loc = "Canada"
    if europe in member.roles:
        loc = "Europe"
    if africa in member.roles:
        loc = "Africa"
    if middle in member.roles:
        loc = "Middle East"
    if oceania in member.roles:
        loc = "Oceania"
    #DM Status
    if closed in member.roles:
        dm = "DM's Closed"
    if open1 in member.roles:
        dm = "DM's Open"
    if ask in member.roles:
        dm = "Ask To DM"
    #F I N I S H E D
    data = {"name": name,
            "preference": pref,
            "hobbies": hob,
            "bio": bio,
            "Age": age,
            "status": stat,
            "orientation": orient,
            "location": loc,
            "DM Status": dm
           }
    dic = {f"{ctx.author.id}": data}

    path = os.path.join("/home/PRbot/DataBoi/", "profiles.json")
    with open(path, "w") as write:
        json.dump(dic, write)
        json.dump(data, write)
    await ctx.send("Your Profile Has Been Saved!")

Live Demo

  • 使用单个循环有条件地根据当前正在输出的位置来决定是输出星星还是数字:
Option Explicit
Sub Consolidate()

    Dim arrData As Variant
    Dim i As Long
    Dim Sales As New Scripting.Dictionary 'You will need the library Microsoft Scripting Runtime

    Application.ScreenUpdating = False 'speed up the code since excel won't show you what is happening

    'First of all, working on arrays always speeds up a lot the code because you are working on memory
    'instead of working with the sheets
    With ThisWorkbook.Sheets("YourSheet") 'change this
        i = .Cells(.Rows.Count, 1).End(xlUp).Row 'last row on column A
        arrData = .Range("A2", .Cells(i, 2)).Value 'here im assuming your row 1 has headers and we are storing the data into an array
    End With

    'Then we create a dictionary with the data
    For i = 1 To UBound(arrData) 'from row 2 to the last on Q1 (the highest)
        If Not Sales.Exists(arrData(i, 1)) Then
            Sales.Add arrData(i, 1), arrData(i, 2) 'We add the worker(Key) with his sales(Item)
        Else
            Sales(arrData(i, 1)) = Sales(arrData(i, 1)) + arrData(i, 2) 'if the worker already exists, sum his sales
        End If
    Next i

    'Now you have all the workers just once
    'If you want to delete column A and B and just leave the consolidate data:
    With ThisWorkbook.Sheets("YourSheet") 'change this
        i = .Cells(.Rows.Count, 1).End(xlUp).Row 'last row on column A
        .Range("A2:B" & i).ClearContents
        .Cells(2, 1).Resize(Sales.Count) = Application.Transpose(Sales.Keys) 'workers
        .Cells(2, 2).Resize(Sales.Count) = Application.Transpose(Sales.Items) 'Their sales
    End With

    Application.ScreenUpdating = True 'return excel to normal

End Sub

Live Demo

答案 1 :(得分:0)

您需要在数字之前和之后加上星号。因此,让我们扩大您的想法:

int num1;
cout<<"please enter a size between 1-9: "<<flush;
cin>>num1;

for(int i = 0; i < num1; i++)
{
    //Asterisks before the number
    for(int j = 0; j < i; j++)
    {
        cout<<"*";
    }

    cout <<num1;

    //Asterisks after the number
    for(int j = i; j < num1; j++)
    {
        cout<<"*";
    }

    cout << "\n";
}

您可以做一些改进:在函数中进行重构:

void writeRow(int length)
{
    for (int i = 0; i < length; i++)
        std::cout << "*";
}

// Some code...

for(int i = 0; i < num1; i++)
{
    //Asterisks before the number
    writeRow(i);

    cout <<num1;

    //Asterisks after the number
    writeRow(num1-i);

    cout << "\n";
}

答案 2 :(得分:0)

您可以使用std::string构造函数为每一行创建整个字符串,该构造函数需要一个计数和一个字符。

基本上这是模式:

  1. 打印0颗星,数字6,然后num-1颗星。
  2. 打印1星,数字6,然后num-2星。
  3. 打印2颗星,然后数字6,然后num-3颗星。

    等...

因此,模式是构建一个字符串,该字符串由之前的星星,数字6,之后的星星组成,对于每一行,您要在前面的星星增加,在之后的星星减少。

这是一个例子:

#include <string>
#include <iostream>

int main()
{
  int num1 = 6;
  int stars_before = 0;
  int stars_after = num1 - 1;
  for (int i = 0; i < num1; ++i)
     std::cout << std::string(stars_before++, '*') << '6' << std::string(stars_after--, '*') << "\n";
}

输出:

6*****
*6****
**6***
***6**
****6*
*****6