水题(4)模拟题

目录

CSU 1003: UC Browser

CSU 1006: SAW(电锯惊魂)

CSU 1008: Horcrux

CSU 1019: Simple Line Editor

CSU 1053: The Least Palindromic Number(最小回文数)

CSU 1112: 机器人的指令

CSU 1161: Sums

CSU 1202: 剪刀石头布

CSU 1283: Binary Tree Traversals

CSU 1341: String and Arrays

CSU 1585: 刻苦练习CCF

CSU 1600: Twenty-four point (24点)

HDU 1427 24点游戏

CSU 1777 NBUT 1639 大还是小?

CSU 2043: 身份证号

CSU 2142: 独特的骰子

CodeForces 1B Spreadsheets(EXCEL)

CodeForces 471B MUH and Important Things

CodeForces 616A Comparing Two Long Integers

CodeForces 685A 686C Robbers' watch

CodeForces 691C Exponential notation

CodeForces 697B Barnicle (科学计数法转化成普通数)


CSU 1003: UC Browser

题目:

Description

Brother Xi has recently bought a smart mobile phone. Now he surfs Internet by his mobile phone almost every day. The browser that he uses is UC Browser, which is one of the most popular mobile browsers. To better grasp the users, UC Corporation have also brought out the level system of user account. The higher the level of your account, the more privileges you can enjoy. The level of your account is calculated by your experiences. The correspondence of level and experience is as follows:

Level

Experiences

Level

Experiences

Level

Experiences

0

0-49

3

250-349

6

550-649

1

50-149

4

350-449

7

650-749

2

150-249

5

450-549

8

>=750

You can get 10 experiences after using UC Browser one day in a row, 20 experiences for two days in a row, 30 experiences for three days in a row, 40 experiences for four days in a row, 50 experiences for five days in a row. If you use UC Browser six days in a row, the experiences you can get will be equal 10, like your using UC Browser one day in a row. It’s come back to the starting of your using UC Browser. It’s a circle.

Now you have known the Xi’s record of using UC Browser, I’ll hope you calculate the level of Xi’s account.

Input

The first line of the input contains a single integer T (0<T<120) which is the number of test cases, followed by 2*T lines. For each test case, the first line is the number of days n (0<n<=100), the second line is a series of 0 and 1. 0 stands for not using UC browser on that day and 1 stands for using UC browser on that day.

Output

For each test case, output the corresponding level of Xi’s account in one line.

Sample Input

2611010112111111110101

Sample Output

12

代码:

#include<iostream>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    int a;
    for(int i = 0; i < t; i++)
    {
        cin >> a;
        char*ch = new char[a];
        cin >> ch;
        int j = 0;
        int score = 0;
        for(; j < a; )
        {
            int number = 0;     
            while(ch[j] == '0'&&j < a)j++;
            while(ch[j] == '1'&&j < a)
            {
                number++;
                j++;
                int num = number % 5;
                int point = 50;
                if(num)point = 10 * num;
                score += point;
            }
        }
        if(score >= 750)cout << 8;
        else if (score < 50)cout << 0;
        else cout << (score + 50) / 100;
        cout << endl;
    }
    return 0;
}

CSU 1006: SAW(电锯惊魂)

题目:

I want to play a game.

Up until now, you've simply sat in front of computers watching others cheer to solve their problems. Now I see you as a strange mix of someone apathetic. But mostly just pathetic. Cause you haven't solved all these problems, at least not this one. So are you going to watch yourself end your warm-up contest here today, or do something about it?

These are the rules. I have nothing in the input data. But you, you come up with a capital letter in your mind. Yes, that’s the very letter I want you to output.

Input

Nothing to input. But congratulations. You still have time.

Most people are so ungrateful to get ACs. But not you. Not anymore.

Output

Know that I'm not lying. Better hurry up. Output the capital letter in your mind.

Make your choice.

Sample Input

(nothing)

Sample Output

(a letter stated above)

代码:

#include<iostream>
using namespace std;
 
int main()
{
	cout << "B";
	return 0;
}

这个题目要看运气,标程是每次产生一个随机字母,要是运气好的话碰巧字母一样才能AC

CSU 1008: Horcrux

题目:

Description

A Horcrux is an object in which a Dark wizard or witch has hidden a fragment of his or her soul for the purpose of attaining immortality. Constructing a Horcrux is considered Dark magic of the foulest, most evil kind, as it violates laws of nature and morality, and requires a horrific act (a.k.a. murder) to accomplish.

There are two kinds of horcruxes, the white one, denoted as , and the black one, denoted as . Topper has got N horcruxes, and he wants to destroy them to win the Dark wizard. Toper places all the horcruxes in a line from left to right, one by one, and then says a magic spell to destroy them. In order to make the magic spell works, Toper needs to know the number of the white horcruxes.

Since the horcruxes also has magic, when placing the horcruxes, they will change color from white to black or from black to white under the following rules:

  1. When Topper places the i-th horcrux and i is an even number: If the i-th horcrux and the rightmost horcrux have different colors, all consecutive horcruxes of the same color on the right change its color.

  2. In other situations, no magic works.

For example, suppose the horcruxes on the line are:

△△▲▲△△△

After placing 7 horcruxes.

If the 8-th horcrux is white, since its color and the color of the rightmost horcrux are the same. Therefore, the horcruxes on the line become as follows:

△△▲▲△△△△

If the 8-th horcrux is black, since its color and the color of the rightmost horcrux are different, the 3 consecutive white stones on the right change their color. Therefore, the stones on the line become as follows:

△△▲▲▲▲▲▲

You see, it’s not an easy job to beat the Dark wizard. So write a program to help Topper.

Input

There are some test cases. In each test case, the first line contains a positive integer n (1≤n≤100,000), which is the number of horcruxes. The following n lines denote the sequence in which Topper places the horcruxes. 0 stands for white horcrux, and 1 stands for black horcrux.

Output

For each test case, output one line containing only the number of white horcruxes on the line after Topper places n horcruxes.

Sample Input

8
1
0
1
1
0
0
0
0
8
1
0
1
1
0
0
0
1

Sample Output

6
2

代码:

#include<iostream>
#include<stdio.h>
using namespace std;
 
int col[100005], num[100005];
 
int main()
{
	int n, key;
	while (scanf("%d", &n)!=EOF)
	{
		key = 1;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &col[key]);
			num[key] = 1;
			if (i % 2 == 0)
			{
				if (col[key - 1] == col[key])num[key - 1]++;
				else
				{
					col[key - 1] = col[key];
					num[key - 1]++;
					if (key > 2)num[key - 2] += num[key - 1], key--;
				}
			}
			else if (key>1 && col[key - 1] == col[key])num[key - 1]++;
			else key++;
		}
		int s = 0;
		for (int i = 1; i < key; i++)if (col[i] == 0)s += num[i];
		printf("%d\n", s);
	}
	return 0;
}

CSU 1019: Simple Line Editor

题目:

Description

