现在的位置: 首页 > 综合 > 正文

模拟水题。。。

2017年10月16日 ⁄ 综合 ⁄ 共 2219字 ⁄ 字号 评论关闭

贴个题目,然后。。。。最近常常卡题。。。。。

可以先不debug吗。。。

Problem description

When Grace was in third grade, her elementary school teacher assigned her the following problem:

What is the smallest possible sum of two numbers that together use the numerals 1278, and 9?

Grace figured out that the answer to this problem is 207 (for example, as 78 + 129), but when the teacher assigned four pages of similar problems as homework, Grace got bored. It turns out that Grace was a rather advanced third grader, so she decided that
it would be more fun to write a computer program to solve such problems. Surely you can do the same!

Input

Input:  Each problem is described on a single line. The line begins with an integer N, such that 2 ≤ N ≤ 14, designating the number of numerals included in the problem. Following that are those N numerals.
There will always be at least 2 numerals that are nonzero. The end of the input is designated by a line containing only the value 0.

Output

Output:  For each case, output a line with the minimum sum S that can be achieved. Please keep in mind that by standard convention, the numeral 0 cannot appear as the first digit of either summand.

Sample Input
5 1 2 7 8 9
6 3 4 2 2 2 2
9 0 1 2 3 4 0 1 2 3
0
Sample Output
207
447
11257
/*
9 0 1 2 3 4 0 1 2 3
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 100
struct node
{
    int num;
    int a[maxn];
}map[2];
int aa,bb,a[maxn],b[maxn],c[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        aa=bb=0;
        map[0].num=map[1].num=0;
        int flag=0;
        int tmp=0,ans=0;
        int i,j,k;
        for(i=0;i<n;i++) scanf("%d",&a[i]);
        sort(a,a+n);
        for(i=0;i<n;i++)
        {
            map[flag].a[map[flag].num++]=a[i];
            flag=!flag;
        }
        for(i=0;i<map[flag].num;i++) {b[i]=map[flag].a[i];if(b[i]==0) tmp++;}
        for(i=0;i<map[!flag].num;i++) {c[i]=map[!flag].a[i];if(c[i]==0) ans++;}
        int t=map[flag].num,p=map[!flag].num;
        sort(b,b+t);
        sort(c,c+p);
      //  for(i=0;i<t;i++) printf("**%d",b[i]);printf("\n");for(i=0;i<p;i++) printf("**%d",c[i]);printf("\n");
      //  printf("tmp=%d  ans=%d\n",tmp,ans);
        for(i=0;i<t;i++)
        {
            if(i==0&&b[i]==0)
            {
                aa=b[i+tmp];
                i+=tmp;
             //   printf("aa=%d\n",aa);
                while(tmp) {aa*=10;tmp--;}//printf("*^^^\n");}
                continue;
            }
            aa=aa*10+b[i];
         //   printf("aa=%d\n",aa);
         //   printf("***\n");
        }
        for(i=0;i<p;i++)
        {
            if(i==0&&c[i]==0)
            {
                bb=c[i+ans];
                i+=ans;
          //      printf("bb=%d\n",bb);
                while(ans) {bb*=10;ans--;}
                continue;
            }
            bb=bb*10+c[i];
         //   printf("bb=%d\n",bb);
          //  printf("&&&&\n");
        }
        printf("%d\n",aa+bb);

    }
    return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.