水题(2)找规律

目录

CSU 1150: 食用油

CSU 1263: 最少钱币数

CSU 1271: Brackets Sequence(括号匹配问题)

CSU 1284: Cutting Cake

CSU 1363: Count 101


CSU 1150: 食用油

题目:

Description

        从前,有一个远得要命的小岛上住着一位打油师。每天有很多顾客去那里购买安全无害的食用油,但打油师是一个吝啬鬼,他从来不会用自己的油桶给顾客打油,因为这样会弄脏了他的油桶,所以顾客都需要带上自己的油桶前往远得要命的小岛上购买一定量的食用油C升。但并不是每个顾客都有刚好为C升的油桶,因此很多顾客都会带着两只容量分别为A,B的油桶赶往远得要命的小岛,因为他们知道,打油师会想办法弄到他们想要的C升食用油,毕竟吝啬鬼不会让上门的生意溜掉。

        打油师现在麻烦了,他必须用顾客带来的容量为A,B的桶,量出所需的C升油出来,这是一项相当庞大的工程,他需要尽量减少自己的操作次数来节约时间,以此来处理大量顾客的需求。

他可以对容量为A,B的油桶进行如下操作:

1. 从仓库中向A添加食用油至A容量已满

2. 从仓库中向B添加食用油至B容量已满

3. 把A桶的食用油倒入B中(不超过B的容量;B可能未满;A可能有剩余)

4. 把B桶的食用油倒入A中(不超过A的容量;A可能未满;B可能有剩余)

5. 把A中的油全部到会仓库

6. 把B中的油全部到会仓库

打油师每次使用以上的任一种操作都要算一次操作次数。

Input

 多组测试数据,每一行为3个整数:A, B, C。所表示的意思如题意 1<=A,B<=100;C<=max(A,B)

Output

 计算最少的操作次数并输出结果,然后输出换行

Sample Input

3 5 4

Sample Output

6

题目的数据其实是保证gcd(a,b)整除c的

思路:把c表示成ax+by的形式,xy一正一负,有2种情况,所以要调用2次f,取较小值

f里面又要分3种情况,c=a,ca

代码:

#include
#include
using namespace std;
 
int f(int a, int b, int c)
{
	if (a == c)return 1;
	int r = 0;
	while ((b*r + c) % a)r++;
	return ((b*r + c) / a + r - (c < a)) * 2;
}
 
int main()
{
	int a, b, c, rr;
	while (cin >> a >> b >> c)cout << min(f(a, b, c), f(b, a, c)) << endl;
	return 0;
}

CSU 1263: 最少钱币数

题目:

Description

作为A公司的职员,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了。但是对于公司财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡最近就在考虑一个问题:如果每个员工的工资额都知道,最少需要准备多少张人民币,才能在给每位职员发工资的时候都不用老师找零呢?这里假设员工的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。

Input

输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资(工资<5000)。

Output

每个测试用例输出一行,即凑成钱数值M最少需要的钱币个数。如果凑钱失败,输出“Impossible”。你可以假设,每种待凑钱币的数量是无限多的。

Sample Input

3
1 2 3
2
1 2

Sample Output

4
2

代码:

#include
using namespace std;
 
