一些简单数据结构算法的实现1

来源:互联网 发布:ubuntu 开机脚本 编辑:程序博客网 时间:2024/05/20 20:58

http://pat.zju.edu.cn/contests/pat-practise/1005

#include<iostream>

#include<cstring>

#include<stack>

usingnamespace std;

int calculate(char num[]) {

       int sum = 0;

       for (int i = 0; i <strlen(num); i++)

              sum += (num[i] - '0');

       return sum;

}

char *digitToEnglish(int x)

{

       switch(x)

       {

       case 0:return"zero";

       case 1:return"one";

       case 2:return"two";

       case 3:return"three";

       case 4:return"four";

       case 5:return"five";

       case 6:return"six";

       case 7:return"seven";

       case 8:return"eight";

       case 9:return"nine";

       }

}

void printEnglish(stack<int> s) {

       int x;

       while (!s.empty()) {

              x = s.top();

              s.pop();

              cout <<" "<<digitToEnglish(x);

       }

       cout<<endl;

}

void print(int sum) {

       stack<int> s;

       while (sum>=10) {

              s.push(sum % 10);

              sum /= 10;

       }

       cout<<digitToEnglish(sum);

       printEnglish(s);

}

 

int main() {

 

       char num[101];

       cin >> num;

       int sum = calculate(num);

       print(sum);

       return 0;

}

 

 

 

 

http://pat.zju.edu.cn/contests/pat-practise/1008

 

#include<iostream>

using namespace std;

int main()

{

  int n;

  int target;

  cin>>n;

  int current=0;

  int time=0;

  while(n--)

  {

      cin>>target;

     if(target>=current)

      {

       time+=(target-current)*6+5;

      }

      else

      {

       time+=(current-target)*4+5;

      }

     current=target;

  }

 cout<<time<<endl;

  return 0;

}

 

 

http://pat.zju.edu.cn/contests/pat-practise/1036

#include<iostream>

#include<cstring>

usingnamespace std;

structstudent {

       charname[11];

       chargender[10];

       charid[11];

       intgrade;

};

int main() {

       student femaleHigh;

       student maleLow;

       student temp;

 

       femaleHigh.grade = -1;

       maleLow.grade = 101;

       int n;

       cin >> n;

       while (n--) {

              cin>>temp.name>>temp.gender>>temp.id>>temp.grade;

              if(strcmp(temp.gender,"M")==0&&temp.grade<=maleLow.grade)

              {

                     strcpy(maleLow.name,temp.name);

                     strcpy(maleLow.gender,temp.gender);

                     strcpy(maleLow.id,temp.id);

                     maleLow.grade=temp.grade;

              }elseif(strcmp(temp.gender,"F")==0&&temp.grade>=femaleHigh.grade)

              {

                     strcpy(femaleHigh.name,temp.name);

                     strcpy(femaleHigh.gender,temp.gender);

                     strcpy(femaleHigh.id,temp.id);

                     femaleHigh.grade=temp.grade;

              }

       }

       if(femaleHigh.grade!=-1)

       {

              cout<<femaleHigh.name<<" "<<femaleHigh.id<<endl;

       }else{

              cout<<"Absent"<<endl;

       }

       if(maleLow.grade!=101)

       {

              cout<<maleLow.name<<" "<<maleLow.id<<endl;

       }else

       {

              cout<<"Absent"<<endl;

       }

 

       if(femaleHigh.grade!=-1&&maleLow.grade!=101)

       {

              cout<<femaleHigh.grade-maleLow.grade<<endl;

       }else

       {

              cout<<"NA"<<endl;

       }

return 0;

}

 

http://pat.zju.edu.cn/contests/pat-practise/1035

 

#include<iostream>

#include<cstring>

usingnamespace std;

structpeople {

       charusername[11];

       charpassword[11];

};

 

bool change(char *password) {

       bool flag =false;

       for (int i = 0; i <strlen(password); i++){

              if (password[i] =='1') {

                     password[i] = '@';

                     flag = true;

              }

              if (password[i] =='0') {

                     password[i] = '%';

                     flag = true;

              }

              if (password[i] =='l') {

                     password[i] = 'L';

                     flag = true;

              }

              if (password[i] =='O') {

                     password[i] = 'o';

                     flag = true;

              }

       }

       return flag;

}

int main() {

       int n;

       cin >> n;

       int k = n;

       char username[11];

       char password[11];

       people modify[1000];

       int num = 0;

       while (n--) {

              cin >> username>> password;

              if(change(password)) {

                     strcpy(modify[num].username, username);

                     strcpy(modify[num].password, password);

                     num++;

              }

       }

       if (num == 0) {

              if (k == 1) {

                     cout << "There is" << k<< " accountand no account is modified";

              } else {

                     cout << "Thereare " << k<< "accounts and no account is modified";

              }

       } else {

              cout << num<< endl;

              for (int i = 0; i <num; i++)

                     cout <<modify[i].username <<" " <<modify[i].password << endl;

       }

       return 0;

}