给定数字 0-9 各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意 0 不能做首位)。例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558。
现给定数字,请编写程序输出能够组成的最小的数。
输入格式:
输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0、数字 1、……数字 9 的个数。整数间用一个空格分隔。10 个数字的总个数不超过 50,且至少拥有 1 个非 0 的数字。
输出格式:
在一行中输出能够组成的最小的数。
输入样例:
2 2 0 0 0 3 0 0 1 0
输出样例:
10015558
#include<cstdio> int count[10]; int main(){ for(int i=0;i<10;i++){ scanf("%d",&count[i]); } for(int i=1;i<10;i++){ if(count[i]>0){ printf("%d",i); count[i]--; break; } } for(int i=0;i<10;i++){ while(count[i]!=0){ printf("%d",i); count[i]--; } } return 0; }
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.
Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (≤), the number of different kinds of mooncakes, and D (≤ thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.
Sample Input:
3 200
180 150 100
7.5 7.2 4.5
Sample Output:
9.45
#include<cstdio> #include<algorithm> using namespace std; struct mooncake{ double store; double money; double price; }moon[1010]; bool cmp(mooncake a,mooncake b){ return a.price > b.price; } int main(){ int n; double d; scanf("%d%lf",&n,&d); for(int i=0;i<n;i++){ scanf("%lf",&moon[i].store); } for(int i=0;i<n;i++){ scanf("%lf",&moon[i].money); moon[i].price = moon[i].money / moon[i].store; } sort(moon,moon+n,cmp); double ans=0.0; for(int i=0;i<n;i++){ if(moon[i].store <= d){ d = d - moon[i].store; ans = ans + moon[i].money; }else{ ans = ans + moon[i].price * d; break; } } printf("%.2f",ans); return 0; }
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product... but hey, magically, they have some coupons with negative N's!
For example, given a set of coupons { 1 2 4 − }, and a set of product values { 7 6 − − } (in Mars dollars M$) where a negative value corresponds to a bonus product. You can apply coupon 3 (with N being 4) to product 1 (with value M$7) to get M$28 back; coupon 2 to product 2 to get M$12 back; and coupon 4 to product 4 to get M$3 back. On the other hand, if you apply coupon 3 to product 4, you will have to pay M$12 to the shop.
Each coupon and each product may be selected at most once. Your task is to get as much money back as possible.
Input Specification:
Each input file contains one test case. For each case, the first line contains the number of coupons NC, followed by a line with NC coupon integers. Then the next line contains the number of products NP, followed by a line with NP product values. Here 1, and it is guaranteed that all the numbers will not exceed 230.
Output Specification:
For each test case, simply print in a line the maximum amount of money you can get back.
Sample Input:
4
1 2 4 -1
4
7 6 -2 -3
Sample Output:
43
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 100010; int coupon[maxn],product[maxn]; int main(){ int nc,np; scanf("%d",&nc); for(int i=0;i<nc;i++){ scanf("%d",&coupon[i]); } scanf("%d",&np); for(int i=0;i<np;i++){ scanf("%d",&product[i]); } sort(coupon,coupon+nc); sort(product,product+np); long long ans = 0; int j=0,i=0; while(i<nc && j<np && coupon[i]<0 && product[j] <0 ){ ans += coupon[i] * product[j]; i++; j++; } i = nc - 1; j = np - 1; while(i>=0 && j>=0 && coupon[i]>0 && product[j]>0){ ans += coupon[i] * product[j]; i--; j--; } printf("%d ",ans); return 0; }
Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the smallest number in one line. Notice that the first digit must not be zero.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
#include<cstdio> #include<iostream> #include<algorithm> #include<string> using namespace std; const int maxn = 10010; string str[maxn]; bool cmp(string a,string b){ return a+b < b+a; } int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ cin >> str[i]; } sort(str,str+n,cmp); string ans; for(int i=0;i<n;i++){ ans += str[i]; } while(ans.size() != 0 && ans[0]=='0'){ ans.erase(ans.begin()); } if(ans.size()==0) cout<<0; else cout<<ans; return 0; }