蓝桥杯练习系统全题解(持续更新)

1. 入门训练
Fibonacci数列

#include   
using namespace std;

int main()
{
	int n = 0;
	while(cin >> n)
	{
		long long f1 = 1 , f2 = 1 , fi = 0;
		long long ans = 1;	
		for( int i = 2 ; i < n ; i++ )
		{
		//  f1 , f2 , fi;
			fi = f1 + f2;
			f1 = f2;
			f2 = fi % 10007;
			ans = fi % 10007;
		}
		cout << ans << endl;
	} 
	return 0;
}

圆的面积

#include  
#include 
#include 
#define PI 3.14159265358979323

using namespace std;

int main(void)
{
	int r;
	while ( cin >> r && r <= 10000 && r >= 1 && r != EOF ) 
	{
		double s;
		s = PI * pow(r,2);
		cout << fixed << setprecision(7) << s << endl;
	}
	return 0;
}

序列求和

#include
using namespace std;
int main()
{
	long long n = 0;
	while(cin>>n&&n!=EOF)
	{
		long long ans = 0;
		ans = ( 1 + n ) * n / 2; 
		cout << ans << endl;
	}
	return 0;
}

A+B问题

#include 
 
using namespace std;
 
int main()
{
    int a, b;
    cin >> a >> b;
    cout << a + b;
    return 0;
}

2. 基础练习
数列排序

#include  
#include  
using namespace std;
int main(void)
{
	int n = 0 , f = 0; 
	cin >> n;
	int *a = new int[n];
	while(n--)
	{
		cin >> a[f];
		f += 1 ;
	}
	sort(a, a + f);
	for(int i = 0; i < f; i++)
	{
		if(i <= f - 1 ) 
		cout << a[i] << " ";
		else
		cout << a[i];
	}
	return 0;
}

十六进制转八进制

#include
    #include
    using namespace std;
    int main()
    {
        string s1,s2;
        int n;
        int i,j,k;
        cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>s1;
            s2="";
            for(j=0;j<s1.length();j++)
            {
                switch(s1[j])
                {
                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;
                }
            }
            if(s2.length()%3==1)
            s2="00"+s2;
            if(s2.length()%3==2)
            s2="0"+s2;
            int flag=0;
            for(k=0;k<s2.length()-2;k+=3)
            {
                int p=4*(s2[k]-'0')+2*(s2[k+1]-'0')+s2[k+2]-'0';
                if(p)
                flag=1;
                if(flag)
                cout<<p;
             } 
             cout<<endl;
         } 
         return 0;
}

十六进制转十进制

#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep for(int i = 0; i < n; i++)
#define setprecision spc
using namespace std;

int main()
{
	long long sum = 0;
	string s1 , s2 ;
	cin >> s1;
	int flag = 0;
	for(int i = s1.length() - 1; i >= 0; i--)
	{
//		cout<<"s1["<
		if((int)s1[i] - 48 > 9)
		{
			switch(s1[i])
			{
				case 'A':sum += 10 * pow(16,flag);
						 flag+=1;
						 break;
				case 'B':sum += 11 * pow(16,flag);
						 flag+=1;
						 break;
				case 'C':sum += 12 * pow(16,flag);
						 flag+=1;
						 break;
				case 'D':sum += 13 * pow(16,flag);
						 flag+=1;
						 break;
				case 'E':sum += 14 * pow(16,flag);
						 flag+=1;
						 break;
				case 'F':sum += 15 * pow(16,flag);
						 flag+=1;
						 break;
			}
		}
		else
		{
//			cout << i << " " << s1[i] << endl;
			sum += ((int)s1[i]-48)*pow(16,flag);
//			cout << "sum: " << sum << endl;
			flag += 1;
		}
	}
	cout << sum << endl;
	return 0;
}

十进制转十六进制

#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
#define long long ll
using namespace std;

