POJ 1002 我的代码为啥一直是Wrong Answer? 测试数据全部试过了,都能pass 35
importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Scanner;/****@author...
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Evan
* @date 2010/05/19
*/
public class Main {
public static String[] inputStrings;
public static ArrayList<String> outputStrings = new ArrayList<String>();
public static ArrayList<Integer> outputCounts = new ArrayList<Integer>();
/**
* format string
*
* @param args
*/
public static String formatInput(String inputstr) {
inputstr = inputstr.replaceAll("-", "");
char[] chars = new char[inputstr.length() + 1];
chars[3] = '-';
inputstr.getChars(0, 3, chars, 0);
inputstr.getChars(3, inputstr.length(), chars, 4);
for (int i = 0; i < chars.length; ++i) {
// if not digit then replace with map
if (i != 3) {
if (!Character.isDigit(chars[i])) {
if (chars[i] >= 'A' && chars[i] <= 'P') {
chars[i] = (char) ('0' + (chars[i] - 'A') / 3 + 2);
}
if (chars[i] >= 'R' && chars[i] <= 'Y') {
chars[i] = (char) ('5' + (chars[i] - 'Q') / 3 + 2);
}
}
}
}
return String.valueOf(chars);
}
/**
* filter and count
*/
public static void filterAndCount() {
for (int i = 0; i < inputStrings.length; ++i) {
String currStr = inputStrings[i];
int j;
for (j = i + 1; j < inputStrings.length; ++j) {
if (!inputStrings[j].equals(currStr)) {
if (j - i > 1) {
outputStrings.add(currStr);
outputCounts.add(j - i);
}
i = j - 1;
break;
}
}
// if all are equal
if (j == inputStrings.length && (j - i) > 1) {
outputStrings.add(currStr);
outputCounts.add(j - i);
i = j - 1;
}
}
}
/**
* print list
*/
public static void printResult() {
for (int i = 0; i < outputStrings.size(); ++i) {
System.out
.println(outputStrings.get(i) + " " + outputCounts.get(i));
}
}
/**
* main
*
* @param args
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int total = scan.nextInt();
inputStrings = new String[total];
int count = 0;
while (scan.hasNext()) {
String stdForm = formatInput(scan.next());
inputStrings[count] = stdForm;
++count;
if (count == total)
break;
}
// sort
Arrays.sort(inputStrings);
//
filterAndCount();
//
printResult();
}
} 展开
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Evan
* @date 2010/05/19
*/
public class Main {
public static String[] inputStrings;
public static ArrayList<String> outputStrings = new ArrayList<String>();
public static ArrayList<Integer> outputCounts = new ArrayList<Integer>();
/**
* format string
*
* @param args
*/
public static String formatInput(String inputstr) {
inputstr = inputstr.replaceAll("-", "");
char[] chars = new char[inputstr.length() + 1];
chars[3] = '-';
inputstr.getChars(0, 3, chars, 0);
inputstr.getChars(3, inputstr.length(), chars, 4);
for (int i = 0; i < chars.length; ++i) {
// if not digit then replace with map
if (i != 3) {
if (!Character.isDigit(chars[i])) {
if (chars[i] >= 'A' && chars[i] <= 'P') {
chars[i] = (char) ('0' + (chars[i] - 'A') / 3 + 2);
}
if (chars[i] >= 'R' && chars[i] <= 'Y') {
chars[i] = (char) ('5' + (chars[i] - 'Q') / 3 + 2);
}
}
}
}
return String.valueOf(chars);
}
/**
* filter and count
*/
public static void filterAndCount() {
for (int i = 0; i < inputStrings.length; ++i) {
String currStr = inputStrings[i];
int j;
for (j = i + 1; j < inputStrings.length; ++j) {
if (!inputStrings[j].equals(currStr)) {
if (j - i > 1) {
outputStrings.add(currStr);
outputCounts.add(j - i);
}
i = j - 1;
break;
}
}
// if all are equal
if (j == inputStrings.length && (j - i) > 1) {
outputStrings.add(currStr);
outputCounts.add(j - i);
i = j - 1;
}
}
}
/**
* print list
*/
public static void printResult() {
for (int i = 0; i < outputStrings.size(); ++i) {
System.out
.println(outputStrings.get(i) + " " + outputCounts.get(i));
}
}
/**
* main
*
* @param args
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int total = scan.nextInt();
inputStrings = new String[total];
int count = 0;
while (scan.hasNext()) {
String stdForm = formatInput(scan.next());
inputStrings[count] = stdForm;
++count;
if (count == total)
break;
}
// sort
Arrays.sort(inputStrings);
//
filterAndCount();
//
printResult();
}
} 展开
1个回答
展开全部
你注意 接收的那个字符串的必须开大 。 150 一上,给你我AC的代码把。但是是C++写的。代码写的不好。请见谅。
#include<iostream>
#include<stdio.h>
using namespace std;
class Init{
public :
int InitPhone(char phone[],int *value);
};
class _Sort_{
private:
int AdjustHeap(int data[],int n ,int start);
bool flag;
public :
int HeapSort ( int data[],int n );
int Clear ( int data[],int n );
_Sort_(){
flag = false ;
}
};
int main(){
int t,i;
int *data;
char phone[150];
Init creat;
_Sort_ DATA;
cin >> t;
data = new int [ t ] ;
for( i = 0 ; i < t ; i++ ){
cin >> phone ;
creat.InitPhone(phone,&data[i]);
}
DATA.HeapSort( data , t);
DATA.Clear( data , t );
delete [] data;
return 0;
}
int Init :: InitPhone(char phone[],int *value){
int i , j ;
char ch;
j = 1000000 ;
for( i = 0,*value = 0 ; phone[ i ] ; i++){
ch = phone[ i ] ;
if( ch >= '0' && ch <= '9' ){
*value += (ch - '0') * j;
j /= 10 ;
}else{
if( ch >= 'A' && ch <= 'Z'){
if(ch>='A'&&ch<='P')
*value += ((ch-'A')/3+2) * j;
if(ch>='R'&&ch<='Y')
*value += ( (ch-'A'-1)/3+2 ) * j;
j /= 10 ;
}
}
}
return 0;
}
int _Sort_ :: AdjustHeap(int data[],int n ,int start ){
int mid,i;
while( 2 *start + 1 < n ){
i = 2 *start + 1 ;
if( i + 1 < n ){
if( data[ i + 1 ] > data[ i ] )
i++;
}
if( data[start] < data[i ]){
mid = data[start];
data[start] = data[i];
data[i] = mid;
start = i;
}else{
break;
}
}
return 0;
}
int _Sort_ ::HeapSort ( int data[],int n ){
int i , mid;
for( i =( n - 1 ) / 2 ; i >= 0 ; i-- )
AdjustHeap(data,n,i);
for( i = n -1 ; i > 0 ; i -- ){
mid = data[ i ];
data[ i ] = data[0];
data[0] = mid;
AdjustHeap(data,i,0);
}
return 0;
}
int _Sort_ ::Clear(int data[],int n ){
int i ,j ,k;
for( i = 0 ; i < n ; ){
if( data[ i ] == data[ i+1] ){
for( j = 1 ; data[ i + j -1 ] == data[ i + j ] ; j++ );
this ->flag = true;
printf("%03d-%04d %d\n",data[i]/10000,data[i] %10000,j);
i += j;
}else{
i ++;
}
}
if( false == this ->flag )
cout<<"No duplicates."<<endl;
return 0;
}
#include<iostream>
#include<stdio.h>
using namespace std;
class Init{
public :
int InitPhone(char phone[],int *value);
};
class _Sort_{
private:
int AdjustHeap(int data[],int n ,int start);
bool flag;
public :
int HeapSort ( int data[],int n );
int Clear ( int data[],int n );
_Sort_(){
flag = false ;
}
};
int main(){
int t,i;
int *data;
char phone[150];
Init creat;
_Sort_ DATA;
cin >> t;
data = new int [ t ] ;
for( i = 0 ; i < t ; i++ ){
cin >> phone ;
creat.InitPhone(phone,&data[i]);
}
DATA.HeapSort( data , t);
DATA.Clear( data , t );
delete [] data;
return 0;
}
int Init :: InitPhone(char phone[],int *value){
int i , j ;
char ch;
j = 1000000 ;
for( i = 0,*value = 0 ; phone[ i ] ; i++){
ch = phone[ i ] ;
if( ch >= '0' && ch <= '9' ){
*value += (ch - '0') * j;
j /= 10 ;
}else{
if( ch >= 'A' && ch <= 'Z'){
if(ch>='A'&&ch<='P')
*value += ((ch-'A')/3+2) * j;
if(ch>='R'&&ch<='Y')
*value += ( (ch-'A'-1)/3+2 ) * j;
j /= 10 ;
}
}
}
return 0;
}
int _Sort_ :: AdjustHeap(int data[],int n ,int start ){
int mid,i;
while( 2 *start + 1 < n ){
i = 2 *start + 1 ;
if( i + 1 < n ){
if( data[ i + 1 ] > data[ i ] )
i++;
}
if( data[start] < data[i ]){
mid = data[start];
data[start] = data[i];
data[i] = mid;
start = i;
}else{
break;
}
}
return 0;
}
int _Sort_ ::HeapSort ( int data[],int n ){
int i , mid;
for( i =( n - 1 ) / 2 ; i >= 0 ; i-- )
AdjustHeap(data,n,i);
for( i = n -1 ; i > 0 ; i -- ){
mid = data[ i ];
data[ i ] = data[0];
data[0] = mid;
AdjustHeap(data,i,0);
}
return 0;
}
int _Sort_ ::Clear(int data[],int n ){
int i ,j ,k;
for( i = 0 ; i < n ; ){
if( data[ i ] == data[ i+1] ){
for( j = 1 ; data[ i + j -1 ] == data[ i + j ] ; j++ );
this ->flag = true;
printf("%03d-%04d %d\n",data[i]/10000,data[i] %10000,j);
i += j;
}else{
i ++;
}
}
if( false == this ->flag )
cout<<"No duplicates."<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询