NOJ coder的音乐烦恼--字符串处理
http://acm.nit.net.cn/showproblem.jsp?pid=1366


View Code
1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 #include<cmath> 5 6 using namespace std; 7 8 int const ic_max = 5001; 9 10 struct stuNode 11 { 12 int iPriority;//优先级 13 string strSong;//歌曲名 14 }; 15 16 void vInputData(int iN,stuNode stuSong[]); 17 void vGetData(string strT,stuNode& stuSong); 18 bool bCmp(const stuNode& stuA,const stuNode& stuB); 19 void vPrintAns(int iN,stuNode stuSong[]); 20 21 int main() 22 { 23 int iNum; 24 stuNode stuSong[ic_max]; 25 26 while(cin >> iNum) 27 { 28 vInputData(iNum,stuSong); 29 sort(stuSong+1,stuSong+1+iNum,bCmp); 30 vPrintAns(iNum,stuSong); 31 } 32 33 return 0; 34 } 35 36 void vInputData(int iN,stuNode stuSong[]) 37 { 38 string strTemp; 39 40 getchar(); 41 for(int i=1; i<=iN; i++) 42 { 43 getline(cin,strTemp);//默认/n 44 vGetData(strTemp,stuSong[i]); 45 } 46 } 47 48 void vGetData(string strT,stuNode& stuSong) 49 { 50 int iPrio; 51 int iBlank;//标记最后一个空格的位置 52 string strPrio;//获取优先级的字符串形式 53 54 iPrio = 0; 55 //获取最后一个空格的位置 56 for(int i=strT.size()-1; i>=0; i--) 57 { 58 if(strT[i] == ' ') 59 { 60 iBlank = i; 61 break; 62 } 63 } 64 //截取相应子段 65 stuSong.strSong = strT.substr(0,iBlank); 66 strPrio = strT.substr(iBlank+1,strT.size()-iBlank); 67 //将字符串转换为数字 68 for(int i=strPrio.size()-1; i>=0; i--) 69 { 70 if(strPrio[i] == '-') 71 { 72 iPrio *= -1; 73 } 74 iPrio += (strPrio[i]-'0') * (int)pow(10.0,1.0*(strPrio.size()-1-i)); 75 } 76 stuSong.iPriority = iPrio; 77 } 78 79 bool bCmp(const stuNode& stuA,const stuNode& stuB) 80 { 81 return stuA.iPriority < stuB.iPriority; 82 } 83 84 void vPrintAns(int iN,stuNode stuSong[]) 85 { 86 int iTotal; 87 88 iTotal = iN; 89 //获取优先级<=0的个数 90 for(int i=1; i<=iN; i++) 91 { 92 if(stuSong[i].iPriority <= 0) 93 iTotal --; 94 else 95 break; 96 } 97 cout << iTotal << endl; 98 for(int i=iN-iTotal+1; i<=iN; i++) 99 { 100 cout << stuSong[i].strSong << endl; 101 } 102 }

浙公网安备 33010602011771号