1. 程式人生 > >poj - 1002題解

poj - 1002題解

排序 || 字符串 ostream ring pre cin ios str

題意:給定一個字符串,按一定格式處理後排序並輸出

題解:轉換成7位數字來處理即可,輸出的時候註意格式。

  1     #include<iostream>
  2     #include<algorithm>
  3     #include<string>
  4     using namespace std;
  5     long a[100001];
  6     bool com(const int &x,const int&y)
  7     {
  8         return x<y;
  9     }
 10
int main() 11 { 12 int n; 13 cin>>n; 14 for(int i=1;i<=n;i++) 15 { 16 string s; 17 cin>>s; 18 for(int j=0;j<s.length();j++) 19 { 20 if(s[j]==Q||s[j]==Z||s[j]==-) 21
{ 22 continue; 23 } 24 if(s[j]==A||s[j]==B||s[j]==C) 25 { 26 s[j]=2; 27 } 28 if(s[j]==D||s[j]==E||s[j]==F) 29 { 30 s[j]=
3; 31 } 32 if(s[j]==G||s[j]==H||s[j]==I) 33 { 34 s[j]=4; 35 } 36 if(s[j]==J||s[j]==K||s[j]==L) 37 { 38 s[j]=5; 39 } 40 if(s[j]==M||s[j]==N||s[j]==O) 41 { 42 s[j]=6; 43 } 44 if(s[j]==P||s[j]==R||s[j]==S) 45 { 46 s[j]=7; 47 } 48 if(s[j]==T||s[j]==U||s[j]==V) 49 { 50 s[j]=8; 51 } 52 if(s[j]==W||s[j]==X||s[j]==Y) 53 { 54 s[j]=9; 55 } 56 } 57 long k=0; 58 long m=1; 59 for(int j=s.length()-1;j>=0;j--) 60 { 61 if(s[j]==-||s[j]==Q||s[j]==Z) 62 { 63 continue; 64 } 65 else 66 { 67 k+=((int)s[j]-(int)0)*m; 68 m*=10; 69 } 70 } 71 a[i]=k; 72 } 73 sort(a+1,a+n+1,com); 74 bool check=true; 75 int t=0; 76 int count=1; 77 for(int i=2;i<=n+1;i++) 78 { 79 if(i<=n&&a[i]==a[i-1]) 80 { 81 t=a[i]; 82 count++; 83 } 84 else 85 { 86 if(count>=2) 87 { 88 int k=a[i-1]; 89 int t1=k/10000; 90 cout<<t1/100; 91 cout<<(t1-t1/100*100)/10; 92 cout<<t1%10; 93 cout<<"-"; 94 t1=k%10000; 95 cout<<t1/1000; 96 t1=t1-t1/1000*1000; 97 cout<<t1/100; 98 t1=t1-t1/100*100; 99 cout<<t1/10; 100 cout<<t1%10; 101 cout<<" "<<count<<endl; 102 check=false; 103 } 104 count=1; 105 } 106 } 107 if(check) 108 { 109 cout<<"No duplicates."; 110 } 111 return 0; 112 }

poj - 1002題解