7-1 估值一亿的AI核心代码
函数:
string 中 find() 函数
string中find()返回值是字母在母串中的位置(下标记录),如果没有查询到,则返回string::npos(这是一个很大的数,其值不需要知道)
find返回值是size_type
string::size_type从本质上来说,是一个整型数int
可以用int
string s;
int rc = s.find(/*.....*/);
然后判断,
if ( rc == string::npos )
最好是用auto
string::size_type rc = s.find(/*.....*/);
或
auto rc = s.find(/*.....*/);
这个时候使用
if ( rc == string::npos )
就会正确了。
replace()替换函数
用法:
1.用str替换指定字符串从起始位置pos开始长度为len的字符
string& replace (size_t pos, size_t len, const string& str);
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
str=str.replace(str.find("a"),2,"#"); //从第一个a位置开始的两个字符替换成#
cout<<str<<endl;
return 0;
}
2.用str替换 迭代器起始位置 和 结束位置 的字符
string& replace (const_iterator i1, const_iterator i2, const string& str);
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
str=str.replace(str.begin(),str.begin()+5,"#"); //用#替换从begin位置开始的5个字符
cout<<str<<endl;
return 0;
}
3.用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "he is@ a@ good boy";
string substr = "12345";
str=str.replace(0,5,substr,substr.find("1"),4); //用substr的指定字符串替换str指定字符串
cout << str << endl;
return 0;
}
erase()删除函数
1.erase(pos,n);
删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
2.erase(position);
删除position处的一个字符(position是个string类型的迭代器)
it=str.begin()+1;
str.erase (it);
3.erase(first,last);
删除从first到last之间的字符(first和last都是迭代器)
str.erase (str.begin()+5, str.end()-7);
int ispunct(int c)函数
检查所传的字符是否是标点符号字符
int ispunct(char c);
如果参数是除字母,数字和空格外可打印字符,函数返回非零值,否则返回零值
如果 c 是一个标点符号字符,则该函数返回非零值(true),否则返回 0(false)
(本来想按自己的思路写代码的,但是可能是自己太菜了,最后实在是没写出来,看的网上大佬的代码思路)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
string str;
vector<string> v;
cin>>n;
getchar();
while(n--)
{
getline(cin,str);
cout<<str<<endl<<"AI: ";
for(int i=0;i<str.size();i++)
{
if(str[i]>='A'&&str[i]<='Z')
{
if(str[i]!='I')
{
str[i]=str[i]-'A'+'a';
}
}
else if((str[i]>='0'&&str[i]<='9')||(str[i]>='a'&&str[i]<='z'))
{ //小写字母或数字
continue;
}
else if(str[i]!=' ')
{ //标点符号
str.insert(i," ");
i++;
}
if(str[i] =='?')
{
str[i]='!';
}
}
string temp="";
for(int i=0;i<str.size();i++)
{ //以空格为分隔
if(str[i]!=' ')
{
temp+=str[i];
}
else
{
if(temp!="")
{
v.push_back(temp);
temp="";
}
}
}
if(temp!="")
{
v.push_back(temp);
}
for(int i=0;i<v.size();i++)
{
if(v[i]=="I"||v[i]=="me")
{
v[i]="you";
}
else if(v[i]=="can"||v[i]=="could")
{
if((i+1)<v.size())
{
if(v[i+1]=="you")
{
v[i+1] = v[i];
v[i]="I";
}
}
}
}
for(int i=0;i<v.size();i++)
{
cout<<v[i];
if(i==v.size()-1)
{
break;
}
if((v[i+1][0]>='0'&&v[i+1][0]<='9')||(v[i+1][0]>='a'&&v[i+1][0]<='z')||(v[i+1][0]=='I'))
{
printf(" ");
}
}
cout<<endl;
v.clear();//清空v数组,为下一趟循环做准备
}
return 0;
}
7-3 N个数求和
函数:
gcd();
int 和 long 和 long long 类型的都可以
int gcd(int a,int b)
{
if(b == 0)
return a;
return gcd(b,a%b);
}
返回两个或多个整数的最大公约数,最大公约数是能分别将各个参数除尽的最大整数
思路:
1.依次输入相加,并且通分
(注意通分的时候不要除0)
2.输出的时候要分情况变换成假分数
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
//求最大公约数函数
long long gcd(long long a,long long b)
{
if(a==0)
{
return 0;
}
else
{
return (b==0) ? a: gcd(b,a%b);
//固定格式要记住
}
}
int main()
{
int n;
long long a,b,c,d;
char d1,d2;
cin>>n;
cin>>a>>d1>>b;
int t=gcd(a,b);
//约分
if(a)
{
a=a/t;
b=b/t;
}
//依次输入相加
for(int i=1;i<n;i++)
{
cin>>c>>d2>>d;
//通分
long long b1=b/gcd(b,d)*d;
a=a*b1/b+c*b1/d;
b=b1;
int t2=gcd(a,b);
if(t2!=0)
{
a=a/t2;
b=b/t2;
}
//cout<<a<<"/"<<b<<endl;
}
//cout<<a<<"/"<<b<<endl;
if(a&&a/b==0)
{
cout<<a%b<<"/"<<b<<endl;
}
else if(a%b==0)
{
cout<<a/b<<endl;
}
else
{
cout<<a/b<<" "<<a%b<<"/"<<b<<endl;
}
}
7-12 月饼
思路:
1.构建结构体存入相关数据
2.自定义排序函数,用sort()快排
3.最后要用循环输出
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
struct moon
{
double money;
double num;
double sum;
}mon[1010];
bool comp(moon m1,moon m2)
{
return m1.num>m2.num;
}
int main()
{
int n,d;
cin>>n>>d;
for(int i=0;i<n;i++)
{
cin>>mon[i].sum;
}
for(int i=0;i<n;i++)
{
cin>>mon[i].money;
}
for(int i=0;i<n;i++)
{
mon[i].num=mon[i].money/mon[i].sum;
}
sort(mon,mon+n,comp);
double sum1=0;
for(int i=0;i<n;i++)
{
if(d>=mon[i].sum)
{
sum1+=mon[i].money;
d=d-mon[i].sum;
}
else
{
sum1=sum1+d*mon[i].num;
break;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<sum1;
}
7-10 链表去重
(向大佬学习的代码)
思路:
根据第一个节点地址,依次往下排,并标记链表的顺序
然后根据绝对值将链表分成两部分
#include <bits/stdc++.h>
using namespace std;
struct num
{
int date;
int next;
}x[100000];
int vis[100000], y[100000], a[100000], b[100000];
int main()
{
ios::sync_with_stdio(false);
int d,num,cnt=0;
cin >> d >> num;
for(int i=0; i<num; i++)
{
int a, b, c;
cin >> a >> b >> c;
x[a].date = b;
x[a].next = c;
}
num=0; //重新统计可用的节点数
while(d!=-1)//按照第一个结点地址依次往下排
{
y[cnt++] = d;
d = x[d].next;
num++;
}
//将链表按照要求分为两部分
int cnt1=0,cnt2=0,k;
for(int i=0; i<num; i++)
{
k = x[y[i]].date;
if(vis[(int)fabs(k)])
{
b[cnt2++] = y[i];
}
else
{
a[cnt1++] = y[i];
vis[(int)fabs(k)] = 1;
}
}
for(int i=0; i<cnt1; i++)
{
cout << setw(5) << setfill('0') << a[i] << " " << x[a[i]].date<<" ";
if(i!=cnt1-1)
{
cout << setw(5) << setfill('0') << a[i+1]<<endl;
}
else cout << -1 <<endl;
}
for(int i=0; i<cnt2; i++)
{
cout << setw(5) << setfill('0') << b[i] << " " << x[b[i]].date<<" ";
if(i!=cnt2-1)
{
cout << setw(5) << setfill('0') << b[i+1]<<endl;
}
else cout << -1 <<endl;
}
return 0;
}
7-11 部落
(看的大佬的代码)
并查集,首先把套并查集的模板,然后用set数组存有多少人和互不相交的部落个数,set可以防止重复
#include<bits/stdc++.h>
using namespace std;
int father[10010];
set<int> st;
int findfather(int x)
{
int a = x;
while(x != father[x])
{
x = father[x];
}
while(a != father[a])
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void unionfather(int a, int b)
{
st.insert(a);
st.insert(b);
int fa = findfather(a);
int fb = findfather(b);
if(fa != fb) father[fa] = fb;
}
int main()
{
int n, m, s, t;
cin >> n;
for(int i = 1; i < 10010; i++) father[i] = i;
while(n--)
{
cin >> m >> s;
if(m == 1) unionfather(s, s);
else
{
for(int i = 0; i < m - 1; i++)
{
cin >> t;
unionfather(s, t);
}
}
}
set<int> sum;
for(set<int>::iterator it = st.begin(); it != st.end(); it++)
{
sum.insert(findfather(*it));
}
cout << st.size() << " " << sum.size() << endl;
cin >> n;
int a, b;
while(n--)
{
cin >> a >> b;
if(findfather(a) == findfather(b)) cout << "Y\n";
else cout << "N\n";
}
}