PAT简单题目代码

本文介绍了PAT考试中的一道简单题目1028. List Sorting,主要涉及列表操作和排序算法的知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main()
{
 int a, b;
 cin >> a >> b;
 int c = a + b;
 if (c >= -999 && c <= 999)
  cout << c << endl;
 else
 {
  if (c < 0)
   cout << "-";
  c = abs(c);
  string ss = "";
  int d;
  bool b = true;
  while (c>0)
  {
   char str[10] = { 0 };
   d = c % 1000;
   if (c / 1000 == 0)
   {
     
    sprintf(str, "%d", c);
    ss = "," + ss;
    ss = str + ss;
    break;
   }
   c = c / 1000;
   
   sprintf(str, "%03d",d);
   if (b)
   {
    ss = str + ss;
   }
   
   else
   {  
    ss = "," + ss;
    ss = str + ss;
   }
   
   b = false;
  }
   
  cout <<ss.c_str()<<endl;
 }
 return 0;
}

2.
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main()
{
 float a[1001] = { 0 };
 int sum = 0;
 int size = 0;
 int i, j;
 cin >> i;
 for (int k = 0; k < i;k++)
 {
  int n=0;
  float tmp = 0;
  cin >> n >> tmp;
  a[n] += tmp;
  if (n > size)
   size=n;
 }
 cin >> j;
 for (int k = 0; k < j; k++)
 {
  int n=0;
  float tmp=0;
  cin >> n >>tmp;
  a[n] += tmp;
  if (n > size)
   size = n;
 }
 float ff = 0;
 for (int k = size; k >= 0; k--)
 {
  if (a[k] != ff)
   sum++;
 }
 cout << sum;
 for (int k = size; k >= 0; k--)
 {
  if (a[k] != ff)
   printf(" %d %.1f",k,a[k]);
 }
 cout << endl;
 return 0;
}
3.
需要注意两点:1如果所在地就是目的地的情况 2.多条最短路径时候,路径条数的计算 代码红字部分
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
struct path
{
 int dis, num, amount;

};
path Rode[501][501] = { 0 };
int iCity[501] = { 0 };
int main()
{
 int city, rode, in, save;
 cin >> city >> rode >> in >> save;
 //城市信息
 
 for (int i = 0; i < city; i++)
  cin >> iCity[i];
 //道路信息
 
 for (int i = 0; i < city; i++)
 {
  for (int j = 0; j < city; j++)
  {
   Rode[i][j].dis = 9999;
   Rode[i][j].amount = iCity[j] + iCity[i];
   Rode[i][j].num = 0;
   if (i == j)
   {
    Rode[i][j].dis = 0;
    Rode[i][j].amount = iCity[j];
   }
  }
   
 }
 for (int i = 0; i < rode; i++)
 {
  int j, k;
  cin >> j >> k;
  cin >> Rode[j][k].dis;
  Rode[k][j].dis = Rode[j][k].dis;
  Rode[j][k].num = 1; Rode[k][j].num = 1;
 }
 if (in == save)
 {
  cout << "1 " << iCity[in];
  return 0;
 }

 vector<int>Short;//存放已经确定了最短路径的点
 Short.push_back(in);
 vector<int>Undeal;//存放没有确定了最短路径的点
 for (int i = 0; i < city; i++)
  if (i != in)
   Undeal.push_back(i);

 while (Undeal.size()>0)
 {
  //找出最短距离的点
  vector<int>::iterator it = Undeal.begin();
  int iS = Rode[in][*it].dis;
  vector<int>::iterator iMark = it++;
  for (; it != Undeal.end(); it++)
  {
   if (iS > Rode[in][*it].dis)
   {
    iMark = it;
    iS = Rode[in][*it].dis;
   }
  }
  int iMin = *iMark;
  if (iMin == save)
  {
   cout << Rode[in][save].num << " " << Rode[in][save].amount;
   return 0;
  }
  Undeal.erase(iMark);
  for (it = Undeal.begin(); it != Undeal.end(); it++)
  {
   int idis = Rode[in][iMin].dis + Rode[iMin][*it].dis;
   if (Rode[in][*it].dis > idis)
   {
    Rode[in][*it].dis = idis;
    Rode[in][*it].num = max(Rode[in][iMin].num, Rode[iMin][*it].num);
    Rode[in][*it].amount = Rode[in][iMin].amount + Rode[iMin][*it].amount - iCity[iMin];
   }
   else if (Rode[in][*it].dis == idis)
   {
    Rode[in][*it].num += max(Rode[in][iMin].num, Rode[iMin][*it].num);
    int imount = Rode[in][iMin].amount + Rode[iMin][*it].amount - iCity[iMin];
    if (Rode[in][*it].amount < imount)
     Rode[in][*it].amount = imount;
   }
  }
  Short.push_back(iMin);
 }
 cout << Rode[in][save].num << " " << Rode[in][save].amount;
 return 0;
}
4.
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
using namespace std;  
struct Node
{
 int ID;
 int iNum ;
 int Leaf[100];
 Node()
 {
  iNum = 0;
  memset(Leaf, 0, sizeof(int) * 100);
 }
};
int main()
{
 int inode, non_leaf;
 cin >> inode >> non_leaf;
 Node *node = new Node[inode];
 for (int i = 0; i < inode; i++)
 {
  node[i].ID = i + 1;
  node[i].iNum = 0;
  memset(node[i].Leaf, 0, sizeof(int) * 100);
 }
 for (int i = 0; i < non_leaf;i++)
 {
  int iID;
  cin >> iID;
  cin >> node[iID - 1].iNum;
  for (int j = 0; j < node[iID - 1].iNum; j++)
   cin >> node[iID - 1].Leaf[j];
 }
 vector<Node> level,next;
 level.push_back(node[0]);
 bool bfirst = true;
 while (level.size()>0)
 {
  int num_nonleaf = 0;
  for (int i = 0; i < level.size(); i++)
  {
   if (level[i].iNum == 0)
    num_nonleaf++;
   else
   {
    for (int j = 0; j < level[i].iNum; j++)
    {
     next.push_back(node[level[i].Leaf[j]-1]);
    }
   }
  }
  if (!bfirst)
   cout << " ";
  bfirst = false;
  cout << num_nonleaf;
  level = next;
  next.clear();
 }
 return 0;
}
5.需要注意0的情况
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
 
