码迷,mamicode.com
首页 > 其他好文 > 详细

pat-advance(1069-1072)

时间:2015-08-06 15:06:24      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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;
}







版权声明:本文为博主原创文章,未经博主允许不得转载。

pat-advance(1069-1072)

标签:

原文地址:http://blog.csdn.net/lulu901130/article/details/47316389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!