int main()
{
	int a , b;
	char c , num[maxn];
	cin >> a;
	int fp = 1, flag = 0;
	if(a == 0)
	{
		cout << 0;
	}
	while(a > 0)
	{
		b = a % 16;
		if(b<=9 && b>=0)
		{
			if(fp)
			{
				c = 0;
				fp = 0;
				continue;	
			}
			c = b + 48;
			num[flag] = c;
			flag += 1;
		}
		else
		{
			switch(b)
			{
				case 10: c = 'A'; break;
				case 11: c = 'B'; break;
				case 12: c = 'C'; break;
				case 13: c = 'D'; break;
				case 14: c = 'E'; break;
				case 15: c = 'F'; break;
			}
			num[flag] = c;
			flag += 1;
		}
		a /= 16;
	}
	for(int i = flag - 1; i >= 0; i--)
	{
		cout << num[i];
	}
	return 0;
}

⑤*特殊回文数 *

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
using namespace std;

int number;

int is_num(int n);

int is_equal(int n);

int main(void)
{
	cin >> number;
	for(int i = 10000; i <= 99999; i++)
	{
		if(is_num(i))
		{
			if(is_equal(i))
			{
				cout << i << endl;
			}
		}
	}
	for(int i = 100000; i <= 999999; i++)
	{
		if(is_num(i))
		{
			if(is_equal(i))
			{
				cout << i << endl;
			}
		}
	}
	return 0;
}

int is_num(int n)
{
	int centre = n , count = 0 , a , b , c , d , e , f;
	while(centre > 0)
	{
		count += 1;
		centre /= 10;
	}
	int flag = count;
	if(count==5)
	{
		a = n / 10000 , b = n / 1000 - a * 10 , c = n / 100 - 100 * a - b * 10 , d = n / 10 - 1000 * a - 100 * b - 10 * c , e = n % 10;
		if(a==e&&b==d)
			return 1;
		return 0;
	}
	if(count==6)
	{
		a = n / 100000 , b = n / 10000 - a * 10 , c = n / 1000 - 100 * a - b * 10 , d = n / 100 - 1000 * a - 100 * b - 10 * c , e = n / 10 - 10000 * a - 1000 * b - 100 * c - 10 * d , f = n % 10;
		if(a==f&&b==e&&c==d)
			return 1;
		return 0;
	}
}

int is_equal(int n)
{
	int centre = n , count = 0 , a , b , c , d , e , f;
	while(centre > 0)
	{
		count += 1;
		centre /= 10;
	}
	if(count==5)
	{
		a = n / 10000 , b = n / 1000 - a * 10 , c = n / 100 - 100 * a - b * 10 , d = n / 10 - 1000 * a - 100 * b - 10 * c , e = n % 10;
		if( (a + b + c + d + e) == number )
			return 1;
	    return 0;
	}
	if(count==6)
	{
		a = n / 100000 , b = n / 10000 - a * 10 , c = n / 1000 - 100 * a - b * 10 , d = n / 100 - 1000 * a - 100 * b - 10 * c , e = n / 10 - 10000 * a - 1000 * b - 100 * c - 10 * d , f = n % 10;
		if( (a + b + c + d + e + f) == number )
			return 1;
	    return 0;
	}
}

回文数

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
using namespace std;
int is_num(int n);
int main(void)
{
	for(int i = 1000; i <= 9999; i++)
	{
		if(is_num(i))
		{
			cout << i << endl;
		}
	}
	return 0;
}

int is_num(int n)
{
	int a , b , c , d;
	a = n / 1000 , b = n / 100 - a * 10 , c = n / 10 - 100 * a - b * 10 , d = n % 10;
	if(a==d&&b==c)
		return 1;
	return 0;
}

特殊的数字

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
#define long long ll
using namespace std;

int is_Daffodils_number(int n);

int main(void)
{
	for(int i = 100; i < 999; i++)
	{
		if(is_Daffodils_number(i))
		{
			cout << i << endl;
		}
	}
	return 0;
}

int is_Daffodils_number(int n)
{
	int a , b , c;
	a = n / 100 , b = n / 10 - a * 10 , c = n % 10;
	if(pow(a,3) + pow(b,3) + pow(c,3) == n)
		return 1;
	return 0;
}

