算法笔记 PTA B1016 String 用法

字符串暴力求解
#include <stdio.h>
#include
#include
#include
#include
#include
using namespace std;
int main()
{
string s1;
string s2;
char a;
char b;
cin>>s1;
cin>>a;
cin>>s2;
cin>>b;
int k=0;
string ::iterator ite;
ite=s1.begin();
for(size_t i=0;i<s1.size();i++)//size_t 类型表示C 中任何对象所能达到的最大长度。它是无符号整数
{
if(ite[i]a)
k++;
}
int a1=0;
int x=a-48;
while(k–)
{
a1+=x;
x=x10;
}
k=0;
ite=s2.begin();
for(size_t i=0;i<s2.size();i++)//size_t 类型表示C 中任何对象所能达到的最大长度。它是无符号整数
{
if(ite[i]==b)
k++;
}
int a2=0;
x=b-48;
while(k–)
{
a2+=x;
x=x
10;
}
printf("%d",a1+a2);
return 0;
}
#################################################
#################################################
法二
#include <stdio.h>
#include
#include
#include
#include
#include
using namespace std;
int main()
{
long long A,a,B,b,x=0,y=0;
cin>>A>>a>>B>>b;
while(A!=0)
{
if(A%10
a)
x=x10+a;
A/=10;
}
while(B!=0)
{
if(B%10==b)
y=y
10+b;
B/=10;
}
cout<<x+y;
return 0;
}
#################################################
#################################################
String迭代器三种用法:

#define _SCL_SECURE_NO_WARNINGS

#include
#include //要与c语言里面的#include <string.h>区分
using namespace std;

int main(){

string str("abcdefg");
string::iterator ite;

ite = str.begin();
for (size_t i = 0; i < str.size(); i++){
	cout << *ite;
	ite++;
}
ite = str.begin();
cout << endl;
for (size_t i = 0; i < str.size(); i++){
	cout << ite[i];
}
cout << endl;
ite = str.begin();
for (; ite != str.end(); ite++){
	cout << *ite;
}

cout << endl;

str.append(10, 'a');
//cout << ite[16] << endl; 迭代器失效

system("pause");
return 0;

}

作者:IT1995
来源:CSDN
原文:https://blog.csdn.net/qq78442761/article/details/79661894
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/weixin_42424319/article/details/89176393
今日推荐