在C ++中用新行分隔打印6,5,4,3,2个字符?

时间:2015-11-06 11:18:15

标签: c++ algorithm sorting for-loop

使用{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Example Join", "Parameters": { "Delimiter": { "Type": "String" } }, "Resources": { "LambdaExecutionRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": ["lambda.amazonaws.com"] }, "Action": ["sts:AssumeRole"] } ] }, "Path": "/", "Policies": [ { "PolicyName": "root", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "cloudformation:DescribeStacks" ], "Resource": "*" } ] } } ] } }, "LambdaJoin": { "Type": "AWS::Lambda::Function", "Properties": { "Code": { "ZipFile": { "Fn::Join": ["\n", [ "var response = require('cfn-response');\n", "exports.handler = function (event, context) {\n", "if (event.RequestType === 'Delete') {\n", "return response.send(event, context, response.SUCCESS, {}, event.PhysicalResourceId);\n", "}\n", "var delimiter = event.ResourceProperties.delimiter || '';\n", "var strings = event.ResourceProperties.strings || [];\n", "return response.send(event, context, response.SUCCESS, { string: strings.join(delimiter) }, event.PhysicalResourceId);\n", "};\n" ]]} }, "Handler": "index.handler", "Runtime": "nodejs", "Timeout": "10", "Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] } } }, "CustomJoin": { "Type": "Custom::Join", "Version": "1.0", "Properties": { "ServiceToken": { "Fn::GetAtt": ["LambdaJoin", "Arn"] }, "delimiter": { "Ref": "Delimiter" }, "strings": ["first", "second", "third"] }, "DependsOn": ["LambdaJoin"] } }, "Outputs": { "JoinedString": { "Value": { "Fn::GetAtt": ["CustomJoin", "string"] } } } } 循环,如何将以下输出打印到控制台:

for

侧点:没有兴趣,是否可以在没有****** ***** **** *** ** 循环的情况下执行上述操作?

4 个答案:

答案 0 :(得分:1)

你可以试试这个:

for (int i=6; i>1; i--)
    {
        for (int j=0; j<i; j++)
            cout<<"*";

        cout<<endl;
    }

输出:

******
*****
****
***
**

答案 1 :(得分:1)

while循环怎么样:http://ideone.com/ezk6Ax

string s(6, '*');
while (s.size() > 1) {
    cout << s << endl;
    s.pop_back();
}

答案 2 :(得分:1)

#include <iostream>
#include <string>

int main()
{
  const std::string s("*******");
  for (int i = s.size() - 1; i > 1; i--) {
    std::cout << s.substr(0, i) << std::endl;
  }
  return 0;
}

http://coliru.stacked-crooked.com/a/10924920bc66f91d

答案 3 :(得分:1)

我的变体:

#include <algorithm>
#include <iostream>
#include <iterator>

int main()
{
    for (size_t i = 6; i >= 2; --i) {
        std::generate_n(
            std::ostream_iterator<char>(std::cout, ""), i,
            []() { return '*'; });
        std::cout << '\n';
    }
}