Early computer used line editor, which allowed text to be created and changed only within one line at a time. However, in line editor programs, typing, editing, and document display do not occur simultaneously (unlike the modern text editor like Microsoft Word). Typically, typing does not enter text directly into the document. Instead, users modify the document text by entering simple commands on a text-only terminal. 

Here is an example of a simple line editor which can only process English. In addition, it has two commands. ‘@’ and ‘#’. ‘#’ means to cancel the previous letter, and ‘@’ is a command which invalidates all the letters typed before. That is to say, if you want type “aa”, but have mistakenly entered “ab”, then you should enter ‘#a’ or ‘@aa&rsquo; to correct it. Note that if there is no letter in the current document, ‘@’ or ‘#’ command will do nothing.

Input

The first line contains an integer T, which is the number of test cases. Each test case is a typing sequence of a line editor, which contains only lower case letters, ‘@’ and ‘#’.

there are no more than 1000 letters for each test case.

Output

For each test case, print one line which represents the final document of the user. There would be no empty line in the test data.

Sample Input

2
ab#a
ab@aa

Sample Output

aa
aa

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int t, key;
	cin >> t;
	char ch[1024], ans[1024];
	while (t--)
	{
		cin >> ch;
		key = 0;
		for (int i = 0; ch[i]; i++)
		{
			if (ch[i] == '#')
			{
				if (key)key--;
			}
			else if (ch[i] == '@')key = 0;
			else ans[key++] = ch[i];
		}
		ans[key] = '\0';
		cout << ans << endl;
	}	
	return 0;
}

CSU 1053: The Least Palindromic Number(最小回文数)

题目:

Description

    Palindromic numbers are digital strings that read the same both forwards and backwards. For example, 121, 44 and 3 are Palindromic numbers, 175, 36 are not;

    For a given integer N, you are to find a palindromic number P that satisfy P>N. However, there are many such palindromic numbers. Your task is to find the least one.

Input

    There are several test cases, each test case contains only one positive integer N in one line. The number of digits of N is not exceeding 10,000, and N has not lead zeroes.
    The input will finish with the end of file.

Output

    For each the case, your program will output the least palindromic number P (P > N) on separate line.

Sample Input

44
3
175

Sample Output

55
4
181

题意:

输入一个数n,输出大于它的最小回文数

思路:

(1)首先看n是不是每一位都是9,如果是的话,答案就是n+2(比如大于999的最小回文数就是1001),如果不是就继续讨论

讨论n是不是回文数

(2)如果n不是回文数的话,把n的右边一半变成和左边一半对应一样的字符,,得到m

例如1234变成1221,n=1234,m=1221

比较m和n的大小关系,如果m>n那么m就是答案

如果m<n那么大于n的最小回文数和大于m的最小回文数是一样的,问题就转化成n是回文数的情况

(3)如果n是回文数的话

首先把最靠近中间而且不是9的2个数(或1个数)加1,然后把这2个数中间所有的数全部变成0,得到的就是答案

代码:

#include<iostream>
#include<string.h>
using namespace std;
 
int main()
{	
	char s[10005];
	while (cin >> s)
	{
		int len = strlen(s);
		bool flag = true;
		for (int i = 0; i < len; i++)if (s[i] != '9')flag = false;
		if (flag)
		{
			cout << 1;
			for (int i = 1; i < len; i++)cout << 0;
			cout << 1 << endl;
			continue;
		}
		flag = true;
		for (int i = 0, j = len - 1; i < j; i++, j--)if (s[i] != s[j])flag = false;
		if (!flag)
		{
			bool flag2 = true;
			for (int i = (len - 1) / 2, j = len / 2; i >= 0; i--, j++)
			{
				if (flag2 && s[i] != s[j])
				{
					flag2 = false;
					if (s[i] < s[j])flag = true;
				}
				s[j] = s[i];
			}
		}
		if(flag)for (int i = (len - 1) / 2, j = len / 2; i >= 0; i--, j++)
		{
			if (s[i] == '9')continue;
			s[i]++;
			if (i != j)s[j]++;
			for (int k = i + 1; k < j; k++)s[k] = '0';
			break;
		}		
		for (int i = 0; i < len; i++)cout << s[i];
		cout << endl;
	}
	return 0;
}

CSU 1112: 机器人的指令

题目:

Description

数轴原点有一个机器人。该机器人将执行一系列指令,你的任务是预测所有指令执行完毕之后它的位置。

·LEFT:往左移动一个单位

·RIGHT: 往右移动一个单位

·SAME AS i: 和第i 条执行相同的动作。输入保证i 是一个正整数,且不超过之前执行指令数

Input

输入第一行为数据组数T (T<=100)。每组数据第一行为整数n (1<=n<=100),即指令条数。以下每行一条指令。指令按照输入顺序编号为1~n。

Output

对于每组数据,输出机器人的最终位置。每处理完一组数据,机器人应复位到数轴原点。

Sample Input

2
3
LEFT
RIGHT
SAME AS 2
5
LEFT
SAME AS 1
SAME AS 2
SAME AS 1
SAME AS 4

Sample Output

1
-5

主要就是字符串处理的问题。

代码:

#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
 
int main() 
{
    int n;
    cin >> n;
    int m;
    string c;
    while (n--)
    {
        cin >> m;
        int *list = new int[m];
        int sum = 0;
        for (int i = 0; i < m; i++)
        {
            cin>>c;
            if (c[0] == 'L')list[i] = -1;
            else if (c[0] == 'R')list[i] = 1;
            else
            {
                cin>>c;
                cin>>c;
                int t;
                if (c.length() == 1)t = c[0] - '0';
                else  t = (c[0] - '0') * 10 + ( c[1] - '0');
                 
                list[i] = list[t-1];
            }
            sum += list[i];
        }
        cout << sum << endl;
    }
    return 0;
}

CSU 1161: Sums

题目:

Description

Sometimes Ziwen need to operate even larger numbers. A limit of 1000 digits is so small… You have to find the sum of two numbers with maximal size of 1 000 000 digits.

Input

The first line contains a single integer N that is the length of the given integers(1 ≤ N ≤ 1 000 000). It is followed by these integers written in columns. That is, the next N lines contain two digits each, divided by a space. Each of the two given integers is not less than 0, and the length of their sum does not exceed N. The integers may contain leading zeroes.

Output

Output exactly N digits in a single line representing the sum of these two integers.

Sample Input

4 0 4 4 2 6 8 3 7

Sample Output

4750

代码:

#include<iostream>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    int *list=new int[n];
    int a;
    for(int i=0;i<n;i++)
    {
        cin>>a;
        list[i]=a;
        cin>>a;
        list[i]+=a;
    }
    for(int i=n-1;i>0;i--)
    {
        if(list[i]>9)
        {
            list[i]-=10;
            list[i-1]+=1;
        }
    }
    list[0] %=10;
    for(int i=0;i<n;i++)printf("%d",list[i]);
    return 0;
}

CSU 1202: 剪刀石头布

题目:

Description

现在一共有N个人(分别记为1, 2, …, N)在玩剪刀石头布,如果知道他们每个人都出了什么,你能找出来谁是winner吗?