int main()
{
	int n, a;
	while (cin >> n)
	{
		int ans = 0;
		while (n--)
		{
			cin >> a;
			ans += a / 100, a %= 100;
			ans += a / 50, a %= 50;
			ans += a / 10, a %= 10;
			ans += a / 5, a %= 5;
			if (a)ans++;
			if (a > 2)ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

CSU 1271: Brackets Sequence(括号匹配问题)

题目:

Description

Let us define a regular brackets sequence in the following way:

1. Empty sequence is a regular sequence.

2. If S is a regular sequence, then (S) is a regular sequence.

3. If A and B are regular sequences, then AB is a regular sequence.

For example, these sequences of characters are regular brackets sequences: (), (()), ()(), ()(()), ((())())().

And all the following character sequences are not: (, ), ((), ()), ())(, (()(, ()))().

A sequence of characters '(' and ')' is given. You can insert only one '(' or ')' into the left of the sequence, the right of the sequence, or the place between any two adjacent characters, to try changing this sequence to a regular brackets sequence.

Input

The first line has a integer T (1 <= T <= 200), means there are T test cases in total.

For each test case, there is a sequence of characters '(' and ')' in one line. The length of the sequence is in range [1, 105].

Output

For each test case, print how many places there are, into which you insert a '(' or ')', can change the sequence to a regular brackets sequence.

What's more, you can assume there has at least one such place.

Sample Input

4
)
())
(()(())
((())())(()

Sample Output

1
3
7
3

题意:

给定一个括号串,只需插入1个括号即可全部匹配

求有多少个地方可以插入1个括号使得全部匹配成功

思路:

首先,如何判断一个括号串是否完全匹配?

用数组num记录,num[i]表示前i个字符中,)和(的数量差

如果num[i]全部不超过0,而且最后一项为0,那么就可以完全匹配,反之亦然。

所以本题的答案是:

如果num[i]中有出现1,那么第一个1的位置就是答案

否则,最后一个0的位置与括号串长度之差就是答案

代码:

#include
#include
using namespace std;
 
char ch[100005];
int num[100005];//前i个字符中)和(的数量差
 
int main()
{
	int T, len;
	cin >> T;	
	while (T--)
	{
		cin >> ch + 1;
		len = strlen(ch+1);
		num[0] = 0;
		int i, k0 = 0;
		for (i = 1; i <= len; i++)
		{
			if (ch[i] == '(')num[i] = num[i - 1] - 1;
			else num[i] = num[i - 1] + 1;
			if (num[i] > 0)
			{
				cout << i << endl;
				break;
			}
			if (num[i] == 0)k0 = i;
		}
		if (i > len)cout << len - k0 << endl;
	}
	return 0;
}

CSU 1284: Cutting Cake

题目:

Description

一个蛋糕切N刀,最多能得到多少块?切的过程中不能改变任意一块蛋糕的位置。

Input

输入数据的第一行包含一个整数T (1 <= T <= 100),表示接下来一共有T组测试数据。

每组测试数据占一行,包含一个整数N (1 <= N <= 100),含义同上。

Output

用一行输出一个整数,表示上述问题的答案。

Sample Input

3
2
3
4

Sample Output

4
8
15

代码:

#include
using namespace std;
 
int main()
{
	int t, n;
	cin >> t;
	while (t--)
	{
		cin >> n;
		cout << (n*n - 1)*n / 6 + n + 1 << endl;
	}
	return 0;
}

CSU 1346: 变色龙

题目:

Description

    在一个美丽的小岛上住着一群变色龙:其中有X只变色龙是红色的,Y只变色龙是绿色的,Z只变色龙是蓝色的。
    每个时刻会有两只不同颜色的变色龙相遇,相遇后他们会同时变成第三种颜色。比如,如果一只红色的变色龙和一只蓝色的变色龙相遇了,他们就会同时变成绿色的变色龙,如果一只绿色的变色龙和一只蓝色的变色龙相遇了,他们就会同时变成红色的变色龙,等等。
    那么最后是否有可能所有的变色龙都是同一种颜色呢?

Input

    输入的第一行包含一个整数T (1 <= T <= 100),表示接下来一共有T组测试数据。
    每组数据占一行,包含三个整数XYZ (1 <= XYZ <= 109),含义同上。

Output

    对于每组测试数据,如果最后有可能所有的变色龙都是同一种颜色,用一行输出“Yes”(不包括引号),否则输出“No”(不包括引号)。

Sample Input

4
1 1 1
1 2 3
7 1 2
3 7 5

Sample Output

Yes
No
Yes
No

代码:

#include
using namespace std;
 
int main()
{
	int t, x, y, z;
	cin >> t;
	while (t--)
	{
		cin >> x >> y >> z;
		if (x % 3 == y % 3 || x % 3 == z % 3 || y % 3 == z % 3)cout << "Yes\n";
		else cout << "No\n";
	}
	return 0;
}

CSU 1363: Count 101

题目:

Description

You know YaoYao is fond of his chains. He has a lot of chains and each chain has n diamonds on it. There are two kinds of diamonds, labeled 0 and 1. We can write down the label of diamonds on a chain. So each chain can be written as a sequence consisting of 0 and 1.
We know that chains are different with each other. And their length is exactly n. And what’s more, each chain sequence doesn’t contain “101” as a substring. 
Could you tell how many chains will YaoYao have at most?

Input

There will be multiple test cases in a test data. For each test case, there is only one number n(n<10000). The end of the input is indicated by a -1, which should not be processed as a case.

Output

For each test case, only one line with a number indicating the total number of chains YaoYao can have at most of length n. The answer should be print after module 9997.

Sample Input

3
4
-1

Sample Output

7
12

代码:

#include
using namespace std;
 
int main()
{
	int ans[10001], n;
	ans[0] = 1, ans[1] = 2, ans[2] = 4;
	for (int i = 3; i <= 10000; i++)ans[i] = (ans[i - 1] * 2 - ans[i - 2] + ans[i - 3]) % 9997;
	while (cin >> n)if (n + 1)cout << ans[n] << endl;
	return 0;
}

 

你可能感兴趣的:(new)