115 条题解
-
3yyyz108 LV 8 @ 2017-11-09 09:27:51
借鉴各位dalao的
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; int t,n; int a[15]; int work1()//n为奇数 { int s1=0,s2=0; if(a[1]==0) swap(a[1],a[2]);//不能用0做最高位 for(int i=1;i<=(n+1)/2;i++) s1=s1*10+a[i];//选取非0最小数做最高位,从左往右取(n+1)/2位数 for(int i=n;i>=n/2+2;i--) s2=s2*10+a[i];//选取最大数做最高位,从右往左取n/2+2位数 return abs(s1-s2); } int work2()//n为偶数 //这段和奇数差不多,可自行理解 { int s1,s2,ans=(1<<31)-1; bool judge[15]; for(int i=2;i<=n;i++) { if(a[i-1]) { s2=a[i-1]; s1=a[i]; int l=1,r=n; memset(judge,false,sizeof(judge));//标记已用 judge[i-1]=judge[i]=true; for(int j=1;j<=(n-2)/2;j++) { while(judge[l]) l++; while(judge[r]) r--; judge[l]=judge[r]=true; s1=s1*10+a[l]; s2=s2*10+a[r]; } ans=min(ans,abs(s1-s2)); } } return ans; } int main() { cin>>t; while(t--) { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+1+n); if(n==2) {cout<<a[2]-a[1]<<endl; continue;}//特判 if(n%2==1) cout<<work1()<<endl; else cout<<work2()<<endl; } return 0; }
-
12017-03-04 18:24:53@
JRX2015U43:一道很麻烦的贪心题。
http://blog.csdn.net/qq_31640513/article/details/60335931 -
12016-08-31 14:46:43@
方晨羽——福利站(pascal)
评测状态 Accepted
题目 P1039 最小差距
递交时间 2016-08-31 14:44:21
代码语言 Pascal
评测机 ShadowShore
消耗时间 15 ms
消耗内存 812 KiB
评测时间 2016-08-31 14:44:22
```pascal
var
t,n,i,j:longint;
a:array[0..10] of longint;procedure qsort(aa,bb:longint);
var
i,j,x:longint;
begin
i:=aa;
j:=bb;
x:=a[(i+j) div 2];
repeat
while a[i]<x do inc(i);
while a[j]>x do dec(j);
if i<=j then
begin
a[0]:=a[i];
a[i]:=a[j];
a[j]:=a[0];
inc(i);
dec(j);
end;
until i>j;
if i<bb then qsort(i,bb);
if j>aa then qsort(aa,j);
end;procedure work1;
var
s1,s2,tip:string;
i,a1,a2:longint;
begin
s1:='';
s2:='';
if a[1]=0
then
begin
a[0]:=a[1];
a[1]:=a[2];
a[2]:=a[0];
end;
for i:=1 to n div 2+1 do
begin
str(a[i],tip);
s1:=s1+tip;
end;
for i:=n downto n div 2+2 do
begin
str(a[i],tip);
s2:=s2+tip;
end;
val(s1,a1);
val(s2,a2);
writeln(abs(a1-a2));
end;procedure work2;
var
ans,j,i,a1,a2,min,max:longint;
tip,s1,s2:string;
use:array[1..10] of boolean;
begin
ans:=maxlongint;
for i:=2 to n do
if a[i-1]<>0
then
begin
s1:='';
s2:='';
str(a[i],tip);
s1:=s1+tip;
str(a[i-1],tip);
s2:=s2+tip;
fillchar(use,sizeof(use),false);
use[i-1]:=true;
use[i]:=true;
min:=1;
max:=n;
for j:=1 to (n-2) div 2 do
begin
while use[min] do inc(min);
while use[max] do dec(max);
str(a[min],tip);
use[min]:=true;
s1:=s1+tip;
str(a[max],tip);
use[max]:=true;
s2:=s2+tip;
end;
val(s1,a1);
val(s2,a2);
if abs(a1-a2)<ans then ans:=abs(a1-a2);
end;
writeln(ans);
end;begin
readln(t);
for i:=1 to t do
begin
readln(n);
for j:=1 to n do read(a[j]);
qsort(1,n);
if n=2
then
writeln(abs(a[1]-a[2]))
else
begin
if n mod 2=1 then work1;
if n mod 2=0 then work2;
end;
end;
end.
``` -
12014-01-12 18:18:10@
wa而来somany次后过了
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n,a[10],big,small,ans,t,u[10];
int main()
{
int i,j,k;
cin>>t;
while (t) {
cin>>n;
for (i = 0;i < n;i++) {
cin>>a[i]; u[i] = 0;
}
sort(a,a+n);
if (n < 3) {
if (n == 2) cout<<a[1]-a[0]<<endl;
if (n == 1) cout<<a[0]<<endl;
t--;
continue;
}
if (n&1) {
if (a[0]) {
big = a[0]; u[0] = 1;
} else {
big = a[1]; u[1] = 1;
} small = 0; i = 0; j = n-1;
} else {
int min;
for (i = 0,min = 10;i < n-1;i++)
if ((min > a[i+1]-a[i]) && (a[i])) min = a[i+1]-a[i];
for (i = n/2-1;i < n-1;i++)
if ((min == a[i+1]-a[i]) && (a[i])) break;
j = i;
for (i = n/2-2;i >= 0;i--)
if ((min == a[i+1]-a[i]) && (a[i])) break;
if (j+1-n/2 < n/2-1-i) i = j;
big = a[i+1]; small = a[i]; u[i+1] = u[i] = 1;
i = 0; j = n-1;
}
while (i < j) {
if (!(u[i] || u[j])) {
big = big*10+a[i]; small = small*10+a[j];
u[i] = u[j] = 1;
}
if (u[i]) i++;
if (u[j]) j--;
}
cout<<big-small<<endl;
t--;
}
return 0;
} -
12009-10-08 15:10:44@
难度不高但是很烦很烦的题
分n=2 n>2(奇偶)做就是了 要仔细耐心啊~!
编译通过...
├ 测试数据 01:答案正确... 0ms
├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:答案正确... 0ms
├ 测试数据 05:答案正确... 0ms
├ 测试数据 06:答案正确... 0ms
├ 测试数据 07:答案正确... 0ms
├ 测试数据 08:答案正确... 0ms
├ 测试数据 09:答案正确... 0ms
├ 测试数据 10:答案正确... 0ms
---|---|---|---|---|---|---|---|-
Accepted 有效得分:100 有效耗时:0ms -
02021-11-23 05:00:35@
#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;
} -
02020-01-16 21:18:34@
248452688
-
02020-01-16 21:01:23@
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; int t,n; int a[15]; int work1() { int s1=0,s2=0; if(a[1]==0) swap(a[1],a[2]); for(int i=1;i<=(n+1)/2;i++) s1=s1*10+a[i]; for(int i=n;i>=n/2+2;i--) s2=s2*10+a[i]; return abs(s1-s2); } int work2() { int s1,s2,ans=(1<<31)-1; bool judge[15]; for(int i=2;i<=n;i++) { if(a[i-1]) { s2=a[i-1]; s1=a[i]; int l=1,r=n; memset(judge,false,sizeof(judge)); judge[i-1]=judge[i]=true; for(int j=1;j<=(n-2)/2;j++) { while(judge[l]) l++; while(judge[r]) r--; judge[l]=judge[r]=true; s1=s1*10+a[l]; s2=s2*10+a[r]; } ans=min(ans,abs(s1-s2)); } } return ans; } int main() { cin>>t; while(t--) { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+1+n); if(n==2) {cout<<a[2]-a[1]<<endl; continue;} if(n%2==1) cout<<work1()<<endl; else cout<<work2()<<endl; } return 0; }
-
02018-06-21 21:05:28@
表示wa了好多发才过
要注意单独讨论N==2,N>2分奇偶
还有思路一定要清晰,比较麻烦
#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int Pow(int n)
{
int ans=1;
for(int i=1;i<=n;i++)
ans*=10;
return ans;
}
int main()
{
int T,N,n;
int a[15];
int b[100];
int book[100];
int vis[100];
int c[15];
cin>>T;
while(T--)
{
N=0;
memset(a,0,sizeof a);
memset(book,0,sizeof book);
memset(vis,0,sizeof vis);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>b[i];
if(!book[b[i]])
{
a[++N]=b[i];
book[b[i]]=1;
}
}
int ans,_min=999999;
sort(a+1,a+N+1);
int u,v;
if(N==2)
{
ans=abs(a[1]-a[2]);
_min=ans;
}
else if(N%2==0)
{
int M=10;
int Min=9999999;
memcpy(c,a,sizeof a);
while(M--)
{
for(int i=1;i<=N;i++)
{
int flag=0;
for(int j=i+1;j<=N;j++)
if(abs(a[i]-a[j])<=Min&&a[i]&&a[j])
{
Min=abs(a[i]-a[j]);
u=i;
v=j;
if(!vis[u])
{
vis[u]=1;
flag=1;
break;
}
}//u<v,a[u]<a[v]
if(flag)
break;
}
int prod=a[v];
int prox=a[u];
a[u]=-1;
a[v]=-1;
sort(a+1,a+N+1);
prod*=Pow(N/2-1);
for(int i=3,k=N/2-2;i<=(N-2)/2+2;i++,k--)
prod+=a[i]*Pow(k);
sort(a+1,a+N+1,cmp);
prox*=Pow(N/2-1);
for(int i=1,k=N/2-2;i<=(N-2)/2;i++,k--)
prox+=a[i]*Pow(k);
ans=prod-prox;
_min=min(ans,_min);
memcpy(a,c,sizeof c);
}
}
else
{
int prod;
for(int i=1;i<=N;i++)
if(a[i])
{
prod=a[u=i];
break;
}
int prox=a[N];
a[u]=-1;
a[N]=-1;
sort(a+1,a+N+1);
prod*=Pow((N-1)/2);
for(int i=3,k=(N-1)/2-1;i<=(N-1)/2+2;i++,k--)
prod+=a[i]*Pow(k);
sort(a+1,a+N+1,cmp);
prox*=Pow((N-1)/2-1);
for(int i=1,k=(N-1)/2-2;i<=(N-1)/2-1;i++,k--)
prox+=a[i]*Pow(k);
ans=prod-prox;
_min=ans;
}
cout<<_min<<endl;
}
return 0;
} -
02015-07-13 17:49:26@
#include"iostream"
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int INF=2*3*4*5*6*7*8*9+1;
const int N=15;
int ans;
void dele(int num[],int n,int k)
{
for(int i=k;i<n-1;i++)
num[i]=num[i+1];
for(int i=k;i<n-2;i++)
num[i]=num[i+1];
}
int main()
{
int m;
scanf("%d",&m);
while(m)
{
int n;
scanf("%d",&n);
int num[N];
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
sort(num,num+n);
int mid=n/2;
if(n%2==0)
{
if(n==2)
{
ans=num[1]-num[0];
}
else
{
int k;
int d=10;
for(int i=(num[0]==0)?1:0;i<n-1;i++)
if(num[i+1]-num[i]<d)
{
k=i;
d=num[i+1]-num[i];
}int x=num[k];
int y=num[k+1];
dele(num,n,k);
int s=(n-2)/2;
for(int i=n-3;i>=s;i--)
{
x*=10;
x+=num[i];
}
for(int i=0;i<s;i++)
{
y*=10;
y+=num[i];
}
ans=y-x;
}
}
else
{
int x,y;
int k;
if(num[0]==0)
{
x=num[1];
k=1;
}
else
{
x=num[0];
k=0;
}
for(int i=0;i<=mid;i++)
{
if(i!=k)
{
x*=10;
x+=num[i];
}
}
y=0;
for(int j=n-1;j>mid;j--)
{
y*=10;
y+=num[j];
}
ans=x-y;
}
cout<<ans<<endl;
m--;
}
return 0;
}
只过了两个点,错哪了呢? -
02014-10-30 10:54:09@
NOIP2014赛前AC留念
(贪心有个清晰的思路很重要!!
用你的脑子去打好框架,然后实现.)
(刚开始思路太模糊WA4次,然后思路参考了题解就轻易AC了、、
所以比赛的时候还是要确定好思路啊)
var t,n,i,j:longint;
a:array[0..10] of longint;
procedure qsort(aa,bb:longint);
var i,j,x,temp:longint;
begin
i:=aa;
j:=bb;
x:=a[(i+j) div 2];
repeat
while a[i]<x do inc(i);
while a[j]>x do dec(j);
if i<=j then
begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
inc(i);
dec(j);
end;
until i>j;
if i<bb then qsort(i,bb);
if j>aa then qsort(aa,j);
end;procedure work1;
var s1,s2,tip:string;
temp,i,a1,a2:longint;
begin
s1:='';
s2:='';
if a[1]=0 then
begin
temp:=a[1];
a[1]:=a[2];
a[2]:=temp;
end;
for i:=1 to n div 2+1 do
begin
str(a[i],tip);
s1:=s1+tip;
end;
for i:=n downto n div 2+2 do
begin
str(a[i],tip);
s2:=s2+tip;
end;
val(s1,a1);
val(s2,a2);
writeln(abs(a1-a2));
end;procedure work2;
var ans,j,i,a1,a2,min,max:longint;
tip,s1,s2:string;
use:array[1..10] of boolean;begin
ans:=maxlongint;
for i:=2 to n do
if a[i-1]<>0 then
begin
s1:='';
s2:='';
str(a[i],tip);
s1:=s1+tip;
str(a[i-1],tip);
s2:=s2+tip;
fillchar(use,sizeof(use),false);
use[i-1]:=true;
use[i]:=true;
min:=1;
max:=n;
for j:=1 to (n-2) div 2 do
begin
while use[min] do inc(min);
while use[max] do dec(max);
str(a[min],tip);
use[min]:=true;
s1:=s1+tip;
str(a[max],tip);
use[max]:=true;
s2:=s2+tip;
end;
val(s1,a1);
val(s2,a2);
if abs(a1-a2)<ans then ans:=abs(a1-a2);
end;
writeln(ans);
end;begin
//assign(input,'t2.in');
//assign(output,'t2.out');
//reset(input);
//rewrite(output);
readln(t);
for i:=1 to t do
begin
readln(n);
for j:=1 to n do read(a[j]);
qsort(1,n);
if n=2 then writeln(abs(a[1]-a[2]))
else begin
if n mod 2=1 then work1;
if n mod 2=0 then work2;
end;
end;
//close(input);
//close(output);
end. -
02014-08-04 00:09:27@
program p1039;
var a:array[0..10] of boolean;
b:array[0..10] of longint;
n,sum,t,i:longint;
//
procedure init;
var i,h:longint;
begin
readln(n);
for i:=1 to n do
begin
read(h);
a[h]:=not(a[h]);
end;
readln;
end;
//
procedure make1;
var sum1,sum2,i:longint;
begin
if b[1]=0 then
begin
sum1:=b[2];
for i:=1 to n div 2+1 do
if i<>2 then
begin
sum1:=sum1*10+b[i];
end;
sum2:=b[n];
for i:=n-1 downto n div 2+2 do sum2:=sum2*10+b[i];
end;
if b[1]<>0 then
begin
sum1:=b[1];
for i:=2 to n div 2+1 do
begin
sum1:=sum1*10+b[i];
end;
sum2:=b[n];
for i:=n-1 downto n div 2+2 do
begin
sum2:=sum2*10+b[i];
end;
end;
sum:=abs(sum1-sum2);
end;
//
function min(a,b:longint):longint;
begin
if a>b then exit(b)
else exit(a);
end;
//
procedure make2;
var i,h,sum1,sum2,j,f:longint;
begin
h:=1;sum:=100000;
if b[h]=0 then inc(h);
for i:=h to n-1 do
begin
sum1:=b[i];sum2:=b[i+1];j:=1;f:=1;
while (f<=n div 2-1) do
begin
if j=i then inc(j);if j=i+1 then inc(j);
sum2:=sum2*10+b[j];inc(j);inc(f);
end;
f:=1;j:=n;
while (f<=n div 2-1) do
begin
if j=i+1 then dec(j);if j=i then dec(j);
sum1:=sum1*10+b[j];dec(j);inc(f);
end;
sum:=min(sum,abs(sum1-sum2));
end;
end;
//
procedure main;
var i,h1,h2:longint;
begin
n:=0;
for i:=0 to 10 do
if a[i] then
begin
inc(n);
b[n]:=i;
end;
if n=2 then sum:=b[n]-b[1]
else
begin
if n mod 2=1 then make1;
if n mod 2=0 then make2;
end;
writeln(sum);
end;
//
begin
assign(input,'p1039.in');assign(output,'p1039.out');
reset(input);rewrite(output);
readln(t);
for i:=1 to t do
begin
fillchar(a,sizeof(a),false);
init;
main;
end;
close(output);
end.
WA了一次。。。不好写的题 -
02014-03-16 12:01:40@
var ans,i,min,max,n,temp,t,x,y:longint;
a:array[0..10] of longint;
b:array[0..10] of boolean;
procedure jia(var num1,num2:longint);
begin
num2:=num2*10+num1;
end;
procedure work1;
var i:longint;
begin
x:=0;y:=0;
if a[1]=0 then begin temp:=a[1];a[1]:=a[2];a[2]:=temp;end;
for i:=1 to n div 2+1 do jia(a[i],x);
for i:=n downto n div 2+2 do jia(a[i],y);
writeln(x-y);
end;
procedure work2;
var i,j:longint;
begin
ans:=maxlongint;
for i:=n downto 2 do
if a[i-1]<>0 then
begin
x:=0;y:=0;
fillchar(b,sizeof(b),true);
b[i]:=false;b[i-1]:=false;
jia(a[i],x);jia(a[i-1],y);
min:=1;max:=n;
for j:=1 to n shr 1-1 do
begin
while not b[min] do inc(min);
while not b[max] do dec(max);
b[min]:=false;b[max]:=false;
jia(a[min],x);jia(a[max],y);
end;
if x-y<ans then ans:=x-y;
end;
writeln(ans);
end;
procedure tanxin;
var i,j:longint;
begin
readln(n);
fillchar(a,sizeof(a),0);
for i:=1 to n do read(a[i]);
for i:=1 to n-1 do
for j:=i+1 to n do
if a[j]<a[i] then
begin
temp:=a[i];a[i]:=a[j];a[j]:=temp;
end;
if n=2 then begin writeln(abs(a[2]-a[1]));exit;end
else if n mod 2 =1 then work1
else work2;
end;
begin
readln(t);
for i:=1 to t do tanxin;
end.
不容易啊! -
02013-11-26 18:10:32@
C语言,整整九十三行;
只得十分的很有可能是没有特判有只两个的情况
Sample Input 1
1
2
1 0
Sample Input 2
1
2
0 0
有点坑哈 -
02012-08-07 11:11:50@
太猥琐了,好不容易分析出来了,结果发现此题如此诡异。。。就过了三个点
program p1039;
var t,k,i,j,n,a1,a2,temp,left,min,mini,sum:longint;
s,s1,s2:string;
a,b:array[1..10] of integer;
v:array[1..10] of boolean;
begin
readln(t);
for k:=1 to t do
begin
readln(n);
fillchar(a,sizeof(a),0);
for i:=1 to n do
read(a[i]);
readln;
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then
begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
end;
if n=1 then writeln(a[1])
else if n=2 then writeln(a[2]-a[1])
else if (n mod 2=1) then
begin
s1:='';
s2:='';
fillchar(v,sizeof(v),true);
sum:=1;
while a[sum]=0 do inc(sum);
str(a[sum],s); v[sum]:=false;
s1:=s1+s;
for i:=1 to n do
if v[i] then begin str(a[i],s); v[i]:=false; s1:=s1+s;
if length(s1)=(n div 2+1) then break; end;
for i:=n downto 1 do
if v[i] then begin str(a[i],s); v[i]:=false; s2:=s2+s;
if length(s2)=(n div 2) then break; end;
val(s1,a1); val(s2,a2);
writeln(abs(a1-a2));
end
else begin
for i:=2 to n do
if a[i]=a then begin for j:=i to n do
a[j-1]:=a[j]; n:=n-1; end;
left:=1;
while a[left]=0 do inc(left);
for i:=left to n-1 do
b[i]:=a-a[i];
min:=maxlongint;
for i:=left to n-1 do
if b[i] -
02012-07-31 14:44:06@
注意题目中已经说明了各个数码是不相同的所以可以无视那个诡异的注释
然后要做的就是认真的分类讨论
首先读入数码,然后排序
当只有两个数码的时候是特殊情况,直接输出大的减去小的
其他情况大体思路是:[注意是大体思路,没有涵盖所有可能,但每种可能都很简单]
如果有奇数个数码,那么组成了[n/2]和[n/2]+1位数,使得[n/2]+1位数尽可能的小,[n/2]位数尽可能大,如果有零要安排在[n/2]+1位数的次高位上,比如0,1,2,3,5,7,9就要被组合成1023和975这种情况
如果是偶数个数码,那么枚举最高位(一定是排序后相邻的两个),然后使得大数尽可能小,小数尽可能大
比如:0,1,2,4,6,7就要枚举1,2;2,4;4,6;6,7是最高位的情况依次计算求出最小差值
——————————————————————
总结下要点有几个:
1、认真读题,如果没PASS掉注释说明少读了条件
2、分类讨论:一定不要怕麻烦,这是最稳妥的方法
3、定序:体现在排序读入数据和确定做差的两个数大小上面
4、贪心思想:大数尽可能小,小数尽可能大
——————————————————————
我写完之后是0ms过的,所以不用担心时间
另外那些贴AC系统反馈的什么心态嘛这里是题解喂
另另外呼吁不要贴代码,每个人代码风格不同没有充足的注释可读性非常差的 -
02012-07-12 23:31:45@
编译通过...
├ 测试数据 01:答案正确... 0ms
├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:答案正确... 0ms
├ 测试数据 05:答案正确... 0ms
├ 测试数据 06:答案正确... 41ms
├ 测试数据 07:答案正确... 275ms
├ 测试数据 08:答案正确... 509ms
├ 测试数据 09:答案正确... 494ms
├ 测试数据 10:答案正确... 603ms
---|---|---|---|---|---|---|---|-
Accepted 有效得分:100 有效耗时:1922ms效率不是很高,因为偶数个我用了搜索,奇数个我就写了个贪心.
-
02010-03-25 21:58:58@
编译通过...
├ 测试数据 01:答案正确... 0ms
├ 测试数据 02:答案正确... 0ms
├ 测试数据 03:答案正确... 0ms
├ 测试数据 04:答案正确... 0ms
├ 测试数据 05:答案正确... 0ms
├ 测试数据 06:答案正确... 0ms
├ 测试数据 07:答案正确... 0ms
├ 测试数据 08:答案正确... 0ms
├ 测试数据 09:答案正确... 0ms
├ 测试数据 10:答案正确... 0ms
---|---|---|---|---|---|---|---|-
Accepted 有效得分:100 有效耗时:0ms还不熟悉C++,第一次大小写搞错了,结果0分
-
02010-03-06 19:43:12@
var a,b,c:array[1..30]of longint;
i,j,n,t:longint;function cal1:longint;
var tt,i,j,ave,temp,p:integer;
ans1,ans2:longint;
begin
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
end;
if a[1]=0 then begin
p:=1;
while a[p]=0 do inc(p);
a[1]:=a[p];
a[p]:=0;
end;
ave:=n div 2+1;
ans1:=0;
ans2:=0;
for i:=1 to ave do
ans1:=ans1*10+a[i];
for i:=n downto ave+1 do
ans2:=ans2*10+a[i];
exit(ans1-ans2);
end;function cal2:longint;
var tt,i,j,ave,temp,p,x1,x2,max:integer;
ans1,ans2:longint;
begin
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then begin
temp:=a[i];
a[i]:=a[j];
a[j]:=temp;
end;
max:=11;
for i:=1 to n-1 do
if (a[i]0)and(a0) then
if a-a[i] -
02009-11-08 20:39:05@
题目的hint让人不明白。
如果是3个2,显然答案应该是20.
如果是1,1,9,7,0,2,答案应该是4,而非别的什么。