成绩排序
字符串排序#include <iostream> #include<string> using namespace std; struct result { char name[20]; int scores; }; int rankresult(result*); int number; void swapstruct(result & s1,result & s2) { result temp; temp=s1; s1=s2; s2=temp; } int main() { int pattern; cin>>number; cin>>pattern; result *people=new result[number]; for (int i=0;i<number;i++) { cin>>people[i].name>>people[i].scores; if (people[i].scores<0||people[i].scores>100) { return -1; } } rankresult(people); if (pattern==0) { for (int i=0;i<number;i++) { cout<<people[i].name<<" "<<people[i].scores<<endl; } } else if(pattern==1) { for (int i=number-1;i>=0;i--) { cout<<people[i].name<<" "<<people[i].scores<<endl; } } delete[]people; return 0; } int rankresult(result* people) { for (int i=0;i<number;i++) { for (int j=0;j<number-1-i;j++) { int k=j+1; if (people[j].scores<people[k].scores) { swapstruct(people[j],people[k]); } } } return 0; }
#include <iostream> #include<cctype> int rankletter(char* str); void swapc(char* a,char* b); const int MAX = 1000; int main() { using namespace std; char* str = new char[MAX]; cin.getline(str, MAX); rankletter(str); cout<<str<<endl; delete[]str; return 0; } int rankletter(char* str) { int i=0; int k; if (NULL == str) { return -1; } int n=strlen(str); while (i<n) { for(int j=0;j<n-1-i;j++) { if(toupper(str[j]) >= 'A'&&toupper(str[j]) <='Z') { k=j+1; while((toupper(str[k]) < 'A' || toupper(str[k]) > 'Z') && k < n) { k++; } if (k==n) { continue; } if(toupper(str[k])< toupper(str[j])) { swapc(&str[j],&str[k]); } } } i++; } return 0; } void swapc(char* a,char* b) { char temp; temp=*a; *a=*b; *b=temp; }
【中级】单词倒排
#include <iostream> #include<string> using namespace std; int main() { string str; string strss; string stryy[200]; int i; getline(cin,str); int count=0; int j=0; int n=str.size(); for(i=0;i<n;i++) { /* if(j=n) return 0; i=j;*/ if(toupper(str[j])<'A'||toupper(str[j])>'Z') { j++; continue; } if(toupper(str[i])<'A'||toupper(str[i])>'Z') { basic_string <char> strss = str.substr(j,i-j); j=i+1; stryy[count]=strss; while(toupper(str[j])<'A'||toupper(str[j])>'Z') { j++; } count ++; i=j-1; } } strss = str.substr(j,i-j); stryy[count]=strss; for (int k=count;k>=0;k--) { cout<<stryy[k]<<" "; } cout<<endl; return 0; }