hdu4841圆桌问题

解析:约瑟夫环问题,通过动态数组Vector解决.

#include<iostream>
#include<vector>
using namespace std;
vector<int> table;
int n,m;
int main()
{
while(cin>>n>>m){
//1.每次使用要先初清空
table.clear();
// 2.对table进行标号从0--2*n
for(int i = 0;i < 2*n;i++){
//table[i] = i;
table.push_back(i);//这是动态数组!
}
//3.进行模拟,删除n个坏人
int pos = 0;//删除位置 下次删除的位置:(pos+m-1)% table.size()
for(int i = 0;i<n;i++){
pos=(pos+m-1)%table.size();
table.erase(table.begin()+pos); //进行删除

}
//4.留在table中的都是好人,依次遍历所有的编号,判断进行输出
int j = 0;//标记遍历的位置对应table中的位置
for(int i = 0;i < 2*n;i++){
if(i%50==0&&i) {//每五十个字符一行. 排除i=0情况
cout<<endl;
}
if(i==table[j]&&j<table.size()){//j不能越界.
j++;
cout<<"G";
}else{
cout<<"B";
}
}
cout<<endl<<endl;//空行.
}
return 0;
}

hdu1062Text Reverse

解析:入门题,考察字符串使用的几个函数.
方法一:

#include<iostream>
#include<cstring>
using namespace std;
int n,start;
char ch[1000];
int main() {
//思路分析:
while(cin>>n) {
//1. 读取字符串的个数
//2.getchar()获取末尾 换行符
getchar();
while(n--) {
//3.每次读取一行
gets(ch);//无需初始化字符串.
//4.设置两个变量记录小串的起始,结束位置.并倒序输出 /使用栈来解决.
start = 0;
int i;
for(i = 0; i <strlen(ch); i++) {
if(ch[i]==' ') { //遇到空格,将上一个字符串进行反转
for(int j = i-1; j>=start; j--) {
cout<<ch[j];
}
cout<<" ";
start = i+1;//start进行更新
}
}
for(int j = i-1; j>=start; j--) {
cout<<ch[j];
}
cout<<endl;
}
}


return 0;
}

方法二:由于字符串反转,我们可以通过栈来解决.

#include<iostream>
#include<stack>
using namespace std;
int n;
stack<char> stack1;
int main()
{
while(~scanf("%d",&n)){
getchar();//拿走结尾的回车
while(n--){
while(1){
char ch = getchar();
if(ch==' '||ch=='\n'){
while(!stack1.empty()){
cout<<stack1.top();
stack1.pop();
}
if(ch=='\n'){//进行下一行.
break;
}
cout<<" ";//当是 ' '时,输出空格.
}else{
stack1.push(ch);
}
}
cout<<endl;
}
}
return 0;
}

C/C++字符串常见函数

getchar():读取一个字符.

gets():功读入一个完整的行,一直读到遇到换行符,并用空字符’\0’取代行尾的换行符’\n’。读入时不需要考虑换行符。 参数是一个字符数组.

getline(cin,str):读入一个字符串(string类型),可以接收空格,遇到回车结束.