猜字母
把abcd...s共19个字母组成的序列重复拼接106次,得到长度为2014的串。
接下来删除第1个字母(即开头的字母a),以及第3个,第5个等所有奇数位置的字母。
得到的新串再进行删除奇数位置字母的动作。如此下去,最后只剩下一个字母,请写出该字母。
以下程序哪项运行后不能得到正确答案?
#include<vector>
//定义
vector<int> a,b;
vector<int,2> a;//赋初值2
vector<int> b(a);//把a的值赋给b;
//常见操作
a.assign(b.begin(),b.begin()+3);//b为向量,将b的0-2个元素赋值给向量a
a.assign(4,2);//a含有4个值为2的元素
a.back();//返回a的最后一个元素
a.front();//返回a的第一个元素
a[i];//返回a的第i元素,当且仅当a存在
a.clear();//清空a中的元素
a.empty();//判断a是否为空,空则返回true,非空则返回false
a.pop_back();//删除a向量的最后一个元素
a.begin()+3 ;
a.erase(a.begin()+1,a.begin()+3);//删除a中第一个(从第0个算起)到第二个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到(不包括它)结束
a.push_back(5);//在a的最后一个向量后插入一个元素,其值为5
a.insert(a.begin()+1,5);//在a的第一个元素(从第0个算起)位置插入数值5,
a.insert(a.begin()+1,3,5);//在a的第一个元素(从第0个算起)位置插入3个数,其值都为5
a.insert(a.begin()+1,b+3,b+6);//b为数组,在a的第一个元素(从第0个元素算起)的位置插入b的第三个元素到第5个元素(不包括b+6)
a.size();//返回a中元素的个数
a.capacity();//返回a在内存中总共可以容纳的元素个数
a.resize(10);//将a的现有元素个数调整至10个,多则删,少则补,其值随机
a.resize(10,2);//将a的现有元素个数调整至10个,多则删,少则补,其值为2
a.reserve(100);//将a的容量扩充至100,
a.swap(b);//b为向量,将a中的元素和b中的元素整体交换
a==b;//b为向量,向量的比较操作还有 != >= > <= <
#include<iostream>
#include<queue>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
int main()
{
vector<char> vc1;//动态数组
int i;
for (i = 0; i < 2014; i++)
vc1.push_back('a' + (i % 19));//初始化;
while (vc1.size() != 1)
{
vector<char> vc2;
for (i = 1; i < vc1.size(); i += 2)
vc2.push_back(vc1[i]);
vc1.assign(vc2.begin(), vc2.end());// 将vc2的元素赋值给向量vc1
}
cout << vc1[0] << endl;
return 0;
}
//我写的,用数字直接表示字母
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<cstring>
using namespace std;
int main()
{
vector <int> a ;
vector<int> b ;
for(int i=0;i<2014;i++)
a.push_back(i);
int k=2014;
while(k!=1){
int cnt=0;
b.clear();
for(int i=0;2*i+1<k;i++)
{
b.push_back(a[2*i+1]);
cnt++;
}
a.assign(b.begin(),b.end());
k=cnt;
cout<<k<<" ";
}
char c='a'+(b[0]%19) ;
cout<< c;
return 0;
}
#include<iostream>
#include<queue>
#include<vector>
#include<set>
using namespace std;
int main()
{
string s = "abcdefghijklmnopqrs";
string str;
for (int i = 0; i < 106; i++)
{
str += s;
}
while (str.length() > 1)
{
for (int i = 0; i < str.length(); i++)
{
str.erase(i, 1);//从0开始算,删掉了1个元素;并且自动填补空位;string
}
}
cout << str;
return 0;
}
把int型转化成string
#include <string>
#include <sstream>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
stringstream sstream;
string strResult;
int nValue = 1000;
// 将int类型的值放入输入流中
sstream << nValue;
// 从sstream中抽取前面插入的int类型的值,赋给string类型
sstream >> strResult;
cout << "[cout]strResult is: " << strResult << endl;
printf("[printf]strResult is: %s\n", strResult.c_str());
return 0;
}
//用stringstream来分割指定的字符字符串
#include <iostream>
#include <sstream>
#include <vector>
#include<queue>
#include<string>
using namespace std;
int main() {
string str = "1,2,3,4,5";
stringstream ss(str);
string item;
queue<string> q;
cout << str << endl;
while(getline(ss, item, ','))
cout << item << ' ';
return 0;
}
// input: 1,2,3,4,5
//output: 1 2 3 4 5