int main()
{
 char c[200];
 cin >> c;
 int i = 0;
 int sum = 0;
 while (c[i]!='\0')
 {
  int tmp = (int)(c[i]) - 48;
  sum += tmp;
  i++;
 }
 int k ;
 string out("");
 while (sum>0)
 {
  k = sum % 10;
  switch (k)
  {
  case 0:
   out = " zero" + out;
   break;
  case 1:
   out = " one" + out;
   break;
  case 2:
   out = " two" + out;
   break;
  case 3:
   out = " three" + out;
   break;
  case 4:
   out = " four" + out;
   break;
  case 5:
   out = " five" + out;
   break;
  case 6:
   out = " six" + out;
   break;
  case 7:
   out = " seven" + out;
   break;
  case 8:
   out = " eight" + out;
   break;
  case 9:
   out = " nine" + out;
   break;
  }
  sum = sum / 10;
 }
 if (out == "")
  out = " zero";
 cout << out.substr(1).c_str();  
 return 0;
}
6.

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
struct info
{
 char ID[16];
 int in, out;
 info()
 {
  ID[0] = '\0';
  in = 0;out = 0;
 }
};
int main()
{  
 int num;
 cin >> num;
 info *person = new info[num];
 int iEarly, iLate=0;
 iEarly = 24 * 3600;
 int earM = 0, lateM = 0;
 for (int i = 0; i < num; i++)
 {
  int H1, M1, S1, H2, M2, S2;
  scanf("%s %d:%d:%d %d:%d:%d",person[i].ID,&H1,&M1,&S1,&H2,&M2,&S2);
  person[i].in = H1 * 3600 + M1 * 60 + S1;
  person[i].out = H2 * 3600 + M2 * 60 + S2;
  if (iEarly > person[i].in)
  {
   iEarly = person[i].in;
   earM = i;
  }
  if (iLate < person[i].out)
  {
   iLate = person[i].out;
   lateM = i;
  }
 }
 printf("%s %s", person[earM].ID, person[lateM].ID);
 return 0;
}
7.
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>  
using namespace std;