当且仅当一个人可以赢其他所有人时,才称这个人是winner。

我们将剪刀记作2,石头记作0,布记作5,那么胜负关系就应当是2能赢5,5能赢0,0能赢2。

Input

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

每组测试数据的第一行包含一个整数N (2 <= N <= 100000)表示一共有N个人在玩剪刀石头布,接下来一行一共有N个数,每个数均为0、2或5中的某一个,依次描述了这N个人分别出了什么,其中第i个整数描述了第i个人出了什么。

Output

对于每组数据,用一行输出一个整数表示winner是第几个人([1, N]中的某个整数)。

如果不存在winner,则用一行输出“No winner”(不包括引号)。

Sample Input

3 3 5 5 2 3 2 0 0 3 0 2 5

Sample Output

3 No winner No winner

代码:

#include<iostream>
using namespace std;
 
int main()
{
    int t, n;
    cin >> t;
    int a, b, f;
    while (t--)
    {       
        cin >> n >> a >> b;
        if (a == b)
        {
            int s = 0;
            bool flag = true;
            int m = 0;
            for (int i = 2; i<n; i++)
            {
                cin >> f;
                if (a == 5 && f == 2 || a == 0 && f == 5 || a == 2 && f == 0)
                {
                    if(m==0)m = i;
                    s++;
                }
                else if (a != f)flag = false;
            }
            if (flag && s == 1)cout << m+1;
            else cout << "No winner";
        }
        else if (n>2)
        {
            cin >> f;
            if (f != a && f != b)
            {
                for (int i = 3; i < n; i++)cin >> f;
                cout << "No winner";
            }
            else
            {
                bool fl = true;
                if (a == f)
                {
                    fl = false;
                    a = b;
                    b = f;
                }
                bool flag = true;
                for (int i = 3; i < n; i++)
                {
                    cin >> f;
                    if (f != b)flag = false;
                }
                if (flag)
                {
                    if (fl)
                    {
                        if (a == 2 && b == 5 || a == 5 && b == 0 || a == 0 && b == 2)cout << 1;
                        else cout << "No winner";
                    }
                    else
                    {
                        if (a == 2 && b == 5 || a == 5 && b == 0 || a == 0 && b == 2)cout << 2;
                        else cout << "No winner";
                    }
                }
                else cout << "No winner";
            }
        }
        else
        {
            if (a == 2 && b == 5 || a == 5 && b == 0 || a == 0 && b == 2)cout << 1;
            else cout << 2;
        }
        cout << endl;
    }
    return 0;
}

CSU 1283: Binary Tree Traversals

题目:

Description

给定一棵二叉树先序遍历得到的先序序列和后续遍历得到的后序序列,能够唯一确定这棵二叉树吗?

Input

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

对于每组测试数据,第一行包含一个正整数N (1 <= N <= 26),表示这棵二叉树一共有N个节点。接下来两行每行均包含N个互不相同的大写字母,字符之间没有空格,分别表示对这棵二叉树进行先序遍历和后序遍历后得到的先序序列和后序序列。数据保证根据先序序列和后续序列至少能够构造出一棵二叉树。

Output

对于每组测试数据,如果根据先序序列和后续序列能够唯一确定一棵二叉树,用一行输出N个大写字母,字符之间不应有空格,表示这棵二叉树的中序序列。如果不能唯一确定一棵二叉树,则用一行输出“-1”(不包括引号)。

Sample Input

2
2
AB
BA
3
ABC
BCA

Sample Output

-1
BAC

代码:

#include<iostream>
using namespace std;
 
char s1[27], s2[27], s3[27];
 
bool f(int k1, int k2, int k3, int len)
{
	if (len == 1)
	{
		s3[k3] = s1[k1];
		return true;
	}
	char c = s1[k1 + 1];
	for (int i = k2; i < k2 + len; i++)
	{
		if (s2[i] != c)continue;
		if (i == k2 + len - 2)return false;
		if (!f(k1 + 1, k2, k3, i - k2 + 1))return false;
		if (!f(k1 - k2 + i + 2, i + 1, k3 - k2 + i + 2, k2 + len - 2 - i))return false;
		s3[k3 - k2 + i + 1] = s1[k1];
	}
	return true;
}
 
int main()
{
	int t, n;
	cin >> t;	
	while (t--)
	{
		cin >> n >> s1 >> s2;
		s3[n] = '\0';
		if (f(0, 0, 0, n))cout << s3 << endl;
		else cout << "-1\n";
	}
	return 0;
}

CSU 1341: String and Arrays

题目:

Description

    有一个N*N的字符矩阵,从上到下依次记为第1行,第2行,……,第N行,从左至右依次记为第1列,第2列,……,第N列。
    对于这个矩阵会进行一系列操作,但这些操作只有两类:
    (1) R: 将矩阵逆时针旋转90度;
    (2) P x y: 将此时第x行第y列的字符打印出来,其中1 <= xy <= N

Input

    输入数据的第一行包含一个整数T (1 <= T <= 20),表示接下来一共有T组测试数据。
    对于每组测试数据,第一行包含一个整数N (1 <= N <= 300),含义同上。接下来一共有N行,每行均包含N个大写字母,描述了这个矩阵的初始情况。再接下来一行包含一个整数M (1 <= M <=10000)表示一共对矩阵进行了M次操作。接下来M行,每行均包含一个符合上述格式的操作,依次描述了这M个操作。

Output

    对于每个第(2)类操作,用一行打印出指定位置的字符。
    相邻的两组测试数据中间用一个空行隔开。

Sample Input

3
2
AB
CD
3
P 1 1
R
P 1 1
2
AB
CD
4
R
R
P 2 1
P 1 2
3
ABC
DEF
GHI
5
P 3 3
R
P 3 3
R
P 3 3

Sample Output

A
B

B
C

I
G
A

HINT

    这个题目主要练习如何读入字符串,以及对数组的使用。

    初始矩阵直接用读入字符串的方式读入就可以了,但是每个操作要如何读入呢?先用getchar(x)或者scanf(“%c”, &x)读入一个字符,然后再根据读入的字符判断是否要再读入两个整数? 

    其实getchar(x)或者scanf(“%c”, &x)有的时候很令人头疼(也许只有你亲自尝试过才能体会到), 因为他不仅可以读入一个字母,也可以读入空格、换行等等,因此如果我们不细加控制的话,scanf(“%c”, &x)很可能读到的未必是我们想要的那个字符。但scanf(“%s”, s)就不一样了,会忽略掉空格、换行等等,就像我们使用scanf(“%d”, &x)读整数那样。因此即使只有一个字母,我们也更推荐使用scanf(“%s”, s)的形式去读入,然后s[0]自然就是那个字符了。

    这个题目具体要怎么做呢?看似直接模拟两种操作就好了,打印字符最简单了,而旋转矩阵也不是很麻烦,不过可能要借助一个“中间矩阵”来完成旋转更好一些。但问题来了,如果每个R我们都进行旋转的话,实际上代价是很大的,对于这个题目而言会超时。也许你会机智地想到把相邻的R操作合并成一个,这样代价会小一点,是的,但是这样并不能解决根本问题,比如R和P是交替出现的。

    但其实我们可以做到只要旋转3次矩阵,你想到了吗?

    或者也许我们根本没必要按题意来去旋转矩阵?是否可以直接找到要打印的字符在初始矩阵中的位置呢?

    最后注意相邻两组测试数据之间要打印一个空行,就像在“A Sample Problem”中说的那样,OJ对于输出的格式要求是很严格的。

