11.数字颠倒
【思路】:基础题,简单办法就是不要跟着题目标准输入来,直接将输入当作字符串,反向遍历
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
for(int i=s.length()-1;i>=0;i--)
{
cout<<s[i];
}
cout<<endl;
}
}
12.字符串反转
【思路】:答案和上题一样
13.句子逆序
【思路】:字符串逆序的升级,方法很多,先说个本办法,对句子进行逆序扫描,保存每个单词,遇到空格就输出前一个单词
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s;
while(getline(cin,s))
{
string word ="";
for(int i = s.length()-1;i>=0;i--)
{
if(s[i] !=' ')
{
word.append(1,s[i]);
}
else
{
reverse(word.begin(),word.end());
cout<<word;
cout<<' ';
word ="";
}
}
reverse(word.begin(),word.end());
cout<<word<<endl;
}
}
【思路二】:用压栈方法,后进先出
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
stack<string> ss;
string s;
while(cin>>s)//cin是遇到空格就结束
{
ss.push(s);
}
while(!ss.empty())
{
cout<<ss.top();
ss.pop();
if(!ss.empty())
cout<<' ';
}
cout<<endl;
}
【思路三】:保存前一个单词,读到当前单词,加上前一个单词
#include <iostream>
#include <string>
#include <sstream>
using
namespace
std;
int
main() {
string s;
getline(cin,s);
stringstream ss(s);
string res=
""
, tmp;
while
(ss>>tmp) {
if
(res==
""
)
res=tmp;
else
res=tmp+
" "
+res;
}
cout<<res;
return
0;
}
13.字符串连接最长路径查找
【思路】:使用STL中可重复set
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
int N=0;
cin>>N;
string word="";
multiset<string> strset;
while(N--&&cin>>word)
{
strset.insert(word);
}
for(multiset<string>::iterator iter = strset.begin();iter!=strset.end();iter++)
{
cout <<*iter<<endl;
}
}
14.求int型正整数在内存中存储1的个数
【思路】:一个整数,用自己和自己&操作,每次都能消去该数字二进制中的1
#include <iostream>
using namespace std;
int main()
{
int x;
while(cin>>x)
{
int count =0 ;
while(x!=0)
{
x=x&(x-1);
count++;
}
cout <<count<<endl;
}
}
15.购物单
【思路】:动态规划
pass
16.坐标移动
【思路】:坐标分割后,一个一个条件判断是否符合,最后计算
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
while(getline(cin,s))
{
int pre = 0;
int x = 0, y = 0;
for (int i = 0; i<s.length(); i++)
{
if (s[i] == ';' && (i == pre))
{
pre++;
continue;
}
if (s[i] == ';' && (i != pre))
{
string word = s.substr(pre, i-pre);
pre = i + 1;
int step = 0;
if (word.length() ==3)
{
if (word[1] >= '0'&&word[1] <= '9'&&word[2] >= '0'&&word[2] <= '9')
{
step = (word[1] - '0') * 10 + (word[2] - '0');}
}
if (word.length() == 2)
{
if (word[1] >= '0'&&word[1] <= '9')
{
step = word[1] - '0';}
}
if (step)
{
switch (word[0])
{
case 'A':
{
x -= step;
break;
}
case 'D':
{
x += step;
break;
}
case 'W':
{
y += step;
break;
}
case 'S':
{
y -= step;
break;
}
}
}
}
}
cout << x << "," << y << endl;
}
}
18.简单错误记录
【思路】:自定义数据结构+队列,这里用vector,从倒数第八项开始输出
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>using namespace std;
string getFileName(string path)
{
int pos = path.rfind('\\');
return path.substr(pos+1);
}
string modifyName(string name)
{
if(name.size()>16)
{
name = name.substr(name.size()-16);
}
return name;
}
struct ErrRecord
{
string file;
int lineNo;
int count;
ErrRecord(string file,int lineNo)
{
this->file = file;
this->lineNo = lineNo;
count =1;
}
bool operator==(const ErrRecord &a)
{
return (file==a.file)&&(lineNo==a.lineNo);
}
};int main()
{
string file;
int lineNo;
vector<ErrRecord> myvec;
while(cin>>file>>lineNo)
{
ErrRecord record(getFileName(file),lineNo);
auto res =find(myvec.begin(),myvec.end(),record);
if(res == myvec.end())
{
myvec.push_back(record);
}
else
{
res->count++;
}
}
int count =0;
for(auto item:myvec)
{
if(count+8>=myvec.size())
{
cout<<modifyName(item.file)<<" "<<item.lineNo<<" "
<<item.count<<endl;
}
count++;
}
return 0;
}
19.密码验证合格程序
【思路】:逐个条件检查看是否符合要求。第三个条件:不能有相同长度超2的子串重复——可以分两种思路,一种就是选取任意三个字符和字符串中剩下的三个字符挨个比较;第二种就是用str.find()函数查找剩下子字符串中是否包含选取的长度为3的字符串
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int len = s.length();
int flag[5]={0};
if(len<=8)
{
cout <<"NG"<<endl;
continue;
}
for(int i=0;i<len;i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
flag[0]=1;
}
else if(s[i]>='a'&&s[i]<='z')
{
flag[1]=1;
}
else if(s[i]>='0'&&s[i]<='9')
{
flag[2]=1;
}
else
{
flag[3]=1;
}
}
if(flag[0]+flag[1]+flag[2]+flag[3]<3)
{
cout<<"NG"<<endl;
continue;
}
for(int i=0;i<=len-6;i++)
{
for(int j=i+3;j<len;j++)
{
if(s[i]==s[j]&&s[i+1]==s[j+1]&&s[i+2]==s[j+2])
{
flag[4]=1;
break;
}
}
}
if(flag[4]==1)
{
cout <<"NG"<<endl;
}
else
{
cout <<"OK"<<endl;
}
}
return 0;
}
20.简单密码
【思路】:自定义一个映射逻辑,可以直接用map容器,或者费点事自己写个映射关系
#include <iostream>
#include <string>
using namespace std;
char changetoPassword(char& src)
{
char result;
switch(src)
{
case '1':
result = '1';
break;
case 'a':
result ='2';
break;
case 'b':
result ='2';
break;
case 'c':
result ='2';
break;
case 'd':
result ='3';
break;
case 'e':
result ='3';
break;
case 'f':
result ='3';
break;
case 'g':
result ='4';
break;
case 'h':
result ='4';
break;
case 'i':
result ='4';
break;
case 'j':
result ='5';
break;
case 'k':
result ='5';
break;
case 'l':
result ='5';
break;
case 'm':
result ='6';
break;
case 'n':
result ='6';
break;
case 'o':
result ='6';
break;
case 'p':
result ='7';
break;
case 'q':
result ='7';
break;
case 'r':
result ='7';
break;
case 's':
result ='7';
break;
case 't':
result ='8';
break;
case 'u':
result ='8';
break;
case 'v':
result ='8';
break;
case 'w':
result ='9';
break;
case 'x':
result ='9';
break;
case 'y':
result ='9';
break;
case 'z':
result ='9';
break;
case '0':
result ='0';
break;
case 'Z':
result = 'a';
break;
default:
result = src;
break;
}
if(src>='A'&&src<'Z')
{
result =tolower(src+1);
}
return result;
}int main()
{
string s;
while(cin>>s)
{
for(int i=0;i<s.length();i++)
{
s[i] = changetoPassword(s[i]);
}
cout <<s<<endl;
}
return 0;
}