【问题描述】编写函数,重复打印给定字符n次,n由输入数字确定。
【输入形式】输入一个字符,以及字符需要重复打印的次数
【输出形式】提示信息:please input a char and the times:
【样例输入】a 5
【样例输出】please input a char and the times:
aaaaa
//编写函数,重复打印给定字符n次,n由输入数字确定
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char a;//单个字符
int time;//次数
cout<<"please input a char and the times:"<<endl;
cin>>a>>time;
vector<char>v;
for(;time>0;--time)
{
v.push_back(a);//压入time个字符
}
for(vector<char>::size_type t= 0;t<v.size();++t)
{
cout<<v[t];//输出time个字符
}
return 0;
}
本文介绍了一个简单的C++程序,该程序定义了一个函数,用于接收一个字符及一个整数作为参数,并将该字符重复打印指定次数。文章通过示例展示了如何使用此函数。
926

被折叠的 条评论
为什么被折叠?



