题目
思路
先把字符串转化为一个用最小单位表示的整数,最后输出的时候取余就可以
知识点
这道题真是一言难尽,发现自己好多知识点不会
1. string查找特定字符
//查找s 中flag 出现的所有位置。
flag="a";
position=0;
int i=1;
//使用find查找时,返回第一次出现的下标
while((position=s.find_first_of(flag,position))!=string::npos)
{
//position=s.find_first_of(flag,position);
cout<<"position "<<i<<" : "<<position<<endl;
position++;
i++;
}
2. string提取子字符串
while (begpos < length){
size++;
begpos = str.find_first_not_of(defstr,begpos);
if (begpos == std::string::npos)
return -1;
endpos = str.find_first_of(defstr, begpos);
if (endpos == std::string::npos)
{
endpos = length;
}
//substr不会提取下标为endpos的字符,第二个参数是截取的字符串长度
std::string ssubstr = str.substr(begpos, endpos - begpos);
cont.push_back(ssubstr);
begpos = endpos + 1;
}
代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//用于将字符串转化为整形
int chaiFen(string& s){
int begPos=0, endPos=0, sum=0;
endPos=s.find_first_of(".",begPos);
sum += stoi(s.substr(begPos,endPos-begPos)) * 29 *17;
begPos = endPos+1;
endPos = s.find_first_of(".",endPos+1);
sum += stoi(s.substr(begPos,endPos-begPos)) * 29;
begPos = endPos+1;
sum += stoi(s.substr(begPos,s.size()-begPos));
return sum;
}
int main(){
string s1,s2;
cin >> s1 >> s2;
int a,b;
a = chaiFen(s1);
b = chaiFen(s2);
int ans = b - a;
if(ans < 0){
cout << '-';
ans = -ans;
}
cout << ans/(17*29) << '.' << ans%(17*29)/29 << '.' << ans%29;
return 0;
}