首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求把这段C++代码转换成java代码~不胜感激啊解决办法

2012-04-09 
求把这段C++代码转换成java代码~不胜感激啊#includeiostream#include cstdlib#include stdio.h#incl

求把这段C++代码转换成java代码~不胜感激啊
#include<iostream>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <sstream>
#define M 500
using namespace std;
char s[M];

struct Node {
  // int year, month, date;
  int hour, minutes, seconds;

  Node() {
  hour = minutes = seconds = 0;
  }

  bool operator<(Node a) {
  return hour * 60 * 60 + minutes * 60 + seconds < a.hour * 60 * 60 + a.minutes * 60 + a.seconds;
  }

  Node turn(Node a) {
  Node t;
  t.minutes = minutes - a.minutes;
  t.seconds = seconds - a.seconds;
  // t.date = date - a.date;
  // t.month = date - a.date;
  // t.year = year - a.year;
  t.hour = hour - a.hour;
  if (t.seconds < 0)t.seconds += 60, t.minutes--;
  if (t.minutes < 0)t.minutes += 60, t.hour--;
  t.minutes += t.seconds / 60;
  t.seconds %= 60;
  t.hour += t.minutes / 60;
  t.minutes %= 60;
  return t;
  // t.hour += t.date * 24 + t.month * 30 + t.year * 365;
  }

  void add(Node a) {
  minutes = minutes + a.minutes;
  seconds = seconds + a.seconds;
  hour = hour + a.hour;
  if (seconds < 0)seconds += 60, minutes--;
  if (minutes < 0)minutes += 60, hour--;
  minutes += seconds / 60;
  seconds %= 60;
  hour += minutes / 60;
  minutes %= 60;
  }
} f, t, z, now;

struct Team {
  char name[M];
  Node time[M];
  int num;
  Node ttime;
  int solve[M];

  Team() {
  memset(solve, 0, sizeof (solve));
  num = 0;
  }
} team[M];

int cmp(Team a, Team b) {
  if (a.num != b.num)
  return a.num > b.num;
  return a.ttime < b.ttime;
}
int pro[M];

int main(int argc, char** argv) {
  ifstream out;
  int id;
  char name[25];
  int problem;
  char state[25];
  char L[3][25];
  char tmp[50];
  int y, mon, d;
  int h, min, se;
  cin >> s;
  out.open(s);
  int num;
  bool ok = false;
  int n = 0;
  out >> tmp >> s;
  sscanf(s, "%d:%d:%d", &f.hour, &f.minutes, &f.seconds);
  out >> tmp >> s;
  sscanf(s, "%d:%d:%d", &t.hour, &t.minutes, &t.seconds);
  out.get();
  out.getline(s, 1000, '\n');
  stringstream sin(s);
  while (sin >> num) {
  pro[n] = num;
  n++;
  }
  int nn = 0;
  int pid;
  while (!out.eof()) {
  out.getline(s, 1000, '\n');
  sscanf(s, "%d %s %d %s %s %s %s %s %d:%d:%d", &id, &name, &problem, &state, &L[0], &L[1], &L[2], &tmp, &now.hour, &now.minutes, &now.seconds);
  for (int i = 0; i < n; i++) {
  if (pro[i] == problem) {
  num = i;
  break;
  }
  }
  pid = -1;
  for (int i = 0; i < nn; i++) {
  if (strcmp(team[i].name, name) == 0) {


  pid = i;
  }
  }
  if (pid == -1) {
  strcpy(team[nn].name, name);
  pid = nn;
  nn++;
  }
  if (team[pid].solve[num] != -1) {
  if (strcmp(state, "Accepted") == 0) {
  team[pid].solve[num] = -1;
  team[pid].time[num].add(now.turn(f));
  team[pid].num++;
  } else {
  team[pid].solve[num]++;
  team[pid].time[num].seconds += 20;
  }
  }
  }
  for (int i = 0; i < nn; i++) {
  for (int j = 0; j < n; j++) {
  team[i].time[j].add(z);
  if (team[i].solve[j] == -1) {
  team[i].ttime.add(team[i].time[j]);
  }
  }
  }
  sort(team, team + nn, cmp);
  cout << "Rank Name Solved";
  for (int i = 0; i < n; i++)
  cout << " " << pro[i];
  cout << " Penalty" << endl;
  for (int i = 0; i < nn; i++) {
  cout << i + 1 << " " << team[i].name << " " << team[i].num;
  for (int j = 0; j < nn; j++) {
  cout << " ";
  if (team[i].solve[j] == -1) {
  cout << team[i].time[j].hour << ":" << team[i].time[j].minutes << ":" << team[i].time[j].seconds;
  } else {
  cout << -team[i].solve[j];
  }
  }
  cout << " " << team[i].ttime.hour << ":" << team[i].ttime.minutes << ":" << team[i].ttime.seconds << endl;
  }
  out.close();

  return 0;
}



