1 条题解
-
0
C++ :
#include <iostream> #include <algorithm> #include <cstring> #include <fstream> using namespace std; int tian[1001]; int qi[1001]; bool cmp(int a,int b) { return a>b; } int main() { int horse,n; //ifstream cin; //ofstream cout; //cin.open("d.in"); //cout.open("d.out"); cin>>n; while(n--) { cin>>horse; int tianbest,tianworst,qibest,qiworst; int counter=0; if(horse==0) break; for(int i=0;i<horse;i++) cin>>tian[i]; for(int j=0;j<horse;j++) cin>>qi[j]; sort(tian,tian+horse,cmp); sort(qi,qi+horse,cmp); tianbest=0; qibest=0; tianworst=horse-1; qiworst=horse-1; int dui=0; while(dui++<horse) { if(tian[tianbest]>qi[qibest]) { counter+=200; tianbest++; qibest++; } else if(tian[tianbest]<qi[qibest]) { counter-=200; tianworst--; qibest++; } else if(tian[tianbest]==qi[qibest]) { if(tian[tianworst]>qi[qiworst]) { counter+=200; tianworst--; qiworst--; } else { if(tian[tianworst]<qi[qibest]) { counter-=200; } tianworst--; qibest++; } } } if(counter>=0) { cout<<"Xiaoming will get "<<counter<<"QB"<<endl; } else { cout<<"Xiaotao will get "<<-counter<<"QB"<<endl; } //cout<<counter<<endl; } return 0; }
Java :
import java.util.Scanner; class Main<AnyType> { Node firstNode=new Node(0); Node secondNode=new Node(0); int sizeA=1; int sizeB=1; public int getSizeA() { return sizeA; } public int getSizeB() { return sizeB; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader = new Scanner(System.in); int T = reader.nextInt(); for (int i = 0; i < T; i++) { int m = reader.nextInt(); Main test = new Main(); double[] a = new double[m]; double[] b = new double[m]; for (int j = 0; j < m; j++) { a[j] = reader.nextDouble(); } for (int j = 0; j < m; j++) { b[j] = reader.nextDouble(); } test.sort(a); test.sort(b); test.create1(test.firstNode, a); test.create2(test.secondNode, b); int sum = test.abcd(); if (sum >= 0) { System.out.println("Xiaoming will get " + sum + "QB"); } else { System.out.println("Xiaotao will get " + Math.abs(sum) + "QB"); } } } // 计算sum并返回 public <AnyType extends Comparable<? super AnyType>> int abcd( ) { int sum = 0; while (firstNode.next!=null) { Node p=firstNode.next; Node q=secondNode.next; if (p.data > q.data) { sum=sum+200; remove1(1); remove2(1); }if (p.data == q.data) { Node c=getNode(getSizeA(),firstNode); Node d=getNode(getSizeB(),secondNode); if(c.data<=d.data&&c.data!=p.data){ remove1(1); remove2(getSizeB()-1); sum=sum-200; }else if(c.data>d.data){ remove1(getSizeA()-1); remove2(getSizeB()-1); sum=sum+200; }else if(c.data==d.data&&c.data==p.data){ remove1(getSizeA()-1); remove2(getSizeA()-1); } }if (p.data < q.data) { sum=sum-200; remove1(1); remove2(getSizeB()-1); } } return sum; } //获得idx位置的节点 public Node getNode(int idx,Node node){ Node p=node; for(int i=0;i<idx-1;i++){ p=p.next; } return p; } // 删除单链表中的元素 public void remove1(int idx){ Node p=firstNode; for(int i=0;i<idx-1;i++){ p=p.next; } Node node=p.next.next; p.next=node; sizeA--; } public void remove2(int idx){ Node p=secondNode; for(int i=0;i<idx-1;i++){ p=p.next; } Node node=p.next.next; p.next=node; sizeB--; } // 对数组进行构造单链表 public void create1(Node node, double[] a) { Node p = node; for (int i = 0; i < a.length; i++) { p.next = new Node(a[i]); p = p.next; sizeA++; } } public void create2(Node node, double[] a) { Node p = node; for (int i = 0; i < a.length; i++) { p.next = new Node(a[i]); p = p.next; sizeB++; } } // 对输入的数组进行排序 public void sort(double[] a) { for (int i = 0; i < a.length; i++) { for (int j = i; j < a.length - 1; j++) { if (a[j + 1] < a[i]) { double c = a[j + 1]; a[j + 1] = a[i]; a[i] = c; } } } } // 节点类 private static class Node { double data; Node next; Node() { } Node(double date) { this.data = date; this.next = null; } public Node(double d, Node n) { this.data = d; this.next = n; } } }
- 1
信息
- ID
- 3371
- 时间
- 2000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者