CCF/CSP-201503(第4次)

1. 图像旋转

#include <iostream>
using namespace std;

const int maxn = 1005;
int a[maxn][maxn];

int main(){
    int n, m;
    
    cin >> n >> m;
    for(int r = 0; r < n; ++r){
        for(int c = 0; c < m; ++c)
            cin >> a[r][c];
    }
    for(int c = m-1; c >= 0; --c){//从上往下,从右往左
        for(int r = 0; r < n; ++r){
            cout << a[r][c] << " ";
        }
        cout << endl;
    }
    return 0;
}
2 3
1 5 3
3 2 4
 
3 4
5 2
1

2. 数字排序

#include<stdio.h>
#include<algorithm>
#define MAXN 1005
using namespace std;

struct Num {
    int value;
    int count;
    
    bool operator < (const Num &rhs) const{//重载
        if(count == rhs.count) return value < rhs.value;
        return count > rhs.count;
    }
}num[MAXN];

int main(){
    int n, t, cnt = 0;//非0个数
    
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &t);
        if(num[t].count == 0) {
            num[t].value = t;
            cnt++;
        }
        num[t].count++;
    }
    sort(num, num + MAXN);
    for(int i = 0; i < cnt; ++i)
        printf("%d %d\n", num[i].value, num[i].count);
        
    return 0;
}
12 
5 2 3 3 1 3 4 2 5 2 3 5

3 4
2 3
5 3
1 1
4 1

3. 节日

#include <iostream>
using namespace std;

int monthdays[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                   {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};// 月份天数
int leapyear(int year) {// 闰年计算函数
    return ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? 1 : 0;
}
 
int main()
{
    int a, b, c, y1, y2, wk, dy, ly;//每年的"a月的第b个星期c"的日期。
    cin >> a >> b >> c >> y1 >> y2;

    int days = 0;// 计算1850年到起始年的天数
    for(int i = 1850; i < y1; ++i)
        days += 365 + leapyear(i);

    for(int i = y1; i <= y2; ++i) {// 计算年月日并且输出
        int days2 = days;
        ly = leapyear(i);
        for(int j = 1; j < a; ++j)// 计算从1850年开始,到i年a月1日的天数
            days2 += monthdays[ly][j];
        
        wk = 1 + days2 % 7;// 计算i年a月1日的前一天为星期几
        dy = (b-1) * 7 + ((wk >= c) ? (c + 7 - wk) : (c - wk)); // 计算i年的a月第b个星期c是当月的几号
        
        if(dy > monthdays[ly][a])
            cout << "none" << endl;
        else {
            cout << i << "/";//年
            if(a < 10) cout << "0";
            cout << a << "/";//月
            if(dy < 10) cout << "0";
            cout << dy << endl;//日
        }
        days += 365 + leapyear(i);// 为计算下一年做准备:计算从1850年到下一年开始的天数
    }
    return 0;
}
5 2 7 2014 2015
 
2014/05/11
2015/05/10

4. 网络延时

//dfs深度优先搜索
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

//Calculate the distance from node now to each node, and put the result into the array d[]
void dfs(int now, int pre, int d[], vector<int> tree[])//Depth first search
{
    int u;//u traverse all points in tree[now][]
    for(int i = 0; i < (int)tree[now].size(); ++i){
        if ((u = tree[now][i]) != pre) {
            d[u] = d[now] + 1;
            dfs(u, now, d, tree);
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> tree[n+m+2];
    int dist[n+m+2];
    
    //Number of the upper switch connected to the 2~n switch
    for(int i = 2; i <= n; ++i) {
        int t;
        cin >> t;
        tree[i].push_back(t);
        tree[t].push_back(i);
    }
    
    //Number of the upper switch connected to the 1~m computer
    for(int i = 1; i <= m; ++i) {
        int t;
        cin >> t;
        tree[n+i].push_back(t);
        tree[t].push_back(n+i);
    }
    
    //The distance from node 1 to node i is stored in dist[i]
    memset(dist, 0, sizeof(dist));
    dfs(1, 0, dist, tree);
    
    //Find the node start farthest from node 1
    int start = 0;
    dist[start] = 0;
    for(int i = 1; i < n+m+1; ++i){//1~(n+m+1)
        if(dist[i] > dist[start])
            start = i;
    }
    
    //Find the distance from the start node to each node:
    //the distance from the node start to the node i is stored in dist[i]
    memset(dist, 0, sizeof(dist));
    dfs(start, 0, dist, tree);
    
    //Find the node target farthest from the node start
    int target = 0;
    for (int i = 1; i < n+m+1; ++i){
        if(dist[i] > dist[target])
            target = i;
    }
    cout << dist[target] << endl;
    
    return 0;
}
//bfs广度优先搜索
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 20000 + 5;
vector<int> G[maxn];
bool vis[maxn] = {};
int n, m, ans = 0, depth[maxn];

int main()
{
    cin >> n >> m;
    
    for (int i = 2, u; i <= n + m; ++i) {
        cin >> u;
        G[u].push_back(i);
        G[i].push_back(u);
    }
    
    queue<int> Q;
    Q.push(1);
    vis[1] = true;
    int now = 0;
    while (!Q.empty()) {
        now = Q.front();
        Q.pop();
        for (int i = 0; i < G[now].size(); ++i) {
            int v = G[now][i];
            if (vis[v]) continue;
            vis[v] = true;
            Q.push(v);
        }
    }
    Q.push(now);
    memset(vis, false, sizeof(vis));
    vis[now] = true;
    while (!Q.empty()) {
        now = Q.front();
        Q.pop();
        for (int i = 0; i < G[now].size(); ++i) {
            int v = G[now][i];
            if (vis[v]) continue;
            vis[v] = true;
            depth[v] = depth[now] + 1;
            Q.push(v);
        }
    }
    cout << depth[now];
}
4 2 
1 1 3 
2 1

4

5. 最小花费


6 4 
1 7 3 2 5 6 
1 2 4 
1 3 5 
2 4 1 
3 5 2 
3 6 1 
2 5 
4 6 
6 4 
5 6

35
16
26
13
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值