解析写的太好了,我就直接上代码好了。

代码:

#include <iostream>
using namespace std;

char c[301][301];
int t, n, m, sum, x, y;

char f()
{
	if (sum == 0)return c[x][y];
	if (sum == 1)return c[n + 1 - y][x];
	if (sum == 2)return c[n + 1 - x][n + 1 - y];
	return c[y][n + 1 - x];
}

int main()
{
	ios_base::sync_with_stdio(false);
	char ch;
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)cin >> c[i][j];
		cin >> m;
		sum = 0;
		while (m--)
		{
			cin >> ch;
			if (ch == 'R')sum--;	//不是加,是减
			else
			{
				sum = ((sum % 4) + 4) % 4;
				cin >> x >> y;
				cout << f() << endl;
			}
		}
		cout << endl;
	}
	return 0;
}

CSU 1585: 刻苦练习CCF

题目:

Description

star参加了今年三月的CCF认证测试,他很郁闷,因为他连第一题都没有得满分,这对一个自我要求严格的ACMer来说是不能容忍的。这是一道矩阵转换的题目,于是他决定好好学习线性代数(可见数学是多么有用的)。他想请你帮忙做几道矩阵转换的题目。对于一个n*m的矩阵(1<n<=100,1<m<=100),指令1代表将这个矩阵顺时针旋转环90度,指令2代表将这个矩阵逆时针旋转90度。

Input

第一行输入一个T(T<=20),代表共有T组数据测试。
对于每一组测试,第一行输入两个正整数n,m,代表矩阵的维度。第二行输入一个整数q(q=1或2),代表对这个矩阵的指令。接下来的n行,每一行有m个正整数(且小于1000)。

Output

输出要求的矩阵,同一行的相邻两个元素用一个空格隔开。

Sample Input

2
2 2
1
1 2
3 4
3 2
2
1 2 
3 4
5 6

Sample Output

3 1
4 2
2 4 6
1 3 5

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int t, n, m, q, num[100][100];
	cin >> t;
	while (t--)
	{
		cin >> n >> m >> q;
		for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)cin >> num[i][j];
		if (q == 2)for (int j = m - 1; j >= 0; j--)
		{
			for (int i = 0; i < n; i++)cout << ((i) ? " " : "") << num[i][j];
			cout << endl;
		}
		else for (int j = 0; j < m; j++)
		{
			for (int i = n - 1; i >= 0; i--)cout << num[i][j] << ((i) ? " " : "");
			cout << endl;
		}
	}
	return 0;
}

CSU 1600: Twenty-four point (24点)

题目:

Description

Given four numbers, can you get twenty-four through the addition, subtraction, multiplication, and division? Each number can be used only once.

Input

The input consists of multiple test cases. Each test case contains 4 integers A, B, C, D in a single line (1 <= A, B, C, D <= 13).

Output

For each case, print the “Yes” or “No”. If twenty-four point can be get, print “Yes”, otherwise, print “No”.

Sample Input

2 2 3 9
1 1 1 1 
5 5 5 1

Sample Output

Yes
No
Yes

Hint

For the first sample, (2/3+2)*9=24.

我在 24点游戏综述中有提到,24点分2大类,一种是除法可以出现分数的,一种是不可以的,

本题是可以的,在HDU 1427 24点游戏这个题目中是不可以的

代码:

#include<iostream>
using namespace std;
 
bool ok(double a)
{
	return a > 23.99999&&a < 24.00001;
}
 
bool ok(double a, double b)
{
	if (b == 0)return ok(a);
	return ok(a + b) || ok(a - b) || ok(a*b) || ok(a / b);
}
 
bool ok(double a, double b, double c)
{
	if (b == 0)return ok(a, c);
	if (c == 0)return ok(a, b);
	if (ok((a + b), c) || ok((a - b), c) || ok((a * b), c)|| ok(a / b, c))return true;
	if (ok(a, b + c) || ok(a, b - c) || ok(a, b * c)|| ok(a, b / c))return true;
	return false;
}
 
bool ok(double a, double b, double c, double d)
{
	if (ok(a + b, c, d) || ok(a - b, c, d) || ok(a * b, c, d)|| ok(a / b, c, d))return true;
	if (ok(a, b + c, d) || ok(a, b - c, d) || ok(a, b * c, d)|| ok(a, b / c, d))return true;
	if (ok(a, b, c + d) || ok(a, b, c - d) || ok(a, b, c * d)|| ok(a, b, c / d))return true;
	return false;
}
 
bool ok1(int a, int b, int c, int d)
{
	return ok(a, b, c, d) || ok(a, c, b, d) || ok(b, a, c, d) || ok(b, c, a, d) || ok(c, a, b, d) || ok(c, b, a, d);
}
 
int main()
{
	int a, b, c, d;
	while (cin >> a >> b >> c >> d)
	{
		if (ok1(a, b, c, d) || ok1(b, c, d, a) || ok1(c, d, a, b) || ok1(d, a, b, c))cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}

HDU 1427 24点游戏

题目:

Description

速算24点相信绝大多数人都玩过。就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13)。要求只用'+','-','*','/'运算符以及括号改变运算顺序,使得最终运算结果为24(每个数必须且仅能用一次)。游戏很简单,但遇到无解的情况往往让人很郁闷。你的任务就是针对每一组随机产生的四张牌,判断是否有解。我们另外规定,整个计算过程中都不能出现小数。 

Input

每组输入数据占一行,给定四张牌。 

Output

每一组输入数据对应一行输出。如果有解则输出"Yes",无解则输出"No"。 

Sample Input

A 2 3 6

3 3 8 8

Sample Output

Yes

No

主要的思路就是函数的嵌套。

这个题目我是只用一个字符ch记录所有输入的,空格会自动忽略,0我就人为忽略,遇到0就读取下一个字符,这样,A就是表示1,1就是表示10

代码:

#include<iostream>
using namespace std;

int number(char c)
{
	if (c == 'A')return 1;
	if (c == 'J')return 11;
	if (c == 'Q')return 12;
	if (c == 'K')return 13;
	if (c == '1')return 10;
	return c - '0';
}

bool ok(int a, int b)
{
	if (a + b == 24)return true;
	if (a - b == 24)return true;
	if (a*b == 24)return true;
	if (a != 0 && a == 24 * b)return true;
	return false;
}

bool ok(int a, int b, int c)
{
	if (b == 0)
	{
		if (ok(a, c))return true;
		return false;
	}
	if (c == 0)
	{
		if (ok(a, b))return true;
		return false;
	}
	if (ok((a + b), c) || ok((a - b), c) || ok((a * b), c))return true;
	if (a%b == 0 && ok(a / b, c))return true;
	if (ok(a, b + c) || ok(a, b - c) || ok(a, b * c))return true;
	if (b%c == 0 && ok(a, b / c))return true;
	return false;
}

