牛客网-考研机试(排序部分)
KY74+KY115+ky168
对字符串按照ascii码排序。
我的题解:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string a;
cin>>a;
sort(a.begin(),a.end());
cout<<a;
}
参考: 用冒泡排序
#include<stdio.h>
#include<string.h>
int main(){
char str[20];
while(scanf("%s",str)!=EOF){
int length=strlen(str);
for(int i=0;i<length;i++){
for(int j=0;j<length-1-i;j++){
if(str[j]>str[j+1]){
char tmp=str[j];
str[j]=str[j+1];
str[j+1]=tmp;
}
}
} //冒泡排序
for(int i=0;i<length;i++){
printf("%c",str[i]);
}
printf("\n");
}
return 0;
}
后缀字串
1.我的题解:
遇到的问题:
a. string.substr( start, len), 起始位置包括在内,截取长为len的子串,下标从0开始。默认开始为0
b. sort 排序,sort(m,m+len); //排序的起始和结束地址 ,注意是地址。
而且结束的位置是你要排序的下一个(最后一个数据的后一个数据的地址)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string a;
cin>>a;
int len =a.length();
string m[len];
for (int i=0;i<len;i++){
m[i]=a.substr(len-i-1,i+1);
}
sort(m,m+len); //排序的起始和结束地址 ,不用减去1,最后一个数的后一个数
for(int i=0;i<len;i++)
{cout<<m[i]<<endl;}
cin >>a;
}
其他方法:
1.用map:
map<string,int>child;
for(int i=0;i<str.size();i++){
child[str.substr(i)]++; //这里substr,是指从i开始,一直到最后
}
map<string,int>::iterator it;
for(it=child.begin();it!=child.end();it++){
cout<<it->first<<endl;
2.还可用set
字符串内排序:
1.问题:没有看清题目,有多组输入。
其次没有输出endl;
也没有返回值
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string a;
cin>>a;
sort(a.begin(),a.end());
cout<<a;
}
修改:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string a;
while(cin>>a){
sort(a.begin(),a.end());
cout<<a<<endl;
}
return 0;
}
KY68-KY84-KY166
1.注意看数的范围,要不要long long
子串计算:
map库简介:
https://blog.csdn.net/qq_39798042/article/details/81082762
//map功能太强大了,如果面试不提供这个库,立马就GG了
#include<iostream>
using namespace std;
#include<string>
#include<map>
int main() {
string s;
while (cin >> s) {
map<string, int> m;
//对s遍历
for (int i = 0;i<=s.size();i++)
for (int j = 0;j<i;j++)
m[s.substr(j,i-j)]++;
//自动变量
for (auto it = m.begin();it != m.end();it++) {
if (it->second > 1) //second
cout << it->first << ' ' << it->second << endl;
}
}
}
不用库
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 5050
#define LEN 101
using namespace std;
struct STR{
char s[LEN];
unsigned count;
}buf[N];//子串集合
int n;//子串的数量
char str[LEN];//读取的字符串
char child[LEN];//临时存储子串
void MakeChild(int from, int len)
{//读取字符串中的子串,存入child字符串。子串从from开始,长度为len
for(int i=0; i<len; i++)
{
child[i]=str[from+i];
}
child[len]='\0';
}
bool cmp(STR a, STR b)
{
return strcmp(a.s, b.s)<0;
}
void LetsGo()//生成子串列表并完成统计
{
int len=strlen(str);
for(int i=1; i<len; i++)//i是子串长度
{
for(int from=0; from+i<=len; from++)
{
MakeChild(from, i);//生成子串
bool repeat=false;//用来检查这个字串以前是否出现过。先假设没出现过
for(int i=0; i<n; i++)
{
if(strcmp(buf[i].s, child)==0)
{//如果这个字串以前就出现过,那就直接计数器+1
buf[i].count++;
repeat=true;
break;
}
}
if(!repeat)
{//这个字串之前没出现过,那就把这个字串加入字串列表,计数器为1
buf[n].count=1;
strcpy(buf[n++].s, child);
}
}
}
}
int main()
{
while(gets(str))
{
n=0;//子串列表清零
LetsGo();//生成子串列表并完成统计
sort(buf, buf+n, cmp);//给子串列表排序
for(int i=0; i<n; i++)
{
if(buf[i].count>1)//如果出现过不止一次,那就输出
{
printf("%s %d\n", buf[i].s, buf[i].count);
}
}
}
return 0;
}
数字求和:
我的代码:
#include<iostream>
using namespace std;
int main(){
int a,sum=0;
cin >> a;
int tmp;
for (int i = 0; i < 5; i++){
cin >> tmp;
if (tmp < a)
sum=sum+tmp;
}
cout << sum;
}
字符求逆:
我的代码和问题:
#include<iostream>
#include<string>
using namespace std;
int main(){
char a[200]; //不该用string类
int m = 0;
a[m] = cin.get();
while(a[m]!='\n'){ //判断是否结束,不用' ',用\n
m = m + 1;
a[m] = cin.get();
}
for (int i = m-1; i>-1;i--) //am,是\n,去了
{
cout << a[i];
};
}
1.
char s[200];
while(scanf("%s",&s)!=EOF),用scanf
加个 printf("\n");
2.
用string 库和逆序:
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
string s; //不是s[5]??
while(cin>>s){
reverse(s.begin(),s.end());
cout<<s<<endl;
}
return 0;
}
sting类:string 是定义一个字符串,存储的是一段如“abcd”的数据,而且最后还有一个结束符'\0';

浙公网安备 33010602011771号