夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

だいぶ調子が戻ってきた

AOJ 1007

めんどくさい

#include <cstdio>
int main() {
	for (int ix=1;;ix++) {
		int n; scanf("%d",&n);
		if (n==0) break;
		printf("Case %d:\n",ix);
		int f[n][n];
		int nx=0,ny=0;
		if (n%2==0) {
			for (int i=1; i<=n*n; i++) {
				f[nx][ny]=i;
				if (nx==n-1&&ny%2==0) ny++;
				else if (nx==n-1) {
					nx--;
					ny++;
				}
				else if (ny==n-1&&nx%2!=0) nx++;
				else if (ny==n-1) {
					nx++;
					ny--;
				}
				else if (nx==0&&ny%2==0) ny++;
				else if (nx==0) {
					nx++;
					ny--;
				}
				else if (ny==0&&nx%2!=0) nx++;
				else if (ny==0) {
					nx--;
					ny++;
				} 
				else if ((nx+ny)%2==0) {
					nx--;
					ny++;
				}
				else {
					nx++;
					ny--;
				}
			}
		} else {
			for (int i=1; i<=n*n; i++) {
				f[nx][ny]=i;
				if (nx==n-1&&ny%2!=0) ny++;
				else if (nx==n-1) {
					nx--;
					ny++;
				}
				else if (ny==n-1&&nx%2==0) nx++;
				else if (ny==n-1) {
					nx++;
					ny--;
				}
				else if (nx==0&&ny%2==0) ny++;
				else if (nx==0) {
					nx++;
					ny--;
				}
				else if (ny==0&&nx%2!=0) nx++;
				else if (ny==0) {
					nx--;
					ny++;
				} 
				else if ((nx+ny)%2==0) {
					nx--;
					ny++;
				}
				else {
					nx++;
					ny--;
				}
			}
		}
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
				printf("%3d",f[i][j]);
			}
			printf("\n");
		}
	}
}

AOJ 1009

#include <cstdio>
int gcd(int a, int b) {
	return b!=0?gcd(b,a%b):a;
}
int main() {
	int a, b;
	while (scanf("%d%d",&a,&b)!=EOF) printf("%d\n",gcd(a,b));
}

AOJ 1008

#include <cstdio>
#include <climits>
#include <algorithm>
#include <map>
using namespace std;
int main() {
	int alen;
	while (scanf("%d",&alen)) {
		if (alen == 0) break;
		int a[alen];
		for (int i=0; i<alen; i++) scanf("%d",&a[i]);
		sort(a,a+alen);
		map<int, int> cnt;
		for (int i=0; i<alen; i++) {
			if (cnt.count(a[i])==0) {
				cnt[a[i]]=1;
			} else {
				cnt[a[i]]++;
			}
		}
		int res = INT_MAX;
		for (map<int, int>::iterator it=cnt.begin(); it!=cnt.end(); ++it) {
			if ((*it).second > alen/2) {
				res = (*it).first;
			}
		}
		if (res == INT_MAX) {
			printf("NO COLOR\n");
		} else {
			printf("%d\n",res);
		}
	}
}

AOJ 1018

#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
	int N;
	while (scanf("%d",&N)!=EOF) {
		int P[N];
		for (int i=0; i<N; i++) scanf("%d",&P[i]);
		sort(P, P+N);
		int res = 0, sum = 0;
		for (int i=0; i<N; i++) {
			sum += P[i];
			res += sum;
		}
		printf("%d\n", res);
	}
}

AOJ 1027

#include <cstdio>
int main() {
	int K;
	while (scanf("%d",&K)) {
		if (K == 0) break;
		int res = 0;
		for (int i=0; i<K*(K-1)/2; i++) {
			int in; scanf("%d",&in);
			res += in;
		}
		printf("%d\n",res/(K-1));
	}
}

AOJ 1041

#include <cstdio>
int main() {
	int n;
	while (scanf("%d",&n)) {
		if (n == 0) break;
		int res = 0;
		for (int i=0; i<n/4; i++) {
			int in; scanf("%d",&in);
			res += in;
		}
		printf("%d\n",res);
	}
}

PKU 3255

ありほん

#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <functional>
using namespace std;
struct edge {
	int to, cost;
	edge(int _to, int _cost) { to=_to; cost=_cost; }
};
typedef pair<int, int> P;
int N,R;
void solve(vector<edge> G[]) {
	int d[N], d2[N];
	fill(d,d+N,INT_MAX);
	fill(d2,d2+N,INT_MAX);
	priority_queue<P, vector<P>, greater<P> > que;
	d[0]=0;
	que.push(P(0,0));
	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second, dist = p.first;
		if (d2[v] < dist) continue;
		for (int i=0; i<G[v].size(); i++) {
			edge e = G[v][i];
			int dist2 = dist + e.cost;
			if (d[e.to] > dist2) {
				swap(d[e.to], dist2);
				que.push(P(d[e.to], e.to));
			}
			if (d2[e.to] > dist2 && d[e.to] < dist2) {
				d2[e.to] = dist2;
				que.push(P(d2[e.to], e.to));
			}
		}
	}
	printf("%d\n",d2[N-1]);
}
int main() {
	scanf("%d%d",&N,&R);
	vector<edge> G[N];
	for (int i=0; i<R; i++) {
		int f, t, d;
		scanf("%d%d%d",&f,&t,&d);
		G[f-1].push_back(edge(t-1,d));
		G[t-1].push_back(edge(f-1,d));
	}
	solve(G);
}

PKU 3049

ちょっとめんどい

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
char vowel[] = {'a','e','i','o','u'};
void solve(vector<char> ch, int c, int l, int npos, string ns) {
	ns += ch[npos];
	if (++npos == c+1) return;
	if (ns.length() == l) {
		for (int i=0; i<ns.length(); i++) {
			if (ns[i]==vowel[0]||ns[i]==vowel[1]||ns[i]==vowel[2]||
				ns[i]==vowel[3]||ns[i]==vowel[4]) {
				cout<<ns<<endl;
				return;
			}
		}
	}
	for (int i=npos; i<c; i++) {
		solve(ch,c,l,i,ns);
	}
}
int main() {
	int l, c;
	scanf("%d%d",&l,&c);
	vector<char> ch(c);
	for (int i=0; i<c; i++) cin>>ch[i];
	sort(ch.begin(), ch.end());
	string ns = "";
	for (int i=0; i<c; i++) {
		solve(ch, c, l, i, ns);
	}
}

PKU 3672

問題文・・・

#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
	int M, T, U, F, D;
	scanf("%d%d%d%d%d\n",&M,&T,&U,&F,&D);
	char path[T];
	int dist[T];
	for (int i=0; i<T; i++) scanf("%c\n",&path[i]);
	for (int i=0; i<T; i++) {
		if (path[i]=='u'||path[i]=='d') dist[i]=U+D;
		if (path[i]=='f') dist[i]=F*2;
	}
	int res, sum=0;
	for (res=0; res<T; res++) {
		if ((sum+=dist[res])>M) break;
	}
	printf("%d\n", res);
}

USACO Bronzeコンプ目指す(`・ω・´)

広告を非表示にする