## 弱鸡的第二次线上赛总结(TKK18no.4)

第一题:十六进制

输入一个十进制正整数,输出这个数的十六进制表示,其中十六进制的10-15用大写字母A-F表示。

不断取余,将10-15的数替换成相应大写字母即可。

#include<iostream>
#include<string>
#include<cmath>
#include<bitset>
#include<algorithm>
using namespace std;
void D_TO_X(int x) 
{
	int i, j, arr[16];
	for (i = 0; x != 0; i++)
	{
		arr[i] = x % 16;
		if (arr[i] <= 9)
		{
			arr[i] += '0'; 
		}
		else
		{
			switch (arr[i])
			{
			case 10:
				arr[i] = 'A'; break;
			case 11:
				arr[i] = 'B'; break;
			case 12:
				arr[i] = 'C'; break;
			case 13:
				arr[i] = 'D'; break;
			case 14:
				arr[i] = 'E'; break;
			case 15:
				arr[i] = 'F'; break;
			}
		}
		x /= 16;
	}
	for (j = i - 1; j >= 0; j--)
	{
		cout.put(arr[j]);
	}
	cout << endl;
}

int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		int k;
		cin >> k;
		D_TO_X(k);
	}
	return 0;
}

第二题:画矩形

只有一组案例。先是两个正整数m和n(3<=m,n<=10),然后是一个字符c,最后是一个0或者1的整数d。m代表矩形图案的高,n代表矩形图案的宽,c是用来画矩形的符号,如果d是0则画空心的矩形,如果d是1则画实心的矩形。

比较基础,换成1与0两种情况,逐行判断输出即可。

#include<iostream>
#include<string>
#include<cmath>
#include<bitset>
#include<algorithm>
using namespace std;

int main()
{
	int m, n;
	char k;
	bool x;
	cin >> m >> n >> k >> x;
	if (x)
	{
		for (int i = 0; i < m; i++)
		{
			for (int i = 0; i < n; i++)
			{
				cout << k;
			}
			cout << endl;
		}
	}
	else
	{
		for (int i = 0; i < m; i++)
		{
			if (i == 0 || i == m - 1)
			{
				for (int i = 0; i < n; i++)
				{
					cout << k;
				}
				cout << endl;
			}
			else
			{
				cout << k;
				for (int i = 0; i < n-2; i++)
				{
					cout << " ";
				}
				cout << k << endl;
			}
		}
	}
	return 0;
}

第三题:从前有个函数

从前有个数学函数f(x),功能是计算x的因数的个数,例如f(8)=4。(因为8的因数有1、2、4、8)后来有人觉得算一次f函数不过瘾,又嵌套了一层,于是变成了f(f(x)),那么f(f(8))=f(4)=3。随后,就有了更多层的嵌套函数f。人们发现这样写太费事了,于是把嵌套层数当成了f函数的第二个参数,例如f(f(8))写成了f(8,2),f(9)写成了f(9,1)。现在,需要计算f(a,b)=?

这里题目输入的是十亿以内的数,可见要节约时间。于是判素后直接取2,在sqrt下*2,完全平方数再加一。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<string>
using namespace std;
bool f(int m)
{
	for (int i = 2; i <= sqrt(m); i++)
	{
		if (m%i == 0)return 0;
	}
	if (m>1)return 1;
	else return 0;
}

int main()
{
	int n, m, p;
	cin >> n;
	while (n--)
	{
		cin >> m >> p;
		while (p--)
		{
			if (f(m))
			{
				m = 2; break;
			}
			if (m == 1)break;
			int sum = 0;
			for (int i = 1; i <= sqrt(m); i++)
			{
				if (i*i == m)
					sum++;
				else if (m%i == 0)
					sum += 2;
			}
			m = sum;
		}
		cout << m << endl;
	}
}

第四题:五毒教主吃苹果

有一天,邪恶的巫师送给了五毒教主一筐苹果,里面有一些正常的苹果和一些有毒的苹果。凡人吃毒苹果轻则吐血,重则暴毙,然而五毒教主吃一个毒苹果后只会昏睡一天,并无大碍。昏睡的那天五,毒教主不吃苹果。例如,第1天吃了一个毒苹果,则第2天昏睡不吃,第3天继续吃。
恰逢五毒教主最近减肥,每天最多只吃1个苹果,直到吃完这一筐苹果为止。
问:有多少种不同的吃法?(如果有一个正常的苹果和一个毒苹果,那么先吃正常苹果再吃毒苹果,和先吃毒苹果再吃正常苹果,属于2种不同的吃法。)

