A:sort以后求差值最小
int a[100]; int main() { int n,m; cin>>n>>m; for(int i = 0 ; i < m ; i++) cin>>a[i]; sort(a,a+m); int mm = INF; for(int i = 0 ; i+n-1 < m ; i++) mm = min(mm,a[i+n-1]-a[i]); cout<<mm<<endl; return 0; }
B:求屏幕黑框占屏幕的几分之几 . 求LCM后的差值
LL gcd(LL a,LL b){ return b?gcd(b,a%b):a; } LL lcm(LL a,LL b){ return a*b/gcd(a,b); } int main() { LL a,b,c,d; LL a1,b1,c1,d1; cin>>a>>b>>c>>d; a1 = a; b1 = b; c1 = c; d1 = d; LL p = lcm(a,c); b = b * (p/a); a = p; d = d * (p/c); c = p; if(b > d) { LL x = b-d; LL y = b; printf("%I64d/%I64d ",x/gcd(x,y),y/gcd(x,y)); return 0; }else if(b == d) printf("0/1 "); LL q = lcm(b1,d1); a1 = a1 * (q/b1); b1 = q; c1 = c1 * (q/d1); d1 = q; if(a1 > c1){ LL x = a1-c1; LL y = a1; printf("%I64d/%I64d ",x/gcd(x,y),y/gcd(x,y)); return 0; } return 0; }