五、PTA实验作业(结构体)

 

1、提交列表

2、设计思路就是设计一个类似于在数组里面求最低和最大数值的函数

 

 

最终代码如下:

 

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef struct Node{
    char s[35];
    double price;
}Node;
bool cmp(Node a, Node b){
    return a.price<b.price;
}
int main(){
    int n;
    Node t[15];
    cin>>n;
    getchar();
    for(int i = 0;i<n;i++){
        gets(t[i].s);
        cin>>t[i].price;
        getchar();
    }
    sort(t,t+n,cmp);
    printf("%.2lf, %s\n",t[n-1].price,t[n-1].s);
    printf("%.2lf, %s\n",t[0].price,t[0].s);
    return 0;
}


 

 

 

题目二:

1、提交列表

2、设计思路(包括流程图)

 

 最终代码如下:

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
  
  
struct list{  
    char name[11];  
    char num[17];  
    char phone[17];  
    char sex[2];
    char birthday[11];  
};  
struct list p[100];  
  
  
int main()  
{  
  
int i=0;int j=0,N,K;  
scanf("%d",&N);  
for(i=0;i<N;i++)  
{  
scanf("%s %s %c %s %s\n",&p[i].name,&p[i].birthday,&p[i].sex,&p[i].num,&p[i].phone);  
}  
  
  
    scanf ("%d", &K);  
      
    int number;   
    for (i=0; i<K; i++) {  
        scanf ("%d", &number);  
        if (number < N && number>=0)   
        {  
           printf("%s %s %s %s %s\n",p[number].name,p[number].num,p[number].phone,p[number].sex,p[number].birthday);    
        } else {  
            printf ("Not Found\n");   
        }  
    }  
    return 0;  
  
}  

 

 

题目三:

1、提交列表

2. 设计思路(包括流程图)

就是从高到低的顺序来进行配对,高分的0对应低分的1,高分的1对应低分的0;由数组的先后顺序来间接表示分数顺序。

 

 最终代码如下:

 

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<memory.h>
struct node{
  int a;
  char name[9];
}p[50];
int main()
{

     int n;
     scanf("%d", &n);
     int flag;
     for(int i = 0; i < n ;i++)
       {
       scanf("%d %s", &p[i].a, p[i].name);
       }
    for(int i = 0; i < n/2; i++)
    {
    flag = 0;
        printf("%s", p[i].name);
        if(p[i].a == 0)
        {
           for(int j = n-1; j >= n/2; j--)
           {
              if(p[j].a == 1&&p[j].a!= 2)
              {
                 printf(" %s\n",p[j].name);
                 p[j].a = 2;
                 flag = 1;
              }
              if(flag == 1)
              break;
           }
        }
        else if(p[i].a == 1)
        {
            for(int j = n-1; j >= n/2; j--)
           {
              if(p[j].a == 0&&p[j].a!= 2)
              {
                 printf(" %s\n",p[j].name);
                 p[j].a = 2;
                 flag = 1;
              }
              if(flag == 1)
              break;
           }
        }
    }
       return 0;
}

 

 三、截图本周题目集的PTA最后排名。

 

posted @ 2018-01-20 13:13  良易  阅读(577)  评论(0编辑  收藏  举报