杨辉三角形

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
#define long long ll
using namespace std;

int main(void)
{
	int n , tal[34][34] = {{1},{1,1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1},{1}};
	cin >> n;
	for(int i = 2; i < n; i++)
	{
		for(int j = 1; j < i + 1; j++)
		{
			tal[i][j] = tal[i-1][j] + tal[i-1][j-1];
		}
		tal[i][i] = 1;
	}
	int flag = 1;
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < flag; j++)
		{	
			cout << tal[i][j] << " ";
		}
		cout << endl;
		flag += 1;
	}
	return 0;
}

查找整数

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
#define long long ll
using namespace std;

int main(void)
{
	int n;
	int number[1000];
	int a , flag = 1;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> number[i];
	}
	cin >> a;
	for(int i = 0; i < n; i++)
	{
		if(number[i]==a)
		{
			cout << flag;
			return 0;
		}
		else
		{
			flag += 1;
		}
	}
	cout << -1;
	return 0;
}

数列特征

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
#define long long ll
using namespace std;

int main(void)
{
	int n = 0 , number[10000] , sum = 0;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> number[i];
		sum += number[i];
	}
	sort(number,number+n);
	cout << number[n-1] << endl;
	cout << number[0] << endl;
	cout << sum << endl;
	return 0;
}

11、字母图形

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
using namespace std;

int main(void)
{
	int n , m;
	cin >> n >> m;
	char A[n+m-1];
    int Aindex = n - 1;
    for(int i = Aindex; i >= 0; --i)//填充数组A的0~Aindex-1位置的字符
        A[i]=(char)('A' + Aindex-i);
    for(int i = Aindex; i < n + m - 1; ++i)//填充数组A的Aindex+1~m+n-2位置的字符
        A[i]=(char)('A' + i-Aindex);
    for(int i = 0; i < n; ++i)
	{
        for(int j = 0; j < m; ++j)
            cout << A[Aindex - i + j];
   		cout << endl;
    }
	return 0;
}

12、01字串

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
#define long long ll
using namespace std;

int main(void)
{
    for(int i = 0; i < 32; i++)
	{
        cout << i % 32 / 16 << i % 16 / 8 << i % 8 / 4 << i % 4 / 2 << i % 2 << endl;
    }
	return 0;
}

13、闰年判断

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define setprecision spc
#define long long ll
using namespace std;

int main(void)
{
	int year;
	cin >> year;
	if((year%4==0&&year%100!=0)||(year%400==0))
	{
		cout << "yes" ;
	}
	else
	{
		cout << "no";
	}
	return 0;
}

14、阶乘计算

#include
#include
#include
int main()
{
    int n,i,j,r=0;
    int a[3000];
    memset(a,0,sizeof(a));//给数组赋初值0 
    a[0]=1;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
	{
        for(j=0;j<3000;j++)
		{
            int s = a[j]*i+r;
            r = s/10;
            a[j] = s%10;
        }
    }
    for(i=3000-1;i>=0;i--)
	{
        if(a[i])
            break;
    }
    for(j=i;j>=0;j--)
        printf("%d",a[j]);
    return 0;
}

15、高精度加法

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LOCAL
#define maxn 100005
#define INF 0x3f3f3f3f;
#define PI 3.1415926
#define make_pair mp
#define rep(i,a,n) for(int i = a; i < n; i++)
#define pre(i,a,n) for(int i = a; i >= 0; i--)
#define spc setprecision
#define ll long long
using namespace std;

int main(void)
{
	int a[maxn] , b[maxn] , c[maxn];
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	string s1 , s2;
	cin >> s1 >> s2;
	int flag1 = 0 , flag2 = 0;
	for(int i = s1.length() - 1; i >= 0; i--)
	{
		a[i] = (s1[flag1] - '0');
		flag1 += 1;
	}
	for(int i = s2.length() - 1; i >= 0; i--)
	{
		b[i] = (s2[flag2] - '0');
		flag2 += 1;
	}
	int maxlen = max(s1.length(),s2.length());
	int r = 0;
	for(int i = 0; i < maxlen; i++)
	{
		c[i+1]+=((c[i]+=a[i] + b[i])>=10?c[i]%=10,1:0);
	}
	if(c[maxlen-1]==0)  	cout << 1;
	for(int i = maxlen - 1; i >= 0; i--)
	{
		cout << c[i];
	}
	cout << endl;
	return 0;
}