bool ok(int a, int b, int c, int d)
{
	if (ok(a + b, c, d) || ok(a - b, c, d) || ok(a * b, c, d))return true;
	if (a%b == 0 && ok(a / b, c, d))return true;
	if (ok(a, b + c, d) || ok(a, b - c, d) || ok(a, b * c, d))return true;
	if (b%c == 0 && ok(a, b / c, d))return true;
	if (ok(a, b, c + d) || ok(a, b, c - d) || ok(a, b, c * d))return true;
	if (c%d == 0 && ok(a, b, c / d))return true;
	return false;
}

bool ok1(int a, int b, int c, int d)
{
	if (ok(a, b, c, d) || ok(a, c, b, d) || ok(b, a, c, d) || ok(b, c, a, d) || ok(c, a, b, d) || ok(c, b, a, d))return true;
	return false;
}

int main()
{	
	char ch;
	int a, b, c, d;
	while (cin >> ch)
	{
		if (ch == '0')cin >> ch;
		a = number(ch);
		cin >> ch;
		if (ch == '0')cin >> ch;
		b = number(ch);
		cin >> ch;
		if (ch == '0')cin >> ch;
		c = number(ch);
		cin >> ch;
		if (ch == '0')cin >> ch;
		d = number(ch);
	if (ok1(a, b, c, d)||ok1(b,c,d,a)||ok1(c,d,a,b)||ok1(d,a,b,c))cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}

CSU 1777 NBUT 1639 大还是小?

题目:

Description

输入两个实数,判断第一个数大,第二个数大还是一样大。每个数的格式为: [整数部分].[小数部分]

简单起见,整数部分和小数部分都保证非空,且整数部分不会有前导 0。不过,小数部分的最 后可以有 0,因此 0.0 和 0.000 是一样大的。

Input

输入包含不超过 20 组数据。每组数据包含一行,有两个实数(格式如前所述)。每个实数都 包含不超过 100 个字符。 

Output

对于每组数据,如果第一个数大,输出"Bigger"。如果第一个数小,输出"Smaller"。如果两个 数相同,输出"Same"。 

Sample Input

1.0 2.0
0.00001 0.00000
0.0 0.000

Sample Output

Case 1: Smaller
Case 2: Bigger
Case 3: Same

这个题目的输入很长,所以只能用字符串来处理。(char数组也是一种字符串)

只需要2个字符串同时从走往右扫描,扫完就知道谁大谁小了。

因为整数部分没有前缀0,所以先出现小数点的自然是较小的数。

就算题目改成会有前缀0也不麻烦,只要在扫描的开始时候记录0的个数即可。

如果小数点在同样的位置,根据已扫描的整数部分谁大谁小来判断。

如果整数部分也一样大,就要继续扫描小数部分了。

代码:

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
 
char a[101];
char b[101];
int la, lb;
int flag;
 
void f()
{
	la = strlen(a), lb = strlen(b);
	flag = 0;
	int i;
	for (i = 0; i < la && i < lb; i++)
	{
		if (a[i] == '.')
		{
			if (b[i] != '.')
			{
				flag = -1;
				return;
			}
			if (flag)return;
		}
		if (a[i] != '.'&&b[i] == '.')
		{
			flag = 1;
			return;
		}
		if (flag == 0)flag = a[i] - b[i];
	}
	if (flag)return;
	while (i < la)flag += a[i++] - '0';
	while (i < lb)flag -= b[i++] - '0';
}
 
int main()
{
	int cas = 1;
	while (cin >> a >> b)
	{
		cout << "Case " << cas++ << ": ";
		f();
		if (flag > 0)cout << "Bigger";
		else if (flag < 0)cout << "Smaller";
		else cout << "Same";
		cout << endl;
	}
	return 0;
}

函数f中的2个while循环实际上最多只有1个会运行。

我们不关心到底哪个运行了,只需要知道结果肯定是flag的正负直接对应2个数的大小关系即可。

CSU 2043: 身份证号

题目:

Description

每位公民都有一个唯一的身份证号码,居民身份证号码由 17 位本体码和 1 位校验码组成。17 位的本体码由三部分组成:地址码、出生日期码和顺序码。其中地址码占 6 位、出生日期码占 8 位、顺序码占 3 位。顺序码的奇数分配给男性,偶数分配给女性,同时顺序码的 3 位不能为全 0。出生日期码的前 4 位表示出生年份,接下来 2 位表示出生月份,最后两位表示出生日期。考虑到实际的情况,我们约定出生日期必须在 1900 年 1 月 1 日到 2011 年 12 月 31 日的范围内,并且对于闰年的 2 月有 29 日。 最后对于地址码,它表示所属的行政区划,输入文件中会给出对于该测试点所有可行的地址码。1 位的校验码是根据前面 17 位计算得出的。将本体码从左到右用a1,a2…a17表示,校验码用x表示,则下面的关系成立

其中0 <=x<=10,如果x=10,就用大写英文字母表示。
按照前面的规定,可以验证 11010519491231002X 是一个合法的女性身份证号码(110105 是北京市朝阳区的地址码)。现在要你写一个程序验证给定的身份证号码是否合法,并且对于合法的身份证,给出这位公民的性别。
注意:能被 4 整除的非整百年或能被 400 整除的年份称为闰年。例如 2004 年、 2000 年是闰年, 1900 年、 2003年不是闰年。

Input

第一行包含两个正整数M,N 1<=M<=4000 第二行包含M个长度为 6 的数字串, 表示该测试点中可行的地址码,地址码保证第一位数字非 0。 下面包含N行,每行给出一个长度为 18 的字符串,其中前 17 位为 0 到 9 的数字,第 18 位为 0 到 9 的数字或大写字母X

Output

共N行,第i行表示对于第i个输入号码的判断结果。Invalid 表示不合法, Male 表示合法并且为男性, Female 表示合法并且为女性。

Sample Input

2 3
110105 320102
11010519491231002X
320102199502091613
110105198312310013

Sample Output

Female
Male
Invalid

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
 
int m, n, a6[4001];
int d[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
 
bool f(int a)
{
	int low = 0, high = m, mid;
	while (low<high)
	{
		mid = (low + high) / 2;
		if (a6[mid] == a)return true;
		if (a6[mid]>a)high = mid;
		else low = mid + 1;
	}
	return false;
}
 
int main()
{
	char s[18];
	scanf("%d%d", &m, &n);
	for (int i = 0; i<m; i++)scanf("%d", &a6[i]);
	sort(a6, a6 + m);
	while (n--)
	{
		scanf("%s", &s);
		int a = (s[0] - '0') * 100000 + (s[1] - '0') * 10000 + (s[2] - '0') * 1000 + (s[3] - '0') * 100 + (s[4] - '0') * 10 + s[5] - '0';
		int year = (s[6] - '0') * 1000 + (s[7] - '0') * 100 + (s[8] - '0') * 10 + s[9] - '0';
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)d[2] = 29;
		else d[2] = 28;
		int month = (s[10] - '0') * 10 + s[11] - '0';
		int day = (s[12] - '0') * 10 + s[13] - '0';
		int sum = 0;
		for (int i = 0; i<17; i++)sum += (s[i] - '0')*(1 << (17 - i)) % 11;
		if (s[17] == 'X')sum += 10;
		else sum += s[17] - '0';
		if (!f(a) ||  month<1 || month>12 || day<0 || day>d[month] || sum % 11 != 1)
		{
			printf("Invalid\n");
			continue;
		}
		if ((s[16] - '0') % 2)printf("Male\n");
		else printf("Female\n");
	}
	return 0;
}

CSU 2142: 独特的骰子

题目:

Description

天真的Wells认为自己的骰子是独一无二的。

当有人告诉他的骰子很可能在商店里有很多个时,Wells简直不敢相信自己的小耳朵!

那么问题来了,眼花的Wells会告诉你他的独特的骰子的6个面,然后再告诉你一堆商店里骰子。

问和Wells的独特骰子长得一模一样的到底有多少个。

当然骰子是可以任意旋转的。

Input

第一行一个数n。接下来n行每行6个数,分别表示这个骰子的前后左右上下面上的数字。其中第一行为Wells的独特骰子,剩余的为商店骰子。(n<=10000)

Output

输出有几个骰子(不包括第一个)是和第一个一样的。

Sample Input

6
1 2 3 4 5 6
3 4 2 1 5 6
4 3 1 2 5 6
5 6 3 4 2 1
1 2 3 4 6 5
6 6 6 6 6 6

Sample Output

3

代码:

#include<iostream>
using namespace std;
 
int x[6];
 
bool f1(int a, int b, int c, int d, int e, int f)
{
	return a == x[0] && b == x[1] && c == x[2] && d == x[3] && e == x[4] && f == x[5];
}
 
bool f2(int a, int b, int c, int d, int e, int f)
{
	if (f1(a, b, c, d, e, f))return true;
	if (f1(c, d, b, a, e, f))return true;
	if (f1(b, a, d, c, e, f))return true;
	if (f1(d, c, a, b, e, f))return true;
	return false;
}
 
bool f3(int a, int b, int c, int d, int e, int f)
{
	if (f2(a, b, c, d, e, f))return true;
	if (f2(a, b, d, c, f, e))return true;
	return false;
}
 
bool f4(int a, int b, int c, int d, int e, int f)
{
	if (f3(a, b, c, d, e, f))return true;
	if (f3(a, b, e, f, d, c))return true;
	if (f3(e, f, c, d, b, a))return true;
	return false;
}
 
int main()
{
	int n, s = 0, a, b, c, d, e, f;
	cin >> n >> x[0] >> x[1] >> x[2] >> x[3] >> x[4] >> x[5];
	n--;
	while (n--)
	{
		cin >> a >> b >> c >> d >> e >> f;
		if (f4(a, b, c, d, e, f))s++;
	}
	cout << s << endl;
	return 0;
}

CodeForces 1B Spreadsheets(EXCEL)

Description

  在一些知名的表格处理系统中(比如:excel表格),我们经常用大写的字母来表示列,例如A表示第1列,B表示第2列,第26列用Z来表示,同时第27列我们用AA来表示,第28列我们用AB来表示,第29列我们用AC来表示,AZ表示第52列,ZZ之后我们就需要用3个字母来表示列了。

  行的表示比较简单,我们一般用正整数来表示,比如1就表示第1行,5就表示第5行,行和列一起表示成为类似BC23的情况,这个表示在第23行,第55列。

  有时候,我们的系统也会用RXCY的格式来表示,X和Y是整数,分别表示行号和列号。例如:R23C55表示的位置和之前那个例子是一样的。

  你的任务是写一个程序,将这些位置在两种不同的表示方法之间转化。

Input

第一行是一个正整数n(1<=n<=10^5), 表示测试数据的数量。

接下来n行,每行一串字符,表示一个位置,输入保证所有的位置都是正确的,没有一个位置的行号或者列号超过10^ 6。

Output

输出n行,每行是对应的位置的转化结果。

Sample Input

2

R23C55

BC23

Sample Output

BC23

R23C55

首先是26进制,和正常的26进制有一点点区别,但是如果仔细体会进制的规则,还是可以写出很对称的代码的。

我的代码:

#include<stdio.h>
#include<string.h>
 
int main()
{
	int r, c, d, e, f, g, k,l,z;
	char ch[20];
	int n;
	scanf("%d", &n);
	while (n--)
	{
		scanf("%s", ch);
		l = strlen(ch);
		z = 0;
		for (int i = 0; i < l; i++)if (ch[i]>'9')z++;
		if (z == 2 && ch[0] == 'R' && ch[1] <= '9')
		{
			k = 1;
			r = 0;
			while (ch[k] <= '9')
			{
				r *= 10;
				r += ch[k] - '0';
				k++;
			}
			k++;
			c = 0;
			while (k < l)
			{
				c *= 10;
				c += ch[k] - '0';
				k++;
			}
			d = (c - 1) / 26;
			e = (d - 1) / 26;
			f = (e - 1) / 26;
			g = (f - 1) / 26;
			if (g)printf("%c", 'A' + (g - 1) % 26);
			if (f)printf("%c", 'A' + (f - 1) % 26);
			if (e)printf("%c", 'A' + (e - 1) % 26);
			if (d)printf("%c", 'A' + (d - 1) % 26);
			printf("%c", 'A' + (c - 1) % 26);
			printf("%d\n", r);
		}
		else
		{
			if (z == 1)c = ch[0] - 'A' + 1;
			else if (z == 2)c = ch[1] - 'A' + 1 + (ch[0] - 'A' + 1) * 26;
			else if (z == 3)c = ch[2] - 'A' + 1 + (ch[1] - 'A' + 1) * 26 + (ch[0] - 'A' + 1) * 26 * 26;
			else if (z == 4)c = ch[3] - 'A' + 1 + (ch[2] - 'A' + 1) * 26 + (ch[1] - 'A' + 1) * 26 * 26 + (ch[0] - 'A' + 1) * 26 * 26 * 26;
			else c = ch[4] - 'A' + 1 + (ch[3] - 'A' + 1) * 26 + (ch[2] - 'A' + 1) * 26 * 26 + (ch[1] - 'A' + 1) * 26 * 26 * 26 + (ch[0] - 'A' + 1) * 26 * 26 * 26 * 26;
			k = z;
			r = 0;
			while (k < l)
			{
				r *= 10;
				r += ch[k] - '0';
				k++;
			}
			printf("R%dC%d\n", r, c);
		}
	}
	return 0;
}

首先直接用if(z==2&& ch[0]=='R'&& ch[1]<='9')判断到底是R23C55的格式还是BC23的格式,然后分开处理。
最开始我是写了一个函数求ch里面的字母的个数。

int zimu(char ch[])
{
	int l = strlen(ch);
	int sum = 0;
	for (int i = 0; i < l; i++)if (ch[i]>'9')sum++;
	return sum;
}

结果是 Runtime error on test 10 然后就感觉应该是因为这个函数申请了太多内存。
然后仔细一看,现在的程序和最开始的已经完全不一样了,最开始的思路是从左往右一步步判断,后来发现特别麻烦,还存在重复工作的现象。

现在的程序已经不需要这个函数了,就把求ch里面的字母的个数的代码直接写在main函数里面了,然后把所有变量都拿到while(n--)这个循环外面,终于解决了这个问题。

CodeForces 471B MUH and Important Things

题目:

Description

It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are n tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order of their difficulty. Unfortunately, some tasks can have the same difficulty, so the order in which one can perform the tasks may vary.

Menshykov, Uslada and Horace ask you to deal with this nuisance and come up with individual plans for each of them. The plan is a sequence describing the order in which an animal should do all the n tasks. Besides, each of them wants to have its own unique plan. Therefore three plans must form three different sequences. You are to find the required plans, or otherwise deliver the sad news to them by stating that it is impossible to come up with three distinct plans for the given tasks.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of tasks. The second line contains n integers h1, h2, ..., hn (1 ≤ hi ≤ 2000), where hi is the difficulty of the i-th task. The larger number hi is, the more difficult the i-th task is.

Output

In the first line print "YES" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line "NO" (without the quotes). If three desired plans do exist, print in the second line n distinct integers that represent the numbers of the tasks in the order they are done according to the first plan. In the third and fourth line print two remaining plans in the same form.

If there are multiple possible answers, you can print any of them.

Sample Input

Input

4
1 3 3 1

Output

YES
1 4 2 3 
4 1 2 3 
4 1 3 2 

Input

5
2 4 1 4 8

Output

NO

Hint

In the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.

In the second sample there are only two sequences of tasks that meet the conditions — [3, 1, 2, 4, 5] and [3, 1, 4, 2, 5]. Consequently, it is impossible to make three distinct sequences of tasks.

题目就是要输出3种顺序,如果不足3种就是NO。

比如输出的1 4 2 3表示的是第1个数、第4个数、第2个数、第3个数这样的顺序。

要找3种不同的顺序,使得得到的都是单调不减的数列。

对于本题,只需要找3种,可以分类讨论。

1,如果有1个数出现了3次,那么这3个数就有了6种顺序。

2,如果每个数都只出现1次或者2次,1次的当然没什么用,要看有多少个数出现了2次。

如果只有1个的话,那么答案就是NO,否则,2个数各出现了2次就有了4种顺序。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
 
struct node
{
	int number;
	int key;
};
node nod[2001];
int num[2001];
 
bool cmp(node a,node b)
{
	return a.number < b.number;
}
 
int main()
{
	int n, a, f1 = 0, f2 = 0;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a;
		nod[i].number = a;
		nod[i].key = i;
		num[a]++;
		if (num[a] > 1)
		{
			if (f1 == 0)f1 = a;
			else f2 = a;
		}
	}
	sort(nod + 1, nod + 1 + n, cmp);
	if (f1)
	{
		if (num[f1] >2)
		{
			cout << "YES" << endl;
			int k;
			for (int i = 1; i <= n; i++)
			{
				if (nod[i].number == f1)k = i;
				cout << nod[i].key << " ";
			}
			cout << endl;
			for (int i = 1; i <= n; i++)
			{
				if (i == k - 2)
				{
    				cout << nod[k - 1].key << " " << nod[k - 2].key << " ";
				    i += 2;
				}
				cout << nod[i].key << " ";
			}
			cout << endl;
			for (int i = 1; i <= n; i++)
			{
				if (i == k - 1)
				{
				    cout << nod[k].key << " " << nod[k - 1].key << " ";
				    i++;
				continue;
				}
				cout << nod[i].key << " ";
			}
			cout << endl;
		}
		else if (f2)
		{
			cout << "YES" << endl;
			for (int i = 1; i <= n; i++)cout << nod[i].key << " ";
			cout << endl;
			for (int i = 1; i <= n; i++)
			{
				if (nod[i].number == f1)
				{
				    cout << nod[i + 1].key << " " << nod[i].key << " ";
				    i++;
				    continue;
				}
				cout << nod[i].key << " ";
			}
			cout << endl;
			for (int i = 1; i <= n; i++)
			{
				if (nod[i].number == f2)
				{
				    cout << nod[i + 1].key << " " << nod[i].key << " ";
				    i++;
				    continue;
				}
				cout << nod[i].key << " ";
			}
			cout << endl;
		}
		else cout << "NO" << endl;
	}
	else cout << "NO" << endl;
	return 0;
}

