pat-advance(1069-1072)

探讨了通过不断重组四字数直至达到数字黑洞6174的过程,并解决了基于库存与市场需求计算最大利润的问题。

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

1069. The Black Hole of Numbers (20)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 -- the "black hole" of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we'll get:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (0, 10000).

Output Specification:

If all the 4 digits of N are the same, print in one line the equation "N - N = 0000". Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

Sample Input 1:
6767
Sample Output 1:
7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
Sample Input 2:
2222
Sample Output 2:
2222 - 2222 = 0000
PS: 有坑,说是都是4位数,输入其实有可能是1-3位数,需要处理;且输入时6174也需要进行一次计算

//1069
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

bool cmp(char a, char b)
{
  return a > b;
}


string sub(string &s1, string &s2)
{
  string res(4, '0');

  int c = 0;
  for(int i = 3; i>=0; i--)
  {
    if(s1[i] - c < s2[i])
    {
      res[i] = s1[i]-c+10-s2[i] + '0';
      c = 1;
    }
    else
    {
      res[i] = s1[i] - c - s2[i] + '0';
      c = 0;
    }
  }
  return res;
}

int main()
{
  string s(4, '0');
  string temp;
  cin>> temp;

  int i = 3, j = temp.size()-1;
  while(j>=0)
  {
    s[i--] = temp[j--];
  }

  string s1 = s, s2 = s;
  sort(s1.begin(), s1.end(), cmp);
  sort(s2.begin(), s2.end());
  
  do
  {
    s = sub(s1, s2);
    cout<< s1<< " - "<< s2<< " = "<< s<< endl;


    s1 = s; 
    s2 = s;
    sort(s1.begin(), s1.end(), cmp);
    sort(s2.begin(), s2.end());
  }while(s !="6174" && s != "0000");

  return 0;
}


