#include<iostream>usingnamespace std;constint maxn =1005;int a[maxn][maxn];intmain(){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;}return0;}
2 3
1 5 3
3 2 4
3 4
5 2
1
2. 数字排序
#include<stdio.h>#include<algorithm>#define MAXN 1005usingnamespace std;struct Num {int value;int count;booloperator<(const Num &rhs)const{//重载if(count == rhs.count)return value < rhs.value;return count > rhs.count;}}num[MAXN];intmain(){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);return0;}
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>usingnamespace 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}};// 月份天数intleapyear(int year){// 闰年计算函数return((year%4==0&& year%100!=0)|| year%400==0)?1:0;}intmain(){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年到下一年开始的天数}return0;}
5 2 7 2014 2015
2014/05/11
2015/05/10
4. 网络延时
//dfs深度优先搜索#include<iostream>#include<vector>#include<cstring>usingnamespace std;//Calculate the distance from node now to each node, and put the result into the array d[]voiddfs(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);}}}intmain(){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 switchfor(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 computerfor(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 1int 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 startint target =0;for(int i =1; i < n+m+1;++i){if(dist[i]> dist[target])
target = i;}
cout << dist[target]<< endl;return0;}
//bfs广度优先搜索#include<iostream>#include<cstring>#include<vector>#include<queue>usingnamespace std;constint maxn =20000+5;
vector<int> G[maxn];bool vis[maxn]={};int n, m, ans =0, depth[maxn];intmain(){
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];}