Day Three(今天没有算法,string容器,几道简单算法)
代码实现:
#include<iostream>
using namespace std;
#include<string>
void test01() {
string str1;//默认构造
const char* ch = "hello c++";
string str2(ch);//第二种构造方法
cout << str2 << endl;
string str3(str2);//第三种构造方法拷贝
cout << "str3=" << str3 << endl;
string str4(10,'a');//第四中构造方法
cout << "str4=" << str4 << endl;
}
void test02() {
//赋值操作
string s1;
s1 = "hello c++???";//第一种赋值操作
cout << s1 << endl;
string s2;
s2 = s1;//第二种赋值操作
cout << "s2= " << s2 << endl;
string s3;
s3 = 'a';//第三种赋值操作
cout << "s3=" << s3 << endl;
string s4;
s4.assign("hello world!!!");//第四种赋值操作
cout << "s4=" << s4 << endl;
string s5;
s5.assign("hello worid", 8);//第五种复制操作
cout << "s5=" << s5 << endl;
string s6;
s6.assign(s5);//第六种赋值操作
cout << "s6=" << s6 << endl;
string s7;
s7.assign( 10,'a');
cout << "s7=" << s7 << endl;
}
void test03() {
string s1 = "我";
s1 += "爱中华";//第一种字符串拼接
cout << s1 << endl;
s1 += ':';//第二种字符串拼接
cout << s1 << endl;
string s2 = "我爱毛主席";
s1 += s2;//第三种字符串拼接
cout << s1 << endl;
string s3;
s3 = "I";
s3.append("love");//第四种
cout << s3 << endl;
s3.append("game: abcd", 5);
cout << s3 << endl;
string s4= "LOL DNF";
s3.append(s4);
cout << s3 << endl;
s3.append(s4, 0, 3);
cout << s3 << endl;
}
void test04() {//查找
string str = "asdfghjk";
int pos = str.find("jk");//从左
if (pos == -1) {
cout << "未找到"<<endl;
}
else {
cout << "找到字符串pos=" << pos << endl;
}
int pos2 = str.rfind("jk");
cout << pos2 << endl;
str.replace(1, 2, "111");//从第一个位置2个字符替换成111
cout << str << endl;
}
void test05() {
//字符串的比较
string s1 = "123456";//相等返回0 >返回1 <返回-1
int pos = s1.compare("123");
switch (pos) {
case(0):
cout << "两个字符串相等" << endl;
break;
case(1):
cout << "s1大" << endl;
break;
case(-1):
cout << "s2" << endl;
break;
}
}
void test06() {
//字符串的存取
string str = "hello";
for (int i = 0; i < str.size(); i++) {
cout << str[i] << " ";
}
cout << endl;
for (int j = 0; j < str.size(); j++) {//运用at方式
cout << str.at(j) << " ";
}
cout << endl;
//修改单个字符
str[0] = 'x';
cout << str << endl;
str[0] = 'q';//运用at方式
cout << str << endl;
}
void test07() {
//字符串的插入或删除
//用insert进行插入
string str = "hello";
str.insert(2, "llll");
cout << str << endl;
str.insert(4,4, '5');
cout << str << endl;
//用erase进行删除
str.erase(2, 4);
cout << str << endl;
}
void test08() {
//子串的获取
string str = "helloeee";
string str2 = str.substr(1, 3);
cout << str2 << endl;
string emeil = "zhangsan@qq.com";
int pos = emeil.find('@');
string str3 = emeil.substr(0, pos);
cout << str3 << endl;
}
int main() {
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
return 0;
}
来道简单的算法题:
题目描述
在不考虑字符排列的条件下,对于相差只有一个字符的两个字符串,实现一个算法来识别相差的那个字符。要求如下:
-
当传入的字符串为
aad
和ad
时,结果为a
。 -
当传入的字符串为
aaabccdd
和abdcacade
时,结果为e
。
输入描述
输入两行字符串,长度均不超过 100。
输出描述:
输出一行,为相差的那个字符。
输入输出样例
示例
输入
aad ad
输出
a
代码实现:
#include<bits/stdc++.h> using namespace std; int main(){ string a ,b; cin>>a; cin>>b; sort(a.begin(),a.end()); sort(b.begin(),b.end()); int len = a.size(); for(int i=0;i<len;i++){ if(a[i]!=b[i]){ cout<<a[i]; break; }
}
return 0;
}
代码讲解:
输入两个字符串,先对字符串进行排列(这里运用sort()函数),在对两个数进行遍历,直到遍历到不一样的就是我们要找的字符,这里用break结束循环。
题目描述
给定一个数组,找到两个总和为特定值的索引。
例如给定数组 [1, 2, 3, -2, 5, 7],给定总和 7,则返回索引 [1, 4]。
若有多组符合情况则输出索引对中小索引最小的一组。
输入描述
第一行为给定数组的长度,不超过 100。
第二行为数组元素,元素大小不超过 100(可能为负数)。
第三行为特定值。
输出描述
输出一行,为两个索引值,升序输出。
输入输出样例
示例
输入
6 1 2 3 -2 5 7 7
输出
1 4
代码实现:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[100];
int result[2];
for(int i=0;i<n;i++){
cin>>a[i];
}
int sum;
cin>>sum;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i]+a[j]==sum){
cout <<i <<" "<<j<<endl;
return 0;
}
}
}
return 0;
}
说明:
双重循环,暴力求解。
题目描述
给定一个整数 NN,从 1 到 NN 按照下面的规则返回每个数:
-
如果这个数被 3 整除,返回
Fizz
。 -
如果这个数被 5 整除,返回
Buzz
-
如果这个数能同时被 3 和 5 整除,返回
FizzBuzz
。 -
如果这个数既不能被 3 也不能被 5 整除,返回这个数字。
输入描述
输入 N,1<N≤104N,1<N≤104.
输出描述
输出一行,如题干所述。
输入输出样例
示例
输入
15
输出
FizzBuzz
代码实现:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >>n;
if(n%3==0&&n%5!=0){
cout<<"Fizz"<<endl;
} else if(n%3!=0&&n%5==0){
cout <<"Buzz"<<endl;
} else if(n%3==0&&n%5==0){
cout<<"FizzBuzz"<<endl;
} else{ cout<<n<<endl;
}
return 0;
}
说明:
if语句实现(简单)
题目描述
实现选择排序算法。介绍如下:
选择排序的工作原理是每一次从需要排序的数据元素中选出最小的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排列完毕。
请编写代码,完成选择排序,对给定数据进行升序排列
输入描述
第一行,数字 N (2≤N≤100)N (2≤N≤100),表示待排序的元素个数。
第二行,待排序的元素。
输出描述
输出一行,为升序序列。
输入输出样例
示例
输入
6 7 1 4 8 5 2
输出
1 2 4 5 7 8
代码实现:
冒泡排序:
#include <iostream>
using namespace std;
int main() {
int n;
int a[100];
cin>>n;
for(int i=0;i<n;i++){ cin>>a[i];
} int temp; for(int i =0;i<n;i++){ for(int j=i+1;j<n;j++){ if(a[i]>a[j]){ temp = a[i]; a[i] =a[j]; a[j]=temp; } } } for(int i =0 ;i<n;i++){ cout<<a[i]<<" "; }
return 0; }
sort()函数:
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int a[100]; for(int i=0;i<n;i++){ cin>>a[i];
}
sort(a,a+1+n);//[a,a+1+n) for(int i=0;i<n;i++){ cout <<a[i]<<" "; } return 0; }