Codeforces Helpful Maths

Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.

The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn‘t enough for Xenia. She is only beginning to count, so she can calculate a sum
only if the summands follow in non-decreasing order. For example, she can‘t calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.

You‘ve got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.

Input

The first line contains a non-empty string s — the sum Xenia needs to count. String s contains
no spaces. It only contains digits and characters "+". Besides, string s is
a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long.

Output

Print the new sum that Xenia can count.

Sample test(s)

input

3+2+1

output

1+2+3

input

1+1+3+1+3

output

1+1+1+3+3

input

2

output

2

这样的题目因为关键字少,所以就可以转换为counting sort的思想去解决。这样时间效率就只有O(n)了。

void HelpfulMaths()
{
	int A[4] = {0};
	int a;
	while (scanf("%d", &a) != EOF)
	{
		A[a]++;
		getchar();
	}
	int total = A[1]+A[2]+A[3]-1;
	for (unsigned i = 0; i < A[1]; i++, total--)
	{
		printf("1");
		if (total) printf("+");
	}
	for (unsigned i = 0; i < A[2]; i++, total--)
	{
		printf("2");
		if (total) printf("+");;
	}
	for (unsigned i = 0; i < A[3]; i++, total--)
	{
		printf("3");
		if (total) printf("+");
	}
}

Codeforces Helpful Maths,码迷,mamicode.com

时间: 2024-11-10 02:50:10

Codeforces Helpful Maths的相关文章

Codeforces Round #197 (Div. 2) A. Helpful Maths【字符串/给一个连加计算式,只包含数字 1、2、3,要求重新排序,使得连加的数字从小到大】

A. Helpful Maths time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.

codeforces水题100道 第二十五题 Codeforces Round #197 A. Helpful Maths (Div. 2) (strings)

题目链接:http://www.codeforces.com/problemset/problem/339/A题意:重新组合加法字符串,使得按照1,2,3的顺序进行排列.C++代码: #include <iostream> #include <string> using namespace std; int cnt[3]; string s, ans = ""; int main() { cin >> s; int len = s.length();

#197 (div.2) A. Helpful Maths

1.题目描述:点击打开链接 2.解题思路:抽取出所有的数字,排序后输出即可. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #include<map

Codeforces Round #260 (Div. 2) B. Fedya and Maths(循环节)

题目链接:http://codeforces.com/problemset/problem/456/B B. Fedya and Maths time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Fedya studies in a gymnasium. Fedya's maths hometask is to calculate t

Codeforces Round #554 (Div. 2) 1152C. Neko does Maths

学了这么久,来打一次CF看看自己学的怎么样吧 too young too simple 1152C. Neko does Maths 题目链接:"https://codeforces.com/contest/1152/problem/C" 题目大意:给你两个数a,b,现在要你找出一个数k使得(a+k)和(b+k)的最小公倍数最小. 题目思路:暴力(逃) 这题没得思路,想了想既然求LCM了那么和GCD说不定有点关系 然后就没有然后了 比赛的时候交了一发暴力上去,然并软 赛后补题,题解里面

Codeforces Round #287 D.The Maths Lecture

The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可以用 f[i+1][(t*10i+j)%k]=∑f[i][j]+(j==0),(t*10i+j)%k!=0;动态规划 这样可以求出所有f[n][i] i>0 的值. 最后用9*10^(n-1)-∑f[n][i] 就可以得到 答案 #include <bits/stdc++.h> using

Codeforces Round #554 (Div. 2) C.Neko does Maths (gcd的运用)

题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给定两个正整数a,b,其中(1<=a,b<=1e9),求一个正整数k(0<=k),使得a+k与b+k的最小公倍数最小. 解题思路:首先我们需要知道gcd(a,b)=gcd(a,b-a)=gcd(b,b-a)(b>a)的 我们要求的是lcm(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b-a) 因为b-a是定值,所

Codeforces . C.Neko does Maths

题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Neko loves divisors. During the latest number theory lesson, he got an interesting exercise from his math teacher. N

Codeforces Round #287 Div2 D(The Maths Lecture)

Problem 长度为N的数X(十进制),如果X的某一段后缀Y(十进制)可被k整除,则可被统计.问有多少这样的X?(mod m)(不可含前导0) Limits TimeLimit(ms):1000 MemoryLimit(MB):256 N:∈[1,1000] k∈[1,100] m∈[1,109] Look up Original Problem From here Solution 数位dp.设dp[i][j]表示数长度为 i 且数mod k 为 j时, 有多少个.dp[i][0]不要往前转