[解决办法]
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class test {
public static void main(String[] args) throws IOException, ParseException {
Team[] team = new Team[500];
for (int i = 0; i < 500; i++)
team[i] = new Team();

Node f = new Node();
Node t = new Node();
Node z = new Node();
Node now = new Node();

//File file = new File("E://te.txt");
RandomAccessFile in = new RandomAccessFile("E://input.in", "r");
char s[] = new char[500];
int pro[] = new int[500];

int id;
String name = "";
int problem;
String state = "";
String L[] = new String[3];
String tmp = "";
int y, mon, d;
int h, min, se;
// cin >> s;
// out.open(s);
int num = 0;
boolean ok = false;
int n = 0;
String temp = in.readLine();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");

Date startDate = dateFormat.parse(temp.substring(0, temp.length() / 2));
Date endDate = dateFormat.parse(temp.substring(temp.length() / 2));
f.setHour(startDate.getHours());
f.setMinutes(startDate.getMinutes());
f.setSeconds(startDate.getSeconds());
t.setHour(endDate.getHours());
t.setMinutes(endDate.getMinutes());
t.setSeconds(endDate.getSeconds());
temp = in.readLine();
String noList[] = temp.split(" ");


for (int i = 0; i < noList.length; i++)
pro[i] = Integer.parseInt(noList[i]);
n = noList.length;

int nn = 0;
int pid;
// in.close();

try {
while (true) {
temp = in.readLine();
String infoList[] = temp.split(" ");
id = Integer.parseInt(infoList[0]);
name = infoList[1];
problem = Integer.parseInt(infoList[2]);
state = infoList[3];
L[0] = infoList[4];
L[1] = infoList[5];
L[2] = infoList[6];
tmp = infoList[7];
Date subDate = dateFormat
.parse(infoList[8] + " " + infoList[9]);
now.setHour(subDate.getHours());
now.setMinutes(subDate.getMinutes());
now.setSeconds(subDate.getSeconds());
for (int i = 0; i < n; i++) {
if (pro[i] == problem) {
num = i;
break;
}
}
pid = -1;
for (int i = 0; i < nn; i++) {
if (team[i].getName().equals(name)) {
pid = i;
}
}

if (pid == -1) {
team[nn].name = name;
pid = nn;
nn++;
}
if (team[pid].solve[num] != -1) {
if (state.equals("Accepted")) {
team[pid].solve[num] = -1;
team[pid].time[num].add(now.turn(f));
team[pid].num++;
} else {
team[pid].solve[num]++;
team[pid].time[num].seconds += 20;
}
}
}
} catch (Exception e) {

System.out.println("读取结束!");
in.close();
}
for (int i = 0; i < nn; i++) {
for (int j = 0; j < n; j++) {
team[i].time[j].add(z);
if (team[i].solve[j] == -1) {
team[i].ttime.add(team[i].time[j]);
}
}
}
for (int x = 0; x < nn; x++)
for (int q = x; q < nn; q++) {
if (!Team.cmp(team[x], team[q])) {
Team te = team[x];
team[x] = team[q];
team[q] = te;

}

}
System.out.print("Rank Name Solved");
for (int i = 0; i < n; i++)
System.out.print(" " + pro[i]);
System.out.println(" Penalty");
for (int i = 0; i < nn; i++) {
System.out.print((i + 1) + " " + team[i].name + " " + team[i].num);
for (int j = 0; j < n; j++) {
System.out.print(" ");
if (team[i].solve[j] == -1) {
System.out.print(team[i].time[j].hour + ":"
+ team[i].time[j].minutes + ":"
+ team[i].time[j].seconds);
} else {
System.out.print(-team[i].solve[j]);
}
}
System.out.println(" " + team[i].ttime.hour + ":"
+ team[i].ttime.minutes + ":" + team[i].ttime.seconds);
}
// out.close();

return;
}

}