1070. Mooncake (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (<=1000), the number of different kinds of mooncakes, and D (<=500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:
3 200
180 150 100
7.5 7.2 4.5
Sample Output:
9.45
PS:注意,数量需要浮点类型
//1070 
//Greedy
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

struct mooncake {
  double quantity;
  double profits;
  double s_pro;
};

bool cmp(mooncake a, mooncake b)
{
  return a.s_pro > b.s_pro;
}

int main()
{
  int N;
  double D;
  scanf("%d %lf", &N, &D);

  vector<mooncake> mooncakes(N);
  int i;
  for(i = 0; i < N; i++)
  {
    double q;
    scanf("%lf", &q);
    mooncakes[i].quantity = q;
  }
  for(i = 0; i < N; i++)
  {
    double p;
    scanf("%lf", &p);
    mooncakes[i].profits = p;
  
    mooncakes[i].s_pro = mooncakes[i].profits/mooncakes[i].quantity;
  }

  sort(mooncakes.begin(), mooncakes.end(), cmp);

  double sum = D;
  i = 0;
  double profit = 0;
  while(i < mooncakes.size() && sum > 0)
  {
    int cos;
    if(sum > mooncakes[i].quantity)
      cos = mooncakes[i].quantity;
    else
      cos = sum;
    sum = sum - cos;
    profit += mooncakes[i].profits * cos / mooncakes[i].quantity;
    i++;
  }

  printf("%.2lf\n", profit);
  return 0;
}


1071. Speech Patterns (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.

Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?

Input Specification:

Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return '\n'. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:

For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:
Can1: "Can a can can a can?  It can!"
Sample Output:
can 5

//1071
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include<map>
#include <vector>
#include <iterator>
using namespace std;

map<string, int> mp;

int main()
{
  char c;
  string str;
  vector<string> aux;
  int start = 0;

  while((c = getchar()) != '\n')
    str.push_back(c);
  
  string temp;
  int i = 0,  max_t = 0;
  while(i < str.size())
  {
    c = str[i];
    if(isalpha(c))
      temp.push_back(tolower(c));
    else if(isdigit(c))
      temp.push_back(c);
    else
    {
      if(!temp.empty())
      {
        int count = (++mp[temp]);
        if(max_t < count)
          max_t = count;
        temp.clear();
      }
    }
    i++;
  }
  
  if(!temp.empty())
  {
    int count = (++mp[temp]);
    if(max_t < count)
      max_t = count;
  }

  map<string, int>::iterator iter;

  for(iter=mp.begin(); iter!=mp.end(); iter++)
  {
    if(max_t == iter->second)
      break;
  }

  cout<< iter->first<<" "<< max_t<< endl;
  return 0;
}


1072. Gas Station (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.

Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive integers: N (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.

Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.

Output Specification:

For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output “No Solution”.

Sample Input 1:
4 3 11 5
1 2 2
1 4 2
1 G1 4
1 G2 3
2 3 2
2 G2 1
3 4 2
3 G3 2
4 G1 3
G2 G1 1
G3 G2 2
Sample Output 1:
G1
2.0 3.3
Sample Input 2:
2 1 2 10
1 G1 9
2 G1 20
Sample Output 2:
No Solution

ps: 第一条件是离房子最小的距离越大越好

//1072
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;

#define INT_MAX 99999.9
int N, M, K;
float DS;

void helper(vector<vector<float>> &locs, int &res, float &min_d, float &min_s)
{
  int i;
  
  for(i = N+1; i<=M+N; i++)
  {
    float min_ss = INT_MAX;
    float path = 0;
    vector<int> collect(M+N+1, 0);
    vector<float> dist(M+N+1, INT_MAX);
    int j;
    for(j = 1; j<=M+N; j++)
      dist[j] = locs[i][j];
    collect[i] = 1;
    

    while(1) 
    {
      int x = 0;
      int min_dd = INT_MAX;
      for(j = 1; j<=M+N; j++)
      {
        if(!collect[j])
        {
          if(min_dd > dist[j])
          {
            min_dd = dist[j];
            x = j;
          }
        }
      }

      if(x == 0 ||(x <= N && dist[x] > DS))
        break;
      collect[x] = 1;
      if(x <=N)
      {
        if(dist[x] < min_ss)
          min_ss = dist[x];
        path += dist[x];
      }
      int k;
      for(k = 1; k <= M+N; k++)
      {
        if(locs[x][k] && collect[k] == 0)
        {
          if(dist[x] + locs[k][x] < dist[k])
          {
            dist[k] = dist[x] + locs[k][x];
          }
        }
      }
    }

    for(j = 1; j<=M+N; j++)
    {
      if(collect[j] == 0)
        break;
    }

    if(j > M+N && min_ss >= min_s)
    {
      if(min_ss > min_s)
      {
        res = i;
        min_d = path;
        min_s = min_ss;
      }
      else
      {
        if(path < min_d)
        {
          res = i;
          min_d = path;
          min_s = min_ss;
        }
      }
    }
  }
}
int main()
{
  scanf("%d%d%d%f", &N, &M, &K, &DS);
  vector<vector<float>> locs(M+N+1, vector<float>(M+N+1, INT_MAX));

  int i;
  for(i = 1; i<=M+N; i++)
  {
    locs[i][i] = 0;
  }
  for(i =0 ; i< K; i++)
  {
    string p1, p2;
    float d;
    cin>> p1>> p2>> d;
    int n, m;
    if(p1[0] != 'G')
    {
      n = stoi(p1);
    }
    else
    {
      n = stoi(p1.substr(1, p1.size()-1)) + N;
    }

    if(p2[0] != 'G')
    {
      m = stoi(p2);
    }
    else
    {
      m = stoi(p2.substr(1, p2.size()-1)) + N;
    }

    locs[n][m] = locs[m][n] = d;
  }

  int res = 0;
  float min_d = INT_MAX;
  float min_s = 0;
  helper(locs, res, min_d, min_s);

  if(res == 0)
    printf("No Solution\n");
  else
  {
    printf("G%d\n", res-N);
    printf("%0.1f %0.1f\n", min_s, min_d/N);
  }
  return 0;
}







内容概要:本文从关键概念、核心技巧、应用场景、代码案例分析及未来发展趋势五个维度探讨了Python编程语言的进阶之路。关键概念涵盖装饰器、生成器、上下文管理器、元类和异步编程,这些概念有助于开发者突破基础认知的核心壁垒。核心技巧方面,介绍了内存优化、性能加速、代码复用和异步处理的方法,例如使用生成器处理大数据流、numba库加速计算密集型任务等。应用场景展示了Python在大数据处理、Web开发、人工智能和自动化运维等多个领域的广泛运用,特别是在FastAPI框架中构建异步API服务的实战案例,详细分析了装饰器日志记录、异步数据库查询和性能优化技巧。最后展望了Python的未来发展趋势,包括异步编程的普及、类型提示的强化、AI框架的深度整合以及多语言协同。 适合人群:已经掌握Python基础语法,希望进一步提升编程技能的开发者,特别是有意向从事数据科学、Web开发或AI相关工作的技术人员。 使用场景及目标:①掌握Python进阶概念和技术,如装饰器、生成器、异步编程等,提升代码质量和效率;②学习如何在实际项目中应用这些技术,如通过FastAPI构建高效的异步API服务;③了解Python在未来编程领域的潜在发展方向,为职业规划提供参考。 阅读建议:本文不仅提供了理论知识,还包含了丰富的实战案例,建议读者在学习过程中结合实际项目进行练习,特别是尝试构建自己的异步API服务,并通过调试代码加深理解。同时关注Python社区的发展动态,及时掌握最新的技术和工具。
内容概要:本文档《Rust系统编程实战》详细介绍了Rust在系统编程领域的应用,强调了其内存安全、零成本抽象和高性能的特点。文档分为三个主要部分:核心实战方向、典型项目案例和技术关键点。在核心实战方向中,重点讲解了unsafe编程、FFI(外部函数接口)和底层API调用,涉及操作系统组件开发、网络编程、设备驱动开发、系统工具开发和嵌入式开发等多个领域,并列出了每个方向所需的技术栈和前置知识。典型项目案例部分以Linux字符设备驱动为例,详细描述了从环境搭建到核心代码实现的具体步骤,包括使用bindgen生成Linux内核API的Rust绑定,定义设备结构体,以及实现驱动核心函数。 适合人群:对系统编程有兴趣并有一定编程基础的开发者,尤其是那些希望深入了解操作系统底层机制、网络协议栈或嵌入式系统的工程师。 使用场景及目标:①掌握Rust在不同系统编程场景下的应用,如操作系统组件开发、网络编程、设备驱动开发等;②通过实际项目(如Linux字符设备驱动)的学习,理解Rust与操作系统内核的交互逻辑;③提高对unsafe编程、FFI和底层API调用的理解和运用能力。 阅读建议:由于文档内容较为深入且涉及多个复杂概念,建议读者在学习过程中结合实际操作进行练习,特别是在尝试实现Linux字符设备驱动时,务必按照文档提供的步骤逐步进行,并多加调试和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值