PAT简单题目代码

本文介绍了PAT考试中的一道简单题目1028. List Sorting,主要涉及列表操作和排序算法的知识。
摘要由CSDN通过智能技术生成
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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值