115 条题解
-
0peter112358 LV 3 @ 2006-10-14 16:09:13
题目中说给定N个不同的0~9之间的数字……
ZSJZljw给的数据存在重复…… -
02006-10-13 19:24:15@
给不过的朋友一点提示:
4
3
10
0 1 7 5 4 4 5 7 8 9
4
5 1 1 3
3
3 0 0
2
1 0
看看你们能过这些数据? -
02006-09-29 22:17:09@
n为偶数,枚举两个数的第一位,然后构造
n为奇数,直接构造 -
02006-09-05 13:10:29@
很简单的题目,贪心就行了 要注意策略 不要忽视什么
-
02006-08-30 09:08:37@
数据有错吧?
怎么我的输出比标准的小点?
(骗了数据,一看果然是数据的问题) -
-12016-12-04 10:55:58@
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxa 10000
using namespace std;
int num[maxa],a[maxa],b[maxa];
void show(int *a,int n)
{
for(int i=0;i<n;++i)
printf("%d ",a[i]);
printf("\n");
}
int main()
{
int T,n,i,j,t;
scanf("%d",&T);
while(T--)
{
int ans = maxa;
memset(num,0,sizeof(num));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%d",&n);
for(i=0;i<n;++i)
scanf("%d",num+i);
if(n==2)
{
if(num[0]>num[1])
{
printf("%d\n",num[0]-num[1]);
}
else
printf("%d\n",num[1]-num[0]);
}
else
{
if(n%2==0){
sort(num,num+n);
if(num[0]==0)
i = 1;
else
i = 0;
for(j=i;j<n-1;++j)
if((num[j+1]-num[j])<ans)
{
ans = num[j+1]-num[j];
a[0] = num[j+1];
b[0] = num[j];
t =j;
}
num[t] = 0;
num[t+1] = 0;
sort(num,num+n);
t = 0;
for(j=2;j<=n;++j)
{
a[j-1] = num[j];
t++;
if(t==((n-2)/2))
break;
}
for(i=n-1;i>j;--i)
{
b[n-i] = num[i];
}
int x = 0,y =0;
for(i=0;i<n/2;++i)
x = x*10+a[i];
for(i=0;i<n/2;++i)
y =y*10+b[i];
printf("%d\n",x - y);
}
else
{
sort(num,num+n);
if(num[0]==0)
{
a[0] = num[1];
t = 1;
}
else
{
t = 0;
a[0] = num[0];
}
num[t] = 0;
for(i=1;i<=(n-1)/2;++i)
a[i] = num[i];
for(i=n-1;i>(n-1)/2;--i)
b[n-1-i] = num[i];
int x = 0,y =0;
for(i=0;i<n/2;++i)
x = x*10+a[i];
for(i=0;i<n/2;++i)
y =y*10+b[i];
printf("%d\n",x - y);
}
}
}
return 0;
} -
-12016-08-24 20:29:51@
思路和boyface1是一样的,给个代码参考。注意n==2的时候要特判,不然就只有10分,别问我为什么知道←_←
```c++
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;const int INF = 1000000000;
int n;
int num[11];int select (int k) {
int x = 0, y = 0;
x = num[k];
int cnt = n/2, cur = 0;
while (cnt) {
if (cur == k) { cur++; continue;}
x = x*10 + num[cur++];
cnt--;
}
cnt = n/2; cur = n-1;
while (cnt) {
if (cur == k) { cur--; continue;}
y = y*10 + num[cur--];
cnt--;
}
// cout << x << " " << y << " " << x - y << "\n";
return x - y;
}int select (int a, int b) {
int x = num[b], y = num[a];
int cnt = n/2-1, cur = 0;
while (cnt) {
if (cur == a || cur == b) { cur++; continue;}
x = x*10 + num[cur++];
cnt--;
}
cnt = n/2-1; cur = n-1;
while (cnt) {
if (cur == a || cur == b) { cur--; continue;}
y = y*10 + num[cur--];
cnt--;
}
// cout << x << " " << y << " " << x - y << "\n";
return x - y;
}int solve () {
int ans = INF;
sort(num, num+n);
if (n == 2) ans = num[1] - num[0];
if (n % 2 == 0) {
for (int i = num[0] == 0 ? 1 : 0; i+1 < n; i++)
ans = min(ans, select(i, i+1));
} else {
int k = 0;
while (num[k] == 0) k++;
ans = select(k);
}
return ans;
}int main ()
{
int T; cin >> T;
while (T--) {
cin >> n;
for (int i = 0; i < n; i++) cin >> num[i];
cout << solve() << "\n";
}
return 0;
}
``` -
-12016-08-06 16:40:11@
绝对值最小两个数 a,b
a,min,cmin
b,max,cmax -
-12016-07-31 16:18:58@
数据水到让我难以置信。
-
-12016-04-05 10:56:29@
先将n个数字从小到大排序,记排序后的序列为a[i],设这n个数字构成x和y两个数。
如果n为奇数(n=2k+1),那么将最小的非0数字作为x的最高位,然后依次从左往右取k位作为x的低位,从右往左取k位作为y,x-y即为答案。例如给定数字0,2,3,6,7,7,9,那么x=2036, y=977,x-y=1059。
如果n为偶数(n=2k),枚举非零数字a[i]作为x的最高位,a[i+1]作为y的最高位,从右往左取k-1位作为x的低位,从左往右取k-1位作为y的低位,更新答案。 -
-12016-02-23 12:50:58@
-
-12016-02-23 12:49:45@
-
-12016-01-04 16:55:00@
//已AC.
//不过写的乱七八糟的,i、j、k变量乱用。ㄟ(≧◇≦)ㄏ
#include <iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp(int a,int b);
int main()
{
int t,i=0,k,j,n,n1,n2,max,min,last,min2,num;
int a[10],b[10];
scanf("%d",&t);
while(t--)
{
i=0;
scanf("%d",&n);
for(i;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(a,a+n);if(n%2)//如果为奇数
{
sort(b,b+n,cmp);
if(a[0]==0) //a数组第一位不为0
{
for(i=1;i<n;i++)
if(a[i]>0)
{
a[i]^=a[0];
a[0]^=a[i];
a[i]^=a[0];
break;
}
}
n2=n1=(n+1)/2;
max=min=0;
for(i=0;i<n2;i++)
{
max+=a[i]*int(pow(10.0,n1-1)+0.1);
n1--;
}
n2=n1=n/2;
for(i=0;i<n2;i++)
{
min+=b[i]*int(pow(10.0,n1-1)+0.1);
// printf("%d\n",min);
n1--;
}
//printf("%d %d",max,min);
last=max-min;
printf("%d\n",last);
}
else
{
min=0;
if(n==2)
{
min=a[1]-a[0];
printf("%d\n",min);
}
else{
sort(b,b+n);
i=0;max=0,min=0;k=0;
n1=n/2;
int c[10],d[10];
for(i=0;i<10;i++)
{
c[i]=11;
}
i=0;
min2=c[0];
while(a[i]==0)
{
i++;
}
while(i!=n-1)
{
c[i]=a[i+1]-a[i];
if(c[i]<min2)
min2=c[i];
i++;
}
i=10;num=0;//num多少个权值最小
for(i=0;i<10;i++)
{
max=min=0;
if(c[i]==min2)
{
min+=b[i]*int(pow(10.0,n1-1)+0.1);
max+=b[i+1]*int(pow(10.0,n1-1)+0.1);
b[i]=b[i+1]=11;
sort(b,b+n);
n2=(n-2)/2;
for(j=0;j<(n-2)/2;j++)
{
max+=b[j]*int(pow(10.0,n2-1)+0.1);
n2--;
}
n2=(n-2)/2;
for(j=n-3;j>=(n-2)/2;j--)
{
min+=b[j]*int(pow(10.0,n2-1)+0.1);
n2--;
}
d[k]=max-min;
//printf("%d \n",d[k]);
k++;
for(j=0;j<n;j++)
{
b[j]=a[j];
}
}
}
last=d[0];
for(i=0;i<k;i++)
{
if(d[i]<last)
last=d[i];
}
printf("%d\n",last);
}
}}
return 0;
}
bool cmp(int a,int b)
{
return a>b;
} -
-12015-11-02 19:28:34@
NOIP 2015 赛前AC
var a:array[0..9]of boolean;
num:array[1..2,1..2]of longint;
n,tmp,m,tmp1,tmp2,t,count,i,j,k,min,minans:longint;
data:array[1..2,1..2]of longint;
special:boolean;
begin
readln(t);
for i:=1 to t do begin
special:=false;
readln(n);
fillchar(a,sizeof(a),false);
fillchar(num,sizeof(num),0);
fillchar(data,sizeof(data),0);
count:=0;
for j:=1 to n do begin
read(tmp);
if a[tmp] then a[tmp]:=false else a[tmp]:=true;
end;
for j:=0 to 9 do if a[j] then inc(count);
if (count=2) then begin
t:=0;
for j:=1 to 9 do if a[j] then inc(t);
if (t=1)and(count=2) then begin
for j:=1 to 9 do if a[j] then writeln(j);
special:=true;
end;
end;
if (count=1) then
for j:=0 to 9 do if a[j] then begin
writeln(j);
special:=true;
end;
if (count=0) then begin
writeln(0);
special:=true;
end;
min:=maxlongint;
minans:=maxlongint;
if (count mod 2=0)and not special then begin
fillchar(num,sizeof(num),0);
for j:=1 to 8 do if a[j] then
for k:=j+1 to 9 do if a[k] then if k-j<min then min:=k-j;
for j:=1 to 8 do if a[j] then
for k:=j+1 to 9 do if a[k] then if k-j=min then begin
tmp1:=j;
tmp2:=k;
t:=1;
for m:=0 to 9 do if (m<>j)and(m<>k)and a[m] then
if (t<=(count-2)div 2) then begin
tmp2:=tmp2*10+m;
inc(t);
end else break;
for m:=9 downto 0 do if (m<>j)and(m<>k)and a[m] then
if (t>(count-2) div 2)and(t<=count-2) then begin
tmp1:=tmp1*10+m;
inc(t);
end else break;
if tmp2-tmp1<minans then minans:=tmp2-tmp1;
end;
writeln(minans);
end;
if (count mod 2=1)and not special then begin
for j:=1 to 9 do if a[j] then begin
num[1,2]:=j;
data[1,2]:=j;
break;
end;
t:=1;
for j:=9 downto 0 do if (j<>data[1,2])and a[j] then
if (t<=(count-1)div 2) then begin
num[1,1]:=num[1,1]*10+j;
inc(t);
end else break;
t:=1;
for j:=0 to 9 do if (j<>data[1,2])and a[j] then
if (t<=(count-1)div 2) then begin
num[1,2]:=num[1,2]*10+j;
inc(t);
end else break;
writeln(num[1,2]-num[1,1]);
end;
end;
end.
end. -
-12013-02-16 10:15:49@