poj 2718 Smallest Difference 解题报告

链接:poj2718

Smallest Difference
**Time Limit:** 1000MS **Memory Limit:** 65536K
**Total Submissions:** 7259 **Accepted:** 1974

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.

For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

1

0 1 2 4 6 7

Sample Output

28

Source

[Rocky Mountain 2005](http://poj.org/searchproblem?field=source&key=Rocky+Mountain+2005)
一道题暴露了自己在c++方面字符串处理的薄弱,还有输入输出,在这里顺便先做个总结吧:
1.首先是所谓io的黑魔法一句话(关闭cin,cout缓冲区,慎用):
ios_base::sync_with_stdio(0);

std::ios::sync_with_stdio(false);

2.输入输出重定向:(最后两句可以不加)

freopen("date.in","r",stdin);  //重定向所有标准的输入为文件输入

freopen("date.out","w",stdout);//重定向所有标准的输出为文件输出

fclose(stdin);
fclose(stdout);//输出结束

下面是针对online judge 的处理:

#ifndef ONLINE_JUDGE 
    freopen("in.txt","r",stdin); 
    freopen("out.txt","w",stdout); 
#endif 

#ifndef ONLINE_JUDGE 
    fclose(stdin); 
    fclose(stdout); 
#endif
其实还可以在自己的编译参数内预先加上定义,比如
g++ $fullname -o $name -DIamDefine -Wall -std=gnu++0x -static -lm
然后,
#ifdef IamDefine
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif
 

3.读入整行(去除空格):

string line;
getline(cin,line);
line.erase(remove(line.begin(),line.end(),' '),line.end());
while ((ch=getchar())!='\n')
{
    if(ch==' ')continue;
    a[l++]=ch;
}

4.字符串,字符串数组和数字转换处理:

char a[10];
string temp=string(a);
int x=atoi(temp.substr(0,half).c_str());

另外还有atoi(),atol(),atof().等等.

最后提一点,判断字符时记得加 ‘ ‘ !!! char a[n], if(a[n]==1) ??? excuse me???

AC代码:

/*
* Filename:    poj2718.cpp
* Desciption:  穷竭搜索
* Created:     2016-03-07
*
*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<map>
#define INT_MAX 1<<30
using namespace std;
//typedef long long ll;
const int INF=0x7F;
int a[10];
int l,ans;
int getnum(int s,int e){
    int res=0;
    for (int i = s; i < e; i += 1)
    {
        res+=a[i];
        if(i!=e-1)res*=10;
    }
    return res;
}
void solve(){
    int half=l/2;
    int x,y;
    do
    {
        if((a[0]!=0||half==1)&&(a[half]!=0||l-half==1)){
            x=getnum(0,half);
            y=getnum(half,l);
            ans=min(ans,abs(x-y));
        }
    } while (next_permutation(a,a+l));

}
int main()
{
#ifdef FUCK_PROBLEM
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
#endif
    int t;
    scanf("%d ",&t);
    while (t--)
    {
        l=0;
        ans=100000;
        char ch;
        while ((ch=getchar())!='\n')
        {
            if(ch==' ')continue;
            a[l++]=ch-'0';
        }
        solve();
        printf("%d\n",ans);
    }
    return 0;
}

 

 


文章作者: crazyX
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 crazyX !
评论
 上一篇
bitset的使用(转) bitset的使用(转)
原文链接 有些程序要处理二进制位的有序集,每个位可能包含的是0(关)或1(开)的值。位是用来保存一组项或条件的yes/no信息(有时也称标志)的简洁方法。标准库提供了bitset类使得处理位集合更容易一些。要使用bitset类就必须要包含相
2016-03-09
下一篇 
生日快乐~ 生日快乐~
在地下室的我突然想起原来今天自己生日啊,今天好像还是啥女生节吧2333333…. 想了想,20年来第一次和出生那年一样,阴历是正月二十九,阳历3月7日,感觉好像很有意义的样子….. 大白天真的写不出来啥东西,就这么多吧. 对了!算法竞赛真的
2016-03-07
  目录