16、Huffuman树

#include
#include
#include
using namespace std;
int main()
{
	int n, num,ans;
	vector<int> v;
	while (cin >> n)
	{
		ans = 0;
		v.clear();
		while (n--)
		{
			cin >> num;
			v.push_back(num);
		}
		sort(v.begin(), v.end());
		
		while (v.size() > 1)
		{
			ans += v[0] + v[1];
			v.push_back(v[0] + v[1]);
			v.erase(v.begin());
			v.erase(v.begin());
			sort(v.begin(), v.end());
		}
		cout << ans << endl;
	}
    return 0;
}

17、2n皇后问题
关于2n皇后的思路我直接放在了我的搜索专题的blog里面,感兴趣的话可以去看一看:https://blog.csdn.net/qq_42685893/article/details/86664685

在这里插入代码片

18、
这题感觉自己写的有点麻烦,希望大佬们可以提一些优化意见。

#include
#include
using namespace std;
int main()
{
	string arr[24] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
					"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty one", "twenty two", "twenty three"};
	int h, m;
	while(cin >> h >> m && h != EOF && m != EOF)
	{
		cout << arr[h] << " "; //输出小时
		if (m == 0) //如果是整数时间就直接输出 
		{
			cout << "o'clock";
		}
		int t = m % 10, n = m / 10;
		if(m > 0 && m < 20) //1-19比较特殊,单独分出来。 
		{
			cout << arr[m];
		}
		switch(n) //大于20分钟时,输出十位数 
		{
			case 2: cout << "twenty"; break;
			case 3: cout << "thirty"; break;
			case 4: cout << "forty"; break;
			case 5: cout << "fifty"; break;
			default: break;
		}
		if(n >=2 && t != 0) //特殊:大于20分钟时,输出个位数
		{
			cout << " " << arr[t];
		}
	}
	return 0;
}

3. 算法训练
素因子去重

#include
#include
#include
#define maxn 100005
#define ll long long
using namespace std;
ll n ,p = 1;
int a[maxn],flag;
int main(void)
{
	cin >> n;
	for(int i = 2; i <= n; i++)
	{
		int OK = 1;
		if(n % i == 0) //判断i是不是n的因子
		{
			for(int j = 2; j <= sqrt(i); j++) //如果是n的因子,则判断i是否为素数 
			{
				if(i % j == 0)
				{
					OK = 0;
					break;
				}
			}
			if(OK)
			{
				n /= i;
				a[flag++] = i;
				i = 2;
			}
		}
	}
	int flag2 = unique (a , a + flag) - a;
	for(int i = 0; i < flag2; i++)
	{
//		cout << a[i] << endl;
		p *= a[i];
	}
	cout << p;
	return 0;
}

这道题我之前写过一篇题解来详细说明思路和过程,在这就不再多加赘述了。
传送门:https://blog.csdn.net/qq_42685893/article/details/86706833
P0505

#include
#include
#include
#include
using namespace std;

int main()
{
    int n,i,j,r=0;
    int a[3000];
    memset(a,0,sizeof(a));//给数组赋初值0 
    a[0]=1;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
	{
        for(j=0;j<3000;j++)
		{
            int s = a[j]*i+r;
            r = s/10;
            a[j] = s%10;
        }
    }
    for(i=3000-1;i>=0;i--)
	{
        if(a[i])
            break;
    }
    for(j=0;j<=i;j++){ 
    	if(a[j]>0){
    		cout<<a[j]%10;
    		break;
		}
 	}
    return 0;
}

后续内容持续更新,题目的思路后期会补写,欢迎各路大神在评论里多加指正,你们的批评和建议将化为我前进的动力,谢谢!!!

你可能感兴趣的:(蓝桥杯练习题,新手成长记录)