pat 甲级 1027-1030

1027. Colors in Mars (20)

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input

Each input file contains one test case which occupies a line containing the three decimal color values.

Output

For each test case you should output the Mars RGB value in the following format: first output “#”, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a “0” to the left.

Sample Input
15 43 71
Sample Output

123456

代码:

color = [int(i) for i in input().split()]
def toradix(num):
    res = ''
    while num != 0:
        temp = num % 13
        if temp > 9:
            temp = chr(65 + temp - 10)
        res += str(temp)
        num = num // 13
    res = res[::-1].zfill(2)
    return res

result = '#'
for i in range(3):
    result += toradix(color[i])
print(result)

1028. List Sorting (25)

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input

Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.

Sample Input 1
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

代码:

line1 = input().split()
N = int(line1[0])
column = int(line1[1]) - 1
records = []
for i in range(N):
    records.append([])
    records[i] = input().split()
    records[i][2] = int(records[i][2])
if column == 0:
    records = sorted(records, key = lambda x: x[column])
else:
    records = sorted(records, key = lambda x: (x[column], x[0]))
for i in range(N):
    res = records[i][0] + ' ' + records[i][1] + ' ' + str(records[i][2])
    print(res)

1029. Median (25)

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

Given two increasing sequences of integers, you are asked to find their median.

Input

Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (<=1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.

Output

For each test case you should output the median of the two given sequences in a line.

Sample Input
4 11 12 13 14
5 9 10 15 16 17
Sample Output
13

代码:

#include<vector>
//#include<iostream> //cin cout有一个测试点超时
#include<algorithm>
using namespace std;

int main() 
{
    int N;
    /*cin >> N;*/
    scanf("%d", &N);
    vector<int> v;
    int t;
    for (int i = 0; i < N; i++)
    {
        /*cin >> t;*/
        scanf("%d", &t);
        v.push_back(t);
    }
    //cin >> N;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
    {
        //cin >> t;
        scanf("%d", &t);
        v.push_back(t);
    }
    sort(v.begin(), v.end());
    //cout << v[(v.size() - 1) / 2];
    printf("%d", v[(v.size() - 1) / 2]);
    return 0;
}

这题啊,用python能挂一半的测试点,读进来第一行数据就内存超限,所以c++了。

1030. Travel Plan (30)

A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40

代码:

line1 = [int(i) for i in input().split()]
cityNum = line1[0]
roadNum = line1[1]
beginCity = line1[2]
destination = line1[3]
graph = [[[] for i in range(cityNum)] for i in range(cityNum)]
for i in range(roadNum):
    temp = [int(i) for i in input().split()]
    graph[temp[0]][temp[1]] = graph[temp[1]][temp[0]] = [temp[2], temp[3]]
# print(graph)
marked = [False] * cityNum
mindistance = float('inf')
mincost = float('inf')
path = [beginCity]
def dfs(node, distance, cost, temppath):
    global mindistance, mincost, path 
    if node == destination:
        if distance < mindistance:
            mindistance = distance
            mincost = cost
            path = temppath[:]
        elif distance == mindistance:
            if cost < mincost:
                mincost = cost
                path = temppath[:]
        return
    if distance >= mindistance:
        return
    marked[node] = True
    for i in range(cityNum):
        if len(graph[node][i]) > 0 and marked[i] == False:
            temppath.append(i)
            dfs(i, distance + graph[node][i][0], cost + graph[node][i][1], temppath)
            temppath.pop()
            marked[i] == False
    return
dfs(beginCity, 0, 0, path)
path = [str(i) for i in path]
print(' '.join(path), mindistance, mincost)

path要在函数里global, 并且把temppath赋给path要深拷贝,不然函数返回后Path不变

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值