菜鸡成长之路之STL
作者:互联网
菜鸡成长之路之STL
A - 圆桌问题
圆桌上围坐着2n个人。其中n个人是好人,另外n个人是坏人。如果从第一个人开始数数,数到第m个人,则立即处死该人;然后从被处死的人之后开始数数,再将数到的第m个人处死……依此方法不断处死围坐在圆桌上的人。试问预先应如何安排这些好人与坏人的座位,能使得在处死n个人之后,圆桌上围坐的剩余的n个人全是好人。
Input
多组数据,每组数据输入:好人和坏人的人数n(<=32767)、步长m(<=32767);
Output
对于每一组数据,输出2n个大写字母,‘G’表示好人,‘B’表示坏人,50个字母为一行,不允许出现空白字符。相邻数据间留有一空行。
Sample Input
2 3
2 4
Sample Output
GBBG
BGGB
题意:约瑟夫环问题,可以用链表也可以利用vector模拟处死的过程,最终剩余在vector中的都是好人(此处是vector)
AC代码:
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
while(cin>>n>>m){
vector<int> tab;
for(int i=0;i<2*n;i++) //初始化所有人
tab.push_back(i);
int pf=0; //从1号开始数
for(int i=0;i<n;i++) //总共n个坏人
{
pf=(pf+m-1)%tab.size();
tab.erase(tab.begin()+pf);
}
int j=0;
for(int i=0;i<2*n;i++){
if(j<n&&i==tab[j])
{
j++;
cout<<'G';
}
else
cout<<'B';
if((i+1)%50==0) cout<<endl;
}
cout<<endl<<endl;
}
return 0;
}
B - Text Reverse
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Sample Input
3
olleh !dlrow
m’I morf .udh
I ekil .mca
Sample Output
hello world!
I’m from hdu.
I like acm.
Hint
Remember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.
题意:读入一行带空格的字符串,将每个单词逆序输出(单词之间用空格隔开),提示:用getchar()将输入第一个整数后的换行符吞掉。
本题每行输入都以回车结尾,将输入处理好可以使用gets()或是getline读入整行字符串,利用一个二维字符数组将单词逆序输出
AC代码:
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string>
#include<string.h>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 50005;
const double PI = acos(-1.0);
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
char c;
// cin>>n;
scanf("%d",&n);
getchar();
while(n--){
vector<char>str[1001];
int i=0;
while (1)
{
c=getchar();
if(c=='\n'||c==EOF)
break;
if(c!=' ')
str[i].push_back(c);
else
i++;
}
i++;
for(int j=0;j<i;j++)
{
for(int k=str[j].size()-1;k>=0;k--)
cout<<str[j][k];
//输出一个单词输出一个空格
if(j!=i-1)
cout<<' ';
}
cout<<endl;
}
return 0;
}
C - ACboy needs your help again!
ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can’t image how dark the room he was put into is, so poor
标签:const,STL,之路,cin,Sample,int,菜鸡,Input,include 来源: https://blog.csdn.net/newbie_dqt/article/details/113790900