链接: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 7Sample Output
28Source
[Rocky Mountain 2005](http://poj.org/searchproblem?field=source&key=Rocky+Mountain+2005)
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; }