准备知识
以下是string.h库中的常用函数
求字符串长度: int len = strlen(str);
字符串复制: strcpy();
字符串比较: strcmp();
字符串拼接: strcat();
查询字串: strchr();
查询子串: strstr();
查找所有子串在母串中出现的位置: find()
复制子字符串,要求从指定位置开始,并具有指定的长度: substr()
以下是stdlib.h库中的常用函数
将数字字符串(如”136”)转换为int型:int i = atoi(str);
将数字字符串(如”136”)转换为float型:int f = atof(str);
将数字字符串(如”136”)转换为long型:int l = atol(str);
将int型多位数字转换为字符串型: itoa(str, i, 10);(第三个参数表示进制)
字符操作。有很多机试题目需要对字符(不是字符串)进行判断和操作,这里列举一些常用的操作和编程技巧。
判断一个字符是数字: if(c>=’0’ && c<=’9’);
判断一个字符是大写字母: f(c>=’A’ && c<=’Z’);
将char型数字转换为int型的一位数: int b = c - ‘0’;
将int型数字转换为char型数字: char c = char(b + ‘0’);
将大写字母转换为小写字母: char xiaoxie = daxie + (‘a’ - ‘A’);
https://blog.csdn.net/wgx571859177/article/details/79686585
/*
一共三道题,150分钟,第一二题200分,第三题400分,听说150分算过。
主要是只告诉你通过率,没告诉你正确答案和你的错误答案做对比,不知道自己哪里漏了。
*/
/*
第一题1-100围城一个圈,重1开始数,如果等于给定的数就删掉,继续数, 直到剩余的数量小于给定的数。此题100%通过
输入
3
输出
58,91
输入
4
输出
34,45,97
*/
#include <iostream>
#include <string.h>
#include <map>
using namespace std;
struct tarNumber
{
tarNumber()
{
memset(this,0,sizeof(*this));
}
int nNumber;
int nNewNB;
};
int main()
{
int a;
cin >> a ;
if(a<=1 || 100 <=a)
{
cout<<"ERROR!"<<endl;
return 0;
}
map<int,tarNumber> mapNumber;
for (int i=1;i<=100;i++)
{
tarNumber Number;
Number.nNumber = i;
Number.nNewNB = i;
mapNumber[i] = Number;
}
while( a <= mapNumber.size())
{
map<int,tarNumber>::iterator It = mapNumber.begin();
static int nCout = 0;
for (It;It != mapNumber.end();)
{
nCout++;
if(nCout == a)
{
mapNumber.erase(It++);
nCout = 0;
}
else
{
It++;
}
}
}
map<int,tarNumber>::iterator It = mapNumber.begin();
int nIndex = 0;
for (It;It != mapNumber.end();It++)
{
nIndex++;
cout<<It->second.nNumber;
if(nIndex < a-1)
cout<<",";
}
cout<<endl;
system("pause");
}
/*
第2题是一个求相邻的平均数的值小于给定值的区间,比较简单,但是我的通过率只有36%左右,不知道哪里有问题,时间有限直接提交了。现在回想起来,原题好像是说向下取整,我居然用了浮点数,画蛇添足。。。
输入
2
0 0 100 2 2 100 99 0 2
输出
0-1 3-4 7-8
解释:输入二行,第一行是一个值,第二行是一组数字,输出的是下标的区间。0-1表示[0,0],3-4表示[2,2]
*/
#include <string.h>
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
inline void SplitString2Int( const string& src, string delimit, vector<int>& vecRet)
{
string null_subst = "0";
if( src.empty() || src == "" || delimit.empty() || delimit == "" )
return;
int deli_len = delimit.size();
int nIndex = 0;
int last_search_position = 0;
while( ( nIndex = src.find( delimit, last_search_position ) ) != -1 )
{
if( nIndex == last_search_position )
{
int nValue = atoi( null_subst.c_str() );
vecRet.push_back( nValue );
}
else
{
int nValue = atoi( src.substr( last_search_position, nIndex - last_search_position ).c_str() );
vecRet.push_back( nValue );
}
last_search_position = nIndex + deli_len;
}
string last_one = src.substr( last_search_position );
if ( last_one.empty() )
{
int nValue = atoi( null_subst.c_str() );
vecRet.push_back( nValue );
}
else
{
int nValue = atoi( last_one.c_str() );
vecRet.push_back( nValue );
}
}
int main() {
string s;
while(getline(cin,s))
{
static int nLost = 0;
if(s.length() <= 1)
{
nLost = atoi(s.c_str());
continue;
}
vector<int> intNumber;
SplitString2Int(s," ",intNumber);
vector<int>::iterator It = intNumber.begin();
int nAll = 0;
int nCount = 0;
vector<vector<int>> mVectData;
vector<int> vData;
int nIndex = -1;
for (It; It != intNumber.end();It++)
{
nIndex++;
nCount++;
nAll+= *It;
if( (double)nAll / (double)nCount <= nLost)
{
vData.push_back(nIndex);
}
else
{
nCount = 0;
nAll = 0;
if(vData.size() < 2)
{
vData.clear();
continue;
}
mVectData.push_back(vData);
vData.clear();
}
}
if(1< vData.size())
{
mVectData.push_back(vData);
}
if(mVectData.size() < 1)
{
cout<<"NULL"<<endl;
return 0;
}
vector<vector<int>>::iterator vvIt = mVectData.begin();
nIndex = 0;
for (vvIt;vvIt != mVectData.end();vvIt++)
{
vector<int> vData = *vvIt;
int nSize = vData.size();
cout<<vData[0]<<"-"<<vData[nSize-1];
nIndex++;
if(nIndex != mVectData.size())
cout<<" ";
}
cout<<endl;
}
system("pause");
}
//第三题是实现一个计算器, div除法,sub减法,sum加法,乘法mul。小括号的层数没限制,感觉也挺难的,时间太紧没做完
/*
输入
(div 12 (sub 45 45))
输出 ERROR
输入
(div 12 (sub 45 42))
输出 4
*/