class Node {
public int hour, minutes, seconds;

Node() {
hour = minutes = seconds = 0;
}

public int getHour() {
return hour;
}

public void setHour(int hour) {
this.hour = hour;
}

public int getMinutes() {
return minutes;
}

public void setMinutes(int minutes) {
this.minutes = minutes;
}

public int getSeconds() {
return seconds;
}

public void setSeconds(int seconds) {
this.seconds = seconds;
}

public boolean operator(Node a) {
return hour * 60 * 60 + minutes * 60 + seconds < a.hour * 60 * 60
+ a.minutes * 60 + a.seconds;
}

public Node turn(Node a) {
// return a;

Node t = new Node();
t.minutes = minutes - a.minutes;
t.seconds = seconds - a.seconds;
// t.date = date - a.date;
// t.month = date - a.date;
// t.year = year - a.year;


t.hour = hour - a.hour;
if (t.seconds < 0) {
t.seconds += 60;
t.minutes--;
}

if (t.minutes < 0) {
t.minutes += 60;
t.hour--;

}
t.minutes += t.seconds / 60;
t.seconds %= 60;
t.hour += t.minutes / 60;
t.minutes %= 60;
return t;
// t.hour += t.date * 24 + t.month * 30 + t.year * 365;

}

public void add(Node a) {
minutes = minutes + a.minutes;
seconds = seconds + a.seconds;
hour = hour + a.hour;
if (seconds < 0) {
seconds += 60;
minutes--;
}

if (minutes < 0) {
minutes += 60;
hour--;

}
minutes += seconds / 60;
seconds %= 60;
hour += minutes / 60;
minutes %= 60;
}
}

class Team {
public String name = "";
public Node time[] = new Node[500];
public int num;
public Node ttime;
public int solve[] = new int[500];

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Node[] getTime() {
return time;
}

public void setTime(Node[] time) {
this.time = time;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

public Node getTtime() {
return ttime;
}

public void setTtime(Node ttime) {
this.ttime = ttime;
}

public int[] getSolve() {
return solve;
}

public void setSolve(int[] solve) {
this.solve = solve;
}

Team() {
for (int j = 0; j < 500; j++) {
time[j] = new Node();
}
ttime = new Node();
for (int i = 0; i < solve.length; i++) {
solve[i] = 0;
}
num = 0;
}

public static boolean cmp(Team a, Team b) {
if (a.num != b.num)
return a.num > b.num;
return a.ttime.operator(b.ttime);
}
}
//input.in放在E盘,测试内容为
2008-04-25 10:05:00 2008-04-25 20:00:00
1000 1001 1002 1003 1004 1005
12129 lzz 1000 Accepted 904K 10MS C++ 134B 2008-04-25 23:29:27
12130 lza 1001 Accepted 904K 10MS C++ 134B 2008-04-25 23:29:27
12131 lzb 1001 Accepted 904K 10MS C++ 134B 2008-04-25 23:29:26
12132 lzc 1000 Accepted 904K 10MS C++ 134B 2008-04-25 23:29:27
12133 lzc 1004 Accepted 904K 10MS C++ 134B 2008-04-25 23:29:27
12134 lzc 1005 Accepted 904K 10MS C++ 134B 2008-04-25 23:29:27

//输出结果为
读取结束!
Rank Name Solved 1000 1001 1002 1003 1004 1005 Penalty
1 lzc 3 13:24:27 0 0 0 13:24:27 13:24:27 40:13:21
2 lzb 1 0 13:24:26 0 0 0 0 13:24:26
3 lza 1 0 13:24:27 0 0 0 0 13:24:27
4 lzz 1 13:24:27 0 0 0 0 0 13:24:27

热点排行