CodeForces 616A Comparing Two Long Integers

题目:

Description

You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.

The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the functioninput() in Python2 instead of it use the function raw_input().

Input

The first line contains a non-negative integer a.

The second line contains a non-negative integer b.

The numbers a, b may contain leading zeroes. Each of them contains no more than 106 digits.

Output

Print the symbol "<" if a < b and the symbol ">" if a > b. If the numbers are equal print the symbol "=".

Sample Input

Input

9
10

Output

<

Input

11
10

Output

>

Input

00012345
12345

Output

=

Input

0123
9

Output

>

Input

0123
111

Output

>

就是比较2个整数的大小,很简单。

主要就是c语言不熟悉。

代码:

#include<stdio.h>
#include <stdlib.h> 
#include<string.h>
 
 
int main()
{
	char*s1 = new char[1000000];
	char*s2 = new char[1000000];
	int l1, l2;
	char c;
	while (gets(s1)&&gets(s2))
	{
		l1 = strlen(s1);
		l2 = strlen(s2);
		int i = 0, j = 0;
		while (s1[i] == '0'&& l1)
		{
			i++;
			l1--;
		}
		while (s2[j] == '0'&& l2)
		{
			j++;
			l2--;
		}
		if (l1 > l2)c='>';
		else if (l1 < l2)c='<';
		else
		{
			while (s1[i])
			{
				if (s1[i]>s2[j])
				{
					c = '>';
					break;
				}
				if (s1[i]<s2[j])
				{
					c = '<';
					break;
				}
				i++;
				j++;
				l1--;
			}
			if (l1 == 0)c = '=';
		}
		printf("%c\n", c);
	}
	return 0;
}