看起来很吓人,实际上就是递归。普通的苹果和毒苹果就是两种东西,日期无所谓。

#include<iostream>
#include<string>
#include<cmath>
#include<bitset>
#include<algorithm>
using namespace std;
int t(int a, int b)
{
	if (a == 0 && b == 0)return 1;
	else if (b == 0)return 1;
	else if (a == 0)return 1;
	else return t(a - 1, b) + t(a, b - 1);
}
int main()
{
	int f;
	cin >> f;
	while (f--)
	{
		int m, n;
		cin >> m >> n;
		cout << t(m, n) << endl;
	}
	return 0;
}       

第五题:算出18

有3个非负的整数,想看看是否能够通过加减乘除运算,使得答案为18。
每个数字必须都用上,且仅能各使用一次。不要求按照顺序使用,可以添加上适当的括号以改变计算顺序。
注意:除法是按照数学上的运算结果。

这道题有两种做法,一种是暴力,一种还是递归。要考虑到0的情况和是否用到所有数,是否有浮点数误差,还有数的顺序可以替换问题。

#include<iostream>
#include<string>
#include<cmath>
#include<bitset>
#include<algorithm>
using namespace std;

int main()
{
	int f;
	cin >> f;
	while (f--)
	{
		double x1, x2, x3;
		cin >> x1 >> x2 >> x3;
		double a = x1; double b = x2; double c = x3;
		int d = 0;
		if (a + b + c == 18)d = 1;
		else if (a + b - c == 18)d = 1;
		else if (a + b * c == 18)d = 1;
		else if (c!=0&&a + b / c == 18)d = 1;
		else if ((a + b) * c == 18)d = 1;
		else if (c != 0 && (a + b) / c == 18)d = 1;
		else if (a - b + c == 18)d = 1;
		else if (a - b - c == 18)d = 1;
		else if (a - b * c == 18)d = 1;
		else if (c != 0 && a - b / c == 18)d = 1;
		else if ((a - b) * c == 18)d = 1;
		else if (c != 0 && (a - b) / c == 18)d = 1;
		else if (a * b + c == 18)d = 1;
		else if (a * b - c == 18)d = 1;
		else if (a * b * c == 18)d = 1;
		else if (c != 0 && a * b / c == 18)d = 1;
		else if (a * (b + c) == 18)d = 1;
		else if (a * (b - c) == 18)d = 1;
		else if (a / b + c == 18)d = 1;
		else if (a / b - c == 18)d = 1;
		else if (a / b * c == 18)d = 1;
		else if (c != 0 &&b!=0&& a / b / c == 18)d = 1;
		else if (b+c!=0&&a / (b + c) == 18)d = 1;
		else if (b-c!=0&&a / (b - c) == 18)d = 1;
		 a = x2;  b = x1;  c = x3;
		 if (a + b + c == 18)d = 1;
		 else if (a + b - c == 18)d = 1;
		 else if (a + b * c == 18)d = 1;
		 else if (c != 0 && a + b / c == 18)d = 1;
		 else if ((a + b) * c == 18)d = 1;
		 else if (c != 0 && (a + b) / c == 18)d = 1;
		 else if (a - b + c == 18)d = 1;
		 else if (a - b - c == 18)d = 1;
		 else if (a - b * c == 18)d = 1;
		 else if (c != 0 && a - b / c == 18)d = 1;
		 else if ((a - b) * c == 18)d = 1;
		 else if (c != 0 && (a - b) / c == 18)d = 1;
		 else if (a * b + c == 18)d = 1;
		 else if (a * b - c == 18)d = 1;
		 else if (a * b * c == 18)d = 1;
		 else if (c != 0 && a * b / c == 18)d = 1;
		 else if (a * (b + c) == 18)d = 1;
		 else if (a * (b - c) == 18)d = 1;
		 else if (a / b + c == 18)d = 1;
		 else if (a / b - c == 18)d = 1;
		 else if (a / b * c == 18)d = 1;
		 else if (c != 0 && b != 0 && a / b / c == 18)d = 1;
		 else if (b + c != 0 && a / (b + c) == 18)d = 1;
		 else if (b - c != 0 && a / (b - c) == 18)d = 1;
		 a = x1; b = x3; c = x2;
		 if (a + b + c == 18)d = 1;
		 else if (a + b - c == 18)d = 1;
		 else if (a + b * c == 18)d = 1;
		 else if (c != 0 && a + b / c == 18)d = 1;
		 else if ((a + b) * c == 18)d = 1;
		 else if (c != 0 && (a + b) / c == 18)d = 1;
		 else if (a - b + c == 18)d = 1;
		 else if (a - b - c == 18)d = 1;
		 else if (a - b * c == 18)d = 1;
		 else if (c != 0 && a - b / c == 18)d = 1;
		 else if ((a - b) * c == 18)d = 1;
		 else if (c != 0 && (a - b) / c == 18)d = 1;
		 else if (a * b + c == 18)d = 1;
		 else if (a * b - c == 18)d = 1;
		 else if (a * b * c == 18)d = 1;
		 else if (c != 0 && a * b / c == 18)d = 1;
		 else if (a * (b + c) == 18)d = 1;
		 else if (a * (b - c) == 18)d = 1;
		 else if (a / b + c == 18)d = 1;
		 else if (a / b - c == 18)d = 1;
		 else if (a / b * c == 18)d = 1;
		 else if (c != 0 && b != 0 && a / b / c == 18)d = 1;
		 else if (b + c != 0 && a / (b + c) == 18)d = 1;
		 else if (b - c != 0 && a / (b - c) == 18)d = 1;
		 a = x2; b = x3; c = x1;
		 if (a + b + c == 18)d = 1;
		 else if (a + b - c == 18)d = 1;
		 else if (a + b * c == 18)d = 1;
		 else if (c != 0 && a + b / c == 18)d = 1;
		 else if ((a + b) * c == 18)d = 1;
		 else if (c != 0 && (a + b) / c == 18)d = 1;
		 else if (a - b + c == 18)d = 1;
		 else if (a - b - c == 18)d = 1;
		 else if (a - b * c == 18)d = 1;
		 else if (c != 0 && a - b / c == 18)d = 1;
		 else if ((a - b) * c == 18)d = 1;
		 else if (c != 0 && (a - b) / c == 18)d = 1;
		 else if (a * b + c == 18)d = 1;
		 else if (a * b - c == 18)d = 1;
		 else if (a * b * c == 18)d = 1;
		 else if (c != 0 && a * b / c == 18)d = 1;
		 else if (a * (b + c) == 18)d = 1;
		 else if (a * (b - c) == 18)d = 1;
		 else if (a / b + c == 18)d = 1;
		 else if (a / b - c == 18)d = 1;
		 else if (a / b * c == 18)d = 1;
		 else if (c != 0 && b != 0 && a / b / c == 18)d = 1;
		 else if (b + c != 0 && a / (b + c) == 18)d = 1;
		 else if (b - c != 0 && a / (b - c) == 18)d = 1;
	        a = x3; b = x1; c = x2;
		if (a + b + c == 18)d = 1;
		else if (a + b - c == 18)d = 1;
		else if (a + b * c == 18)d = 1;
		else if (c != 0 && a + b / c == 18)d = 1;
		else if ((a + b) * c == 18)d = 1;
		else if (c != 0 && (a + b) / c == 18)d = 1;
		else if (a - b + c == 18)d = 1;
		else if (a - b - c == 18)d = 1;
		else if (a - b * c == 18)d = 1;
		else if (c != 0 && a - b / c == 18)d = 1;
		else if ((a - b) * c == 18)d = 1;
		else if (c != 0 && (a - b) / c == 18)d = 1;
		else if (a * b + c == 18)d = 1;
		else if (a * b - c == 18)d = 1;
		else if (a * b * c == 18)d = 1;
		else if (c != 0 && a * b / c == 18)d = 1;
		else if (a * (b + c) == 18)d = 1;
		else if (a * (b - c) == 18)d = 1;
		else if (a / b + c == 18)d = 1;
		else if (a / b - c == 18)d = 1;
		else if (a / b * c == 18)d = 1;
		else if (c != 0 && b != 0 && a / b / c == 18)d = 1;
		else if (b + c != 0 && a / (b + c) == 18)d = 1;
		else if (b - c != 0 && a / (b - c) == 18)d = 1;
		a = x3; b = x2; c = x1;
		if (a + b + c == 18)d = 1;
		else if (a + b - c == 18)d = 1;
		else if (a + b * c == 18)d = 1;
		else if (c != 0 && a + b / c == 18)d = 1;
		else if ((a + b) * c == 18)d = 1;
		else if (c != 0 && (a + b) / c == 18)d = 1;
		else if (a - b + c == 18)d = 1;
		else if (a - b - c == 18)d = 1;
		else if (a - b * c == 18)d = 1;
		else if (c != 0 && a - b / c == 18)d = 1;
		else if ((a - b) * c == 18)d = 1;
		else if (c != 0 && (a - b) / c == 18)d = 1;
		else if (a * b + c == 18)d = 1;
		else if (a * b - c == 18)d = 1;
		else if (a * b * c == 18)d = 1;
		else if (c != 0 && a * b / c == 18)d = 1;
		else if (a * (b + c) == 18)d = 1;
		else if (a * (b - c) == 18)d = 1;
		else if (a / b + c == 18)d = 1;
		else if (a / b - c == 18)d = 1;
		else if (a / b * c == 18)d = 1;
		else if (c != 0 && b != 0 && a / b / c == 18)d = 1;
		else if (b + c != 0 && a / (b + c) == 18)d = 1;
		else if (b - c != 0 && a / (b - c) == 18)d = 1;
		if (d == 1)cout << "Yes";
		else cout << "No";
		cout << endl;
	}
	return 0;
}