int main()
{
 int num;
 cin >> num;
 int *data = new int[num];
 bool bNegative = true;
 for (int i = 0; i < num; i++)
 {
  cin >> data[i];
  if (data[i] >= 0)
   bNegative = false;
 }
 if (bNegative)
 {
  cout << "0 " << data[0] << " " << data[num - 1];
  return 0;
 }
 int iStart = 0,tmp_j=0;
 int iOutput = data[0], out_i = 0, out_j = 0;
 while (iStart < num)
 {
  if (data[iStart] < 0)
  {
   iStart++;
   continue;
  }
  else
  {
   tmp_j = iStart;
   int tmp_max = data[iStart];
   int i;
   for (i = iStart+1; i < num; i++)
   {
    if (data[i] > 0)
    {
     tmp_max += data[i];
     tmp_j = i;
    }
    else
    {
     int sum = data[i], iMark = i,tmp_sum=data[i];
     for (int j = i+1; j < num; j++)
     {
      tmp_sum += data[j];
      if (tmp_sum > sum)
      {
       sum = tmp_sum;
       iMark = j;
      }
     }
     if (sum > 0)
     {
      tmp_max += sum;
      tmp_j = iMark;
     }
     break;
    }
   }
   //iStart = num;
   if (tmp_max > iOutput)
   {
    out_i = iStart;
    out_j = tmp_j;
    iOutput = tmp_max;
   }
    //找出第一个值大于零的数,没有则结束程序
   iStart = num;
   for (int j = i; j < num; j++)
   {
    if (data[j] >= 0)
    {
     iStart = j;
     break;
    }
   }
  }
 }
 cout << iOutput << " " << data[out_i] << " " << data[out_j];
 return 0;
}
11.
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>  
using namespace std; 
int main()
{  
double f1, f2, f3;
double sum = 1;
for (int i = 0; i < 3; i++)
{
cin >> f1 >> f2 >> f3;
if (f1 > f2)
{
if (f1 > f3)
{
sum *= f1;
cout << "W ";
}
else
{
sum *= f3;
cout << "L ";
}
}
else
{
if (f2 > f3)
{
sum *= f2;
cout << "T ";
}
else
{
sum *= f3;
cout << "L ";
}
}
}
sum = (sum*0.65 - 1) * 2;
printf("%.2f",sum);
return 0;
}

18.
#define  _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int station[501] = {0};
int Cmax = 0;
struct Rode
{
 int dis;
 vector<int> q;
 int set,back;
 Rode()
 {
  set = 0;
  back = 0;
  dis = 9999;
 }
};
void CountSetBack(Rode a,int station,int&set,int&back)
{  
 set = 0, back = 0;
 int tmp = station + a.back - Cmax / 2;
 if (tmp > 0)
 {
  set = a.set;
  back = tmp;
 }
 else
 {
  set = a.set - tmp;
  back = 0;
 }
}
 