CodeForces 685A 686C Robbers' watch

题目:

Description

Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.

First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation.

Note that to display number 0 section of the watches is required to have at least one place.

Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number.

Input

The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 109) — the number of hours in one day and the number of minutes in one hour, respectively.

Output

Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.

Sample Input

Input

2 3

Output

4

Input

8 2

Output

5

Hint

In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2).

In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).

这个题目的大意就是,冒号左边的是0到n-1,都要用7进制表示成相同的长度,右边的数是0到m-1,也都要用7进制表示成相同的长度。

左边的数和右边的数可以独立的取,也就是一共有n*m种情况。

要求一共有多少种情况,使得所有的数码都不重复出现(可以不出现)

代码:

#include<iostream>
using namespace std;
 
int f(int n)		//求7进制的位数
{
	if (n > 117649)return 7;//注意,这个地方没有等于
	if (n > 16807)return 6;
	if (n > 2401)return 5;
	if (n > 343)return 4;
	if (n > 49)return 3;
	if (n > 7)return 2;
	return 1;
}
 
bool ok(int x, int y,int lx,int ly)//判断有没有重复的数码
{
	int list[7] = { 0, 0, 0, 0, 0, 0, 0 };
	while (lx--)
	{
		if (list[x % 7])return false;
		list[x % 7]++;
		x /= 7;
	}
	while (ly--)
	{
		if (list[y % 7])return false;
		list[y % 7]++;
		y /= 7;
	}
	return true;
}
 