这是我的第一种做法,很蠢但至少过了。

#include<iostream>
#include<cmath>
using namespace std;
bool isSame(double a, double b)
{
	if (abs(a - b) <= 1e-7)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool can2(double result, int first, int second)
{
	if (first + second == result
		|| first - second == result
		|| second - first == result
		|| first * second == result
		|| second != 0 && isSame(1.0 * first / second, result)
		|| first != 0 && isSame(1.0 * second / first, result))
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool can31(double result, int first, int second, int third)
{
	if (can2(result - first, second, third)
		|| can2(first - result, second, third)
		|| can2(result + first, second, third)
		|| (result == 0 && first == 0 || first != 0) && can2(result*first, second, third)
		|| first != 0 && can2(result / first, second, third)
		|| result != 0 && first != 0 && can2(first / result, second, third))
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool can3(double result, int first, int second, int third)
{
	if (can31(result, first, second, third)
		|| can31(result, second, first, third)
		|| can31(result, third, first, second))
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	const double result = 18;
	int n;
	cin >> n;
	while (n--)
	{
		int a, b, c;
		cin >> a >> b >> c;
		if (can3(result, a, b, c))
		{
			cout << "Yes" << endl;
		}
		else
		{
			cout << "No" << endl;
		}
	}
	return 0;
}

这是递归的做法,比较高难。

第六题:字符阵列

输出m行m列由字符组成的阵列。其特点是按照字母表顺序蛇形走位,第一行是顺序,第二行是倒序,…。另外,当用到字符Z后,下一个字符是A。

举个栗子,就是3的话,会变成
ABC
FED
GHI
这个按坐标算出来在自己所在的字符串第几个,再输出ascii对应的字母就行。

#include<iostream>
#include<string>
#include<cmath>
#include<bitset>
#include<algorithm>
using namespace std;

int main()
{
	int f;
	cin >> f;
	while (f--)
	{
		int m;
		cin >> m;
		for (int i = 1; i <= m; i++)
		{
			for (int p = 1; p <= m; p++)
			{
				if (i % 2 == 1)
				{
					int x = p + m*(i - 1);
					char n = (x - 1) % 26 + 65;
					cout << n;
				}
				else
				{
					int x = (m - p + 1) + m*(i - 1);
					char n = (x - 1) % 26 + 65;
					cout << n;
				}
			}
			cout << endl;
		}
	}
	return 0;
}
阅读更多

没有更多推荐了,返回首页