int main()
{
 int  N=0, Sp=0, M=0;
 cin >> Cmax >> N >> Sp >> M;
 
 Rode **rode = new Rode*[N + 1];
 station[0] = 0;
 rode[0] = new Rode[N + 1];
 for (int i = 1; i <= N; i++)
 {
  rode[i] = new Rode[N+1];  
  cin >> station[i];
 }
 for (int i = 0; i < M; i++)
 {
  int a = 0, b = 0,  c = 0;
  cin >> a >> b >> c;
  rode[a][b].dis = c;
  rode[b][a].dis = c;
 }
 for (int i = 1; i <= N; i++)
 {
  if (rode[0][i].dis < 9999)
  {
   if (station[i] - Cmax / 2>0)
   {
    rode[0][i].back = station[i] - Cmax / 2;
    rode[0][i].set = 0;
   }
   else
   {
    rode[0][i].set = Cmax / 2 - station[i];
    rode[0][i].back = 0;
   }
   
  }
 }
 vector<int>Unsolve;
 for (int i = 1; i <= N; i++)
  Unsolve.push_back(i);
 while (Unsolve.size()>0)
 {
  vector<int>::iterator min = Unsolve.begin();
  int imin = rode[0][*min].dis;
  for (vector<int>::iterator it = Unsolve.begin(); it != Unsolve.end(); it++)
  {
   if (rode[0][*it].dis < imin)
   {
    imin = rode[0][*it].dis;
    min = it;
   }
  }
  if (*min == Sp)
   break;
  //vector<int>::iterator min=*FindMin(Unsolve, station, rode);
 
  for (vector<int>::iterator it = Unsolve.begin(); it != Unsolve.end(); it++)
  {
   if (it == min)
    continue;
   int tmp = rode[0][*min].dis + rode[*min][*it].dis;
   if (rode[0][*it].dis > tmp)
   {
    rode[0][*it].dis = tmp;
    rode[0][*it].q = rode[0][*min].q; rode[0][*it].q.push_back(*min);
    int set = 0, back = 0;
    CountSetBack(rode[0][*min], station[*it], set, back);
    rode[0][*it].set = set;
    rode[0][*it].back = back;
   }
   else if (rode[0][*it].dis == tmp)
   {
    int set = 0, back = 0;
    CountSetBack(rode[0][*min], station[*it], set, back);
     
    if (rode[0][*it].set>set || (rode[0][*it].set == set&&rode[0][*it].back>back))
    {
     rode[0][*it].q = rode[0][*min].q;
     rode[0][*it].q.push_back(*min);
     rode[0][*it].set = set;
     rode[0][*it].back = back;
    }
   }
  }
  Unsolve.erase(min);
 }
 
  cout << rode[0][Sp].set<<" 0->";
  for (int i = 0; i < rode[0][Sp].q.size();i++)
  {
   cout << rode[0][Sp].q[i] << "->";
  }
  cout << Sp<<" ";
  cout << rode[0][Sp].back;
 
 return 0;
}

1028. List Sorting (25)

#define  _CRT_SECURE_NO_WARNINGS
#include<string>
#include<string.h>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct Student
{
 int ID;
 char name[10];
 int score;
};
bool cmp1(Student a,Student b)
{
 return a.ID < b.ID;
}
bool cmp2(Student a, Student b)
{
 if (strcmp(a.name, b.name) == 0)
  return a.ID < b.ID;
 else
  return strcmp(a.name, b.name) < 0;
}
bool cmp3(Student a, Student b)
{
 if (a.score==b.score)
  return a.ID < b.ID;
 else
  return a.score<b.score;
}
int main()
{
 int N, C;
 cin >> N >> C;
 Student *stu = new Student[N];
 for (int i = 0; i < N; i++)
 {
  scanf("%d %s %d",&stu[i].ID,stu[i].name,&stu[i].score);
 }
 if (C == 1)
  sort(stu,stu+N,cmp1);
 else if (C == 2)
  sort(stu, stu + N, cmp2);
 else
  sort(stu, stu + N, cmp3);
 for (int i = 0; i < N; i++)
  printf("%06d %s %d\n",stu[i].ID,stu[i].name,stu[i].score);
 return 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值