Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

老是WA,大虾帮忙看看好吗?

Posted by sevenever at 2007-03-13 21:36:20 on Problem 1002
思路就是把输入字符串转为整形,放入数组,然后快速排序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#undef _DEBUG


int map[26];
int num;
int * array;



void init();
int parse();
static int comp(const void * a,const void *b);
void print();
void process();

int main()
{
  char s[32];
  int tmp;
  int i;
  

  scanf("%d",&num);
#ifdef _DEBUG
  printf("%d\n",num);
#endif


  init();
  
  i=0;
  
  bzero(s,32);
  while(scanf("%s",s)!=EOF){
#ifdef _DEBUG
    printf("%s\t",s);
    
    printf("%d\n",parse(s));
#endif //_DEBUG
    
    tmp=parse(s);
    array[i++]=tmp;
  	bzero(s,32);
  }
#ifdef _DEBUG
    printf("i=%d\tnum=%d\n",i,num);
#endif //_DEBUG

    //sort
    qsort((void *)array,num,sizeof(int),comp);

    print();
    
    free(array);
    

  return 0;
  
  
}
//转换为整数
int parse(char * s)
{
  char *p;
  int result;
  
  p=s;
  result=0;
  
  while(*p){
    if(isdigit(*p)){
      result=result*10 + *p - '0';
    }
    else if(isupper(*p)){
      result=result*10 + map[(*p)-'A'];
    }
    p++;
  }
  return result;
}

//初始化映射表和分配内存
void init()
{
  map['A'-'A']=2;
  map['B'-'A']=2;
  map['C'-'A']=2;
  map['D'-'A']=3;
  map['E'-'A']=3;
  map['F'-'A']=3;
  map['G'-'A']=4;
  map['H'-'A']=4;
  map['I'-'A']=4;
  map['J'-'A']=5;
  map['K'-'A']=5;
  map['L'-'A']=5;
  map['M'-'A']=6;
  map['N'-'A']=6;
  map['O'-'A']=6;
  map['P'-'A']=7;
  map['Q'-'A']=7;
  map['R'-'A']=7;
  map['S'-'A']=7;
  map['T'-'A']=8;
  map['U'-'A']=8;
  map['U'-'A']=8;
  map['W'-'A']=9;
  map['X'-'A']=9;
  map['Y'-'A']=9;
  map['Z'-'A']=9;

  array=malloc(num*sizeof(int));
  
}
static
int comp(const void * a,const void * b)
{
  return *((int *)a)-*((int *)b);
}
//打印出已经排序的数组
void print()
{
  int i;
  int count;
  int flag;
  


  count=0;
  flag=0;
  
  for(i=0;i<num-1;i++){
    if(array[i]!=array[i+1]){
      if(count > 0){
	flag=1;
	
        printf("%03d-%04d %d\n",array[i]/10000,array[i]%10000,count+1);
      }
      
      count=0;
      
      
    }else{
      count++;
    }
    
  }
  if(count>0){
    flag=1;
    
        printf("%03d-%04d %d\n",array[i]/10000,array[i-1]%10000,count+1);
  }

  if(!flag){
    printf("No duplicates.\n");
  }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator