PAT (乙级) 1037 在霍格沃茨找零钱

题目

题目链接

思路

先把字符串转化为一个用最小单位表示的整数,最后输出的时候取余就可以

知识点

这道题真是一言难尽,发现自己好多知识点不会

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值