每日算法

每日算法

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.3.2


lqb 十六进制转八进制

先将十六进制转化为二进制 ,二进制每四位可以表示一个十六进制的数字
得到十六进制的二进制形式之后在将其长度补足为3的倍数,挑选每三位二进制数字进行计算
因为每三位二进制数可以表示一个八进制数,但是在计算时要将二进制的前导0给去掉

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    for(int k = 1; k <= n; k++)
    {
        string s1,s2;//s1为输入的原始的十六进制串,s2为转化成的二进制串
        cin >> s1;
        s2="";//初始化
        for(int i = 0;i < s1.length(); i++)//遍历,字符串上加上每一位
        {
            switch(s1[i])
            {
                case '0':s2+="0000";break;
                case '1':s2+="0001";break;
                case '2':s2+="0010";break;
                case '3':s2+="0011";break;
                case '4':s2+="0100";break;
                case '5':s2+="0101";break;
                case '6':s2+="0110";break;
                case '7':s2+="0111";break;
                case '8':s2+="1000";break;
                case '9':s2+="1001";break;
                case 'A':s2+="1010";break;
                case 'B':s2+="1011";break;
                case 'C':s2+="1100";break;
                case 'D':s2+="1101";break;
                case 'E':s2+="1110";break;
                case 'F':s2+="1111";break;
                default:break;
            }
        }
        int len=s2.length();
 
        if(len % 3 == 1)//三个二进制为一位八进制,二进制串前面补0,确保二进制串的长度为3的倍数
            s2= "00" + s2;
        else if(len % 3 == 2)
            s2= "0" + s2;
        int flag = 0;
        for(int i= 0;i <= s2.length() - 3 ; i+=3)
        {
            int num = 4 * (s2[i]-'0') + 2 * (s2[i+1]-'0') + (s2[i+2]-'0');
            if(num)flag=1;//忽略前导0
            if(flag)cout<<num;
        }
        cout<<endl;
    }
    return 0;
}

lqb 十六进制转十进制

类似于求字符串转化为十进制的数字的形式 只不过把
所乘的进制数改变了形式而已

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

using namespace std;

string s;
void work(string s)
{
	long long ans = 0;
	for(int i = 0;i < s.size() ;i ++)
	{
		if(s[i] >= 'A' && s[i] <= 'F')
		{
			int t = s[i] - 'A' + 10;
			ans = ans * 16 + t;
		}else{
			int t = s[i] - '0';
			ans = ans * 16 + t;
		}
	}
	cout << ans << endl;
}
int main()
{
	cin >> s;
	work(s);
	return 0;
}

lqb 十进制转十六进制

注意零要特判

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


using namespace std;
char a[] = {'0','1','2','3','4','5','6','7','8','9','A'
,'B','C','D','E','F'};
void work(int n)
{
	string ans;
	while(n)
	{
		ans += a[n % 16];
		n /= 16;
	}
	reverse(ans.begin(),ans.end());
	cout << ans << endl;
}
int main()
{
	int n;
	cin >> n;
	if(n == 0){
		cout << 0 << endl;
	}else work(n);
	return 0;
}

lqb 特殊的回文数

#include <iostream>
#include <algorithm>
#include <string>
#include <istream>
#include <sstream>
using namespace std;
int n;
string tostring(int i)
{
	stringstream s;
	s << i;
	string ans = s.str();
	return ans;
}
bool check(int i)
{
	string s = tostring(i);
	int sum = 0;
	for(int i = 0,j = s.size() - 1;i < s.size() ;i++,j--)
	{
		if(s[i] != s[j])return false;
		sum += s[i] - '0';
	}
	if(sum == n)return true;
	else return false;
}
int main()
{
	cin >> n;
	
	for(int i = 10000;i <= 999999;i ++)
	{
		if(check(i)){
			printf("%d
",i);
		}
	}
	return 0;
}

lqb 回文数

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <istream>
#include <sstream>

using namespace std;

string tostring(int x)
{
	stringstream s;
	s << x;
	string ans = s.str();
	return ans;
}
bool check(int x)
{
	string s = tostring(x);
	for(int i = 0 ,j = s.size() - 1;i < s.size();i++,j--)
	{
		if(s[i] != s[j])return false;
	}
	return true;
}
int main()
{
	for(int i = 1000;i <= 9999;i ++)
	{
		if(check(i))
		{
			printf("%d
",i);
		}
	}
	return 0;
}

lqb 特殊的数字

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;

int f(int x)
{
	return x * x * x;
}
bool check(int x)
{
	int a = x % 10, b = x / 10 % 10,c = x / 100;
	if(f(a) + f(b) + f(c) == x)return true;
	else return false;
} 
int main()
{
	for(int i = 100;i <= 999;i ++)
	{
		if(check(i))printf("%d
",i);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/wlw-x/p/12398453.html