問101 Aizu PR

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0101
文字列の置き換えを実装する問題。

解法
一行丸ごと読み込んであとはstrstrで探して文字を置き換えるだけです。
文字数が同じだからこそできる手抜きテクニックです。

#include<stdio.h>
#include<string.h>

int main(){
	int n;
	char c;
	scanf("%d%c",&n,&c);
	while(n--){
		char text[1001],*ptr;
		scanf("%[^\n]%c",text,&c);
		for(ptr=strstr(text,"Hoshino");ptr!=NULL;ptr=strstr(ptr,"Hoshino")){
			ptr[6]='a';
 			ptr++;
		}
		printf("%s%c",text,c);
	}
}




問102 Matrix-like Computation


解法
書くだけ。

#include<stdio.h>
void calc(int n){
	int colSum[11]={0},rowSum,v;
for(int y=0;y<n;y++){
		rowSum=0;
		for(int x=0;x<n;x++){
			scanf("%d",&v);
			rowSum+=v;
			colSum[x]+=v;
			printf("%5d",v);
		}
 		printf("%5d\n",rowSum);
	}
	rowSum=0;
	for(int x=0;x<n;x++){
		printf("%5d",colSum[x]);
		rowSum+=colSum[x];
	}
	printf("%5d\n",rowSum);
}

int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
 		if(n==0)break;
		calc(n);
	}
}




問103 Baseball Simulation

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0103
簡易ベースボールのデータから得点を計算する問題

解法
指定通り実装するだけ。
コードの短さ1位はジャッジデータが流出しているとしか考えられない。
それより下の上位の人は魔法の呪文みたいなコードを書いていた。


#include<stdio.h>

int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		int Run=0,out=0,score=0;
		char com[10];
		while(out<3){
			scanf("%s",com);
			if(com[1]=='O'){
				score+=Run+1;
 				Run=0;
			}else if(com[1]=='I'){
				Run++;
				if(Run>3){
					Run=3;
					score++;
				}
			}else{
				out++;
			}
 		}
		printf("%d\n",score);
	}
}




問104 Magical Tiles

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0104
ドラクエの魔法の床を題材にした問題。

解法
指定通り実装

#include<stdio.h>
const char LOOP='x';
int main(){
int h,w;
	while(1){
		scanf("%d %d",&h,&w);
		if(w==0&&h==0)break;
 		char map[102][102];
		for(int y=0;y<h;y++){
		scanf("%s",map[y]);
		}
		int x=0,y=0;
		while(map[y][x]!='.'&&map[y][x]!=LOOP){
			char c=map[y][x];
			map[y][x]=LOOP;
			if(c=='>')x++;
			if(c=='<')x--;
			if(c=='v')y++;
			if(c=='^')y--;
 		}
		if(map[y][x]==LOOP)printf("LOOP\n");
		else printf("%d %d\n",x,y);
	}
}



問105Book Index

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0105
単語が出てくるページのデータから本の索引を出力する問題。
解法
コンテナを使って素朴に実装しました。

#include<stdio.h>
#include<string>
#include<iostream>
#include<map>
#include<set>


int main(){
	std::map<std::string,std::set<int> > memo;
	std::map<std::string,std::set<int> >::iterator mIt;
	std::set<int>::iterator sIt;
	std::string str;
	int page;
	char word[50];
	while(scanf("%s %d",word,&page)!=EOF){
		str=word;
 		memo[str].insert(page);
}
	for(mIt=memo.begin();mIt!=memo.end();mIt++){
		std::cout<<(*mIt).first<<"\n";
 		bool f=true;
		for(sIt=(*mIt).second.begin();sIt!=(*mIt).second.end();sIt++){
			if(f){
				f=false;
			}else{
				std::cout<<" ";
			}
 			std::cout<<(*sIt);
		}
		std::cout<<"\n";
	}
}

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2014年02月06日 11:54