PAT准备——题目训练

来源:互联网 发布:软件授权许可协议 编辑:程序博客网 时间:2024/05/21 21:48

1008 数组元素循环右移问题 (20)

一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1……AN-1)变换为(AN-M …… AN-1 A0 A1……AN-M-1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?


输入格式:每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。
输出格式:在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。
输入样例:
6 2
1 2 3 4 5 6
输出样例:
5 6 1 2 3 4

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){  int n, m;  while (cin >> n >> m)  {    int i,num;    vector<int> v;    for (i = 0; i < n; i++)    {      cin >> num;      v.push_back(num);    }    //为保证不超出数组范围对移位进行处理    m = m%v.size();    //转置思想,VU=(U^T V^T)^T    reverse(v.end()-m, v.end());    reverse(v.begin() , v.end()-m);    reverse(v.begin(), v.end());    for (i = 0; i < n - 1; i++)      cout << v[i] << ' ';    cout << v[n - 1] << endl;  }  return 0;}

1034. 有理数四则运算

/*求最大公约数*/int gcd(int a, int b){    int max,min,r;    min = (a > b) ? b : a;    max = (a > b) ? a : b;    while(r = max % min)    {        max = min;        min = r;    }    return min;}

1035. 插入与归并

/*插入算法*/void InsertSort(int arr[],  int n){    int i,j;    int temp;    for(i = 1; i < n; i++)    {        j = i;        temp = arr[i];        while(j > 0 && temp < arr[j-1])        {            arr[j] = arr[j-1];            j--;        }           arr[j] = temp;    }}

1050. 螺旋矩阵

本题要求将给定的N个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第1个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为m行n列,满足条件:m*n等于N;m>=n;且m-n取所有可能值中的最小值。


输入格式:输入在第1行中给出一个正整数N,第2行给出N个待填充的正整数。所有数字不超过104,相邻数字以空格分隔。
输出格式:输出螺旋矩阵。每行n个数字,共m行。相邻数字以1个空格分隔,行末不得有多余空格。
输入样例:
12
37 76 20 98 76 42 53 95 60 81 58 93
输出样例:
98 95 93
42 37 81
53 20 76
58 60 76

#include <iostream>#include <vector>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;bool cmp(int a, int b) {    return a > b;}int main(){    int N;    cin >> N;    vector<int> v;    int temp;    int n = 0;    int m = sqrt(N);    while (N%m != 0)    {        m++;    }    n = N / m;    if (m < n)    {        temp = m;        m = n;        n = temp;    }    //cout << m << endl << n << endl;    for (int i = 0; i < N; i++)    {        cin >> temp;        v.push_back(temp);    }    sort(v.begin(), v.end(), cmp);    vector< vector<int> > mat(m, vector<int>(n));    int i = 0;    for (i = 0; i < m; i++)        for (int j = 0; j < n; j++)            mat[i].push_back(0);    int x = 0, y = 0;    i = 0;    mat[x][y] = v[i++];    while (i < v.size())    {        while (y + 1 < n && mat[x][y + 1] == 0) mat[x][++y] = v[i++];        while (x + 1 < m && mat[x + 1][y] == 0) mat[++x][y] = v[i++];        while (y - 1 >= 0 && mat[x][y - 1] == 0) mat[x][--y] = v[i++];        while (x - 1 >= 0 && mat[x - 1][y] == 0) mat[--x][y] = v[i++];    }    for (i = 0; i < m; i++)    {        for (int j = 0; j < n - 1; j++)            cout << mat[i][j] << ' ';        cout << mat[i][n - 1] << endl;    }    system("pause");    return 0;}

1003. Emergency (25)


As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4

#include <iostream>#include <vector>#include <cstdlib>#include <cstring>using namespace std;#define INFINITY 1999999999void Dijkstra(vector< vector<int> > arc, vector<int> rescue, vector<int> &D, vector<int> &cnt, vector<int> &max, int c1){    int n = D.size();    vector<int> visited(n, 0);    int i, j, k;    int min;    for (i = 0; i < n; i++)    {        D[i] = arc[c1][i];        max[i] = rescue[c1] + rescue[i];        cnt[i] = 1;    }    D[c1] = 0;    visited[c1] = 1;    cnt[c1] = 1;    max[c1] = rescue[c1];    for (i = 1; i < n; i++)    {        min = INFINITY;        for (j = 0; j < n; j++)        {            if (!visited[j] && D[j] < min)            {                min = D[j];                k = j;            }        }        visited[k] = 1;        for (j = 0; j < n; j++)        {            if (!visited[j] && arc[k][j]!=INFINITY)            {                if (min + arc[k][j] < D[j])                {                    D[j] = min + arc[k][j];                    cnt[j] = cnt[k];                    max[j] = max[k] + rescue[j];                }                else if (min + arc[k][j] == D[j])                {                    cnt[j] += cnt[k];                    if (max[j] < max[k] + rescue[j])                        max[j] = max[k] + rescue[j];                }            }        }    }}int main(){    int n;  //城市总数,0-N-1    int m;  //道路总数    int c1, c2; //c1:起始位;c2:终点    int i, j, k;    cin >> n >> m >> c1 >> c2;    //输入一行n个整数,表示第i个城市中救援队总数    vector<int> rescue(n, 0);    for (i = 0; i < n; i++)        cin >> rescue[i];    //输入m行,包括c1,c2,l,表示连接两城市的路径长度    vector< vector<int> > arc(n, vector<int>(n, INFINITY));    for (i = 0; i < n; i++)        arc[i][i] = 0;    for (i = 0; i < m; i++)    {        cin >> j >> k;        cin >> arc[j][k];        arc[k][j] = arc[j][k];    }    vector<int> D(n,INFINITY), cnt(n, 0), max(n);    Dijkstra(arc, rescue, D, cnt, max, c1);    //输出:不同的最短路径数目,最大救援队数目    cout << cnt[c2] << ' ' << max[c2] << endl;    system("pause");    return 0;}
0 0
原创粉丝点击