每日算法 - day 12

每日算法

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.2.23


记录下来自己做题时得思路,并不一定是最优解

pat甲-1023 Have Fun with Numbers (hash,字符串,高精度)

卡了计算出来为0的数据 大家写的时候要小心

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
int book[13];
// 高精度乘法 / 加法 
string add(string a,string b)
{
	string ans;
	int len = a.size() - 1,j = 0;
	for(int i = len; i >= 0; i--)
	{
		int sum = a[i] - '0' + b[i] - '0' + j;
		j = 0;
		while(sum - 10 >= 0){
			j++;sum-=10;
		}
		ans += to_string(sum);
	}
	if(j > 0)ans += to_string(j);
	reverse(ans.begin(),ans.end());
	return ans;
}
int main()
{
	string a;
	cin >> a;
	for(int i = 0;i < a.size() ;i ++)book[a[i] - '0']++;
	
	string ans = add(a,a);
	
	for(int i = 0;i < ans.size();i++)book[ans[i] - '0']--;
	
	// 卡了计算出来某个位置为0的数字 
	for(int i = 0;i <= 9 ; i++)
	{	
		if(book[i] != 0)
		{
			cout << "No" << endl << ans; 
			return 0;
		}
	}	
	cout << "Yes" << endl << ans << endl;
	return 0;
}

pat甲-1024 Palindromic Number (高精度,字符串,回文数)

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
string n;
int  k;
bool check(string s)
{
	for(int i = 0,j = s.size() - 1;j >= 0;i++,j--)
	{
		if(s[i] != s[j])return false;
	}
	return true;
}

string add(string a,string b)
{
	string ans;
	int j = 0;
	for(int i = a.size() - 1;i >= 0; i--)
	{
		int sum = a[i] - '0' + b[i] - '0' + j;
		j = 0;while(sum - 10 >= 0){
			j++;sum-=10;
		}
		ans += to_string(sum);
	}
	if(j > 0)ans += to_string(j);
	reverse(ans.begin(),ans.end());
	return ans;
}

void dfs(string x,int step)
{
	if(step == k && !check(x))
	{
		cout << x << endl << k;
		return;
	}
	if(check(x))
	{
		cout << x << endl << step;
		return;
	}else {
		string t = x;
		reverse(t.begin(),t.end());
		string sum = add(x,t);
		dfs(sum,step+1);
	}
	
}
int main()
{
	cin >> n >> k;
	dfs(n,0);
	return 0;
}
posted @ 2020-02-23 11:16  _starsky  阅读(180)  评论(0编辑  收藏  举报