1. 程式人生 > 實用技巧 >PAT(Basic Level) 1004

PAT(Basic Level) 1004

#include<iostream>
#include<string>

using namespace std;

struct student{
    string name;
    string number;
    int score;
};

int main(){
    int n;
    cin>>n;
    student a[n];
    int max=0;
    int min=100;
    int maxnum,minnum;
    for(int i=0;i<n;i++){
        cin>>a[i].name >>a[i].number >>a[i].score ;
        
if(a[i].score>max){ max=a[i].score; maxnum=i; } if(a[i].score<min){ min=a[i].score; minnum=i; } } cout<<a[maxnum].name <<" "<<a[maxnum].number <<endl; cout<<a[minnum].name <<"
"<<a[minnum].number <<endl; return 0; }

注:

1、以空格、tab或回車可分隔不同的字串

#include<iostream>
#include<string>

using namespace std;

int main(){
    string name,number;
    int score;
    cin>>name>>number>>score;
    
    cout<<name<<" "<<number<<"
"<<score<<endl; //輸入:abc math123456 123 //輸出:abc math123456 123 return 0; }

另:string型別大小比較:兩個字串自左向右逐個字元相比(按ASCII值大小相比較),直到出現不同的字元或遇'\0'為止。