int main()
{
	int n, m;
	int ln, lm;
	int sum;
	while (cin >> n >> m)
	{
		
		sum = 0;
		ln = f(n);
		lm = f(m);
		if ((ln + lm) <= 7)for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (ok(i, j, ln, lm))sum++;
		cout << sum << endl;
	}
	return 0;
}

这个是31ms,还有一个小技巧可以变快一点点。

代码:

#include<iostream>
using namespace std;
 
int f(int n)		//位数
{
	if (n > 117649)return 7;
	if (n > 16807)return 6;
	if (n > 2401)return 5;
	if (n > 343)return 4;
	if (n > 49)return 3;
	if (n > 7)return 2;
	return 1;
}
 
bool ok(int x, int y,int lx,int ly)
{
	int list[7] = { 0, 0, 0, 0, 0, 0, 0 };
	while (lx--)
	{
		if (list[x % 7])return false;
		list[x % 7]++;
		x /= 7;
	}
	while (ly--)
	{
		if (list[y % 7])return false;
		list[y % 7]++;
		y /= 7;
	}
	return true;
}
 
int main()
{
	int n, m;
	int ln, lm;
	int sum;
	while (cin >> n >> m)
	{
		if (n < m)
		{
			n = m + n;
			m = n - m;
			n = n - m;
		}
		sum = 0;
		ln = f(n);
		lm = f(m);
		if ((ln + lm) <= 7)for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)
		if (ok(i, j, ln, lm))sum++;
		cout << sum << endl;
	}
	return 0;
}

这个是15ms

CodeForces 691C Exponential notation

题目:

Description

You are given a positive decimal number x.

Your task is to convert it to the "simple exponential notation".

Let x = a·10b, where 1 ≤ a < 10, then in general case the "simple exponential notation" looks like "aEb". If b equals to zero, the part "Eb" should be skipped. If a is an integer, it should be written without decimal point. Also there should not be extra zeroes in a and b.

Input

The only line contains the positive decimal number x. The length of the line will not exceed 106. Note that you are given too large number, so you can't use standard built-in data types "float", "double" and other.

Output

Print the only line — the "simple exponential notation" of the given number x.

Sample Input

Input

16

Output

1.6E1

Input

01.23400

Output

1.234

Input

.100

Output

1E-1

Input

100.

Output

1E2

因为x是大于0的,所以很容易求得最左边的和最右边的非0数字,不会出现什么意外。

求小数点的位置的时候,如果不存在小数点,就假设在末尾增加一个小数点,也就是dot初始化为strlen(c)

最后输出E后面的内容的时候还挺有意思的,2种情况表达式不一样。

代码:

#include<iostream>
 
using namespace std;
 
char c[1000002];
int main()
{
		cin.getline(c, 1000002);
		int l = strlen(c);
		int start = -1, end = -1;
		int dot = l;
		for (int i = 0; i < l; i++)
		{
			if (c[i] == '.')dot = i;
			else if (c[i] != '0')
			{
				if (start < 0)start = i;
				end = i;
			}
		}
		cout << c[start];
		if (start != end)
		{
			cout << ".";
			for (int i = start + 1; i <= end; i++)
			{
				if (i != dot)cout << c[i];
			}
		}
		if (dot>start + 1)cout << "E" << dot - start - 1;
		if (dot < start)cout << "E" << dot - start;
		return 0;
}

CodeForces 697B Barnicle (科学计数法转化成普通数)

题目:

Description

Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his heart arrow but he needs to know the distance between him and the girl to make his shot accurate.

Barney asked the bar tender Carl about this distance value, but Carl was so busy talking to the customers so he wrote the distance value (it's a real number) on a napkin. The problem is that he wrote it in scientific notation. The scientific notation of some real number x is the notation of form AeB, where A is a real number and B is an integer and x = A × 10B is true. In our case A is between 0 and 9 and Bis non-negative.

Barney doesn't know anything about scientific notation (as well as anything scientific at all). So he asked you to tell him the distance value in usual decimal representation with minimal number of digits after the decimal point (and no decimal point if it is an integer). See the output format for better understanding.

Input

The first and only line of input contains a single string of form a.deb where ad and b are integers and e is usual character 'e' (0 ≤ a ≤ 9, 0 ≤ d < 10100, 0 ≤ b ≤ 100) — the scientific notation of the desired distance value.

a and b contain no leading zeros and d contains no trailing zeros (but may be equal to 0). Also, b can not be non-zero if a is zero.

Output

Print the only real number x (the desired distance value) in the only line in its decimal notation.

Thus if x is an integer, print it's integer value without decimal part and decimal point and without leading zeroes.

Otherwise print x in a form of p.q such that p is an integer that have no leading zeroes (but may be equal to zero), and q is an integer that have no trailing zeroes (and may not be equal to zero).

Sample Input

Input

8.549e2

Output

854.9

Input

8.549e3

Output

8549

Input

0.33e0

Output

0.33

我只想说2个字,心累啊。

代码:

#include<iostream>
using namespace std;
 
char c[107];
 
int main()
{
	cin.getline(c, 107);
	int e;
	for (int i = 0; i < strlen(c); i++)if (c[i] == 'e')e = i;		//求e的位置
	int b = c[e + 1] - '0';
	if (e + 2 < strlen(c))		//求b
	{
		b *= 10;
		b += c[e + 2] - '0';
		if (e + 3 < strlen(c))b = 100;
	}
 
	if (e == 3 && c[2] == '0')		//如果d==0
	{
		cout << c[0];
		if (c[0] != '0')while (b--)cout << '0';
	}
	else if (b)		//如果d!=0,b!=0
	{
		bool flag = false;
		if (c[0] != '0')flag = true;
		if(flag)cout << c[0];		
		for (int i = 2; i < e; i++)
		{			
			if (!flag&&c[i] == '0')
			{
				if (i == 1 + b && i + 1 < e)
				{
					cout << "0.";
					flag = true;
				}
			}
			else
			{
				flag = true;
				cout << c[i];
				if (i == 1 + b && i + 1<e)cout << '.';
			}			
		}
		for (int i = e; i <= b + 1; i++)cout << '0';
	}
	else for (int i = 0; i < e; i++)cout << c[i];		//如果d!=0,b=0
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nameofcsdn/article/details/113801178