static bool cmp(string a, string b) { if (a.size() == b.size()) return a > b; else return a + b > b + a; }什么意思
时间: 2024-03-07 12:50:53 浏览: 35
这段代码定义了一个比较函数 `cmp`,该函数接受两个参数 `a` 和 `b`,它们都是字符串类型。该函数的作用是对字符串类型的数组进行排序。
函数内部的逻辑是:如果两个字符串的长度相等,则比较这两个字符串的字典序,返回字典序较大的那个字符串。如果两个字符串的长度不相等,则将它们拼接起来,比较拼接后的字符串的字典序,返回字典序较大的那个字符串。
这个比较函数的意义是,按照字典序对字符串数组进行排序,排序规则是把长度相等的字符串按照字典序逆序排列,长度不相等的字符串按照拼接后的字典序逆序排列。这个排序规则可以用来解决一些字符串问题,比如给定一组字符串,将它们按照组合起来的字典序最大的方式排列。
相关问题
#include<bits/stdc++.h> using namespace std; struct node { int st,et; }T[110]; bool cmp(node x,node y) { return x.et<y.et; } int n,ans,c; int main() { while(cin>>n&&n) { ans=1; for (int i=0;i<n;i++) cin>>T[i].st>>T[i].et; sort(T,T+n,cmp); c=T[0].et; for (int i=1;i<n;i++) { if (T[i].et>c&&T[i].st>=c) { ans++; c=T[i].et;; } } cout<<ans<<endl; } return 0; }转java
import java.util.Scanner;
import java.util.Arrays;
class Main {
static class Node {
int st, et;
Node(int st, int et) {
this.st = st;
this.et = et;
}
}
static int n, ans, c;
static Node[] T = new Node[110];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
n = sc.nextInt();
if (n == 0) break;
ans = 1;
for (int i = 0; i < n; i++) {
T[i] = new Node(sc.nextInt(), sc.nextInt());
}
Arrays.sort(T, 0, n, (x, y) -> Integer.compare(x.et, y.et));
c = T[0].et;
for (int i = 1; i < n; i++) {
if (T[i].et > c && T[i].st >= c) {
ans++;
c = T[i].et;
}
}
System.out.println(ans);
}
sc.close();
}
}
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn=10000+5; const double eps=1e-10; int dcmp(double x) { if(fabs(x)<eps) return 0; return x<0?-1:1; }struct Point { double x,y; int id; Point(){} Point(double x,double y,int id):x(x),y(y),id(id){} }P[maxn]; typedef Point Vector; Vector operator-(Point A,Point B) { return Vector(A.x-B.x,A.y-B.y,0); }double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; }bool cmp(Point A,Point B) { return dcmp(Cross(A-P[0],B-P[0]))<0; }int main() { int n; while(scanf("%d",&n)==1) { scanf("%lf%lf",&P[0].x,&P[0].y); P[0].id=1; for(int i=1;i<n;++i) { scanf("%lf%lf",&P[i].x,&P[i].y); P[i].id=i+1; if(P[i].x<P[0].x || (P[i].x==P[0].x && P[i].y<P[0].y) ) swap(P[0],P[i]); }sort(P+1,P+n,cmp); printf("%d %d\n",P[0].id,P[n/2].id); }return 0; }转为Java代码
import java.util.*;
import java.io.*;
class Main {
static final double eps = 1e-10;
static int n;
static Point[] P = new Point[10000 + 5];
static class Point {
double x, y;
int id;
public Point() {}
public Point(double x, double y, int id) {
this.x = x;
this.y = y;
this.id = id;
}
}
static int dcmp(double x) {
if (Math.abs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
static class Vector {
double x, y;
public Vector(double x, double y) {
this.x = x;
this.y = y;
}
}
static Vector minus(Point A, Point B) {
return new Vector(A.x - B.x, A.y - B.y);
}
static double Cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
}
static boolean cmp(Point A, Point B) {
return dcmp(Cross(minus(A, P[0]), minus(B, P[0]))) < 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
n = sc.nextInt();
P[0] = new Point(sc.nextDouble(), sc.nextDouble(), 1);
for (int i = 1; i < n; ++i) {
P[i] = new Point(sc.nextDouble(), sc.nextDouble(), i + 1);
if (P[i].x < P[0].x || (P[i].x == P[0].x && P[i].y < P[0].y))
swap(P, 0, i);
}
Arrays.sort(P, 1, n, Main::cmp);
System.out.println(P[0].id + " " + P[n / 2].id);
}
}
static void swap(Point[] P, int i, int j) {
Point temp = P[i];
P[i] = P[j];
P[j] = temp;
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)