0021 Parallelism
#include<stdio.h>
int main(){
double xs[4],ys[4];
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&xs[0],&ys[0],&xs[1],&ys[1],&xs[2],&ys[2],&xs[3],&ys[3]);
printf("%s\n",(xs[1]-xs[0])*(ys[3]-ys[2])==(xs[3]-xs[2])*(ys[1]-ys[0])?"YES":"NO");
}
}
0022 Maximum Sum Sequence
順番に読みこんで解いていきます。
ある所まで読んで、読み込んだ数が今までの和より大きいならその数を出発点に足しなおすという作業を繰り返すことで最大値が自動的にもとまります。
和の計が最大値を更新したらそれを記録し最後に出力します。
#include <stdio.h>
int main()
{
int n,s,t,a;
while(scanf("%d",&n),n!=0){
s=a=-100001;
for(int i=0;i<n;i++){
scanf("%d",&t);
s=t+s>t?t+s:t;
a=a>s?a:s;
}
printf("%d\n",a);
}
}
0023 Circles Intersection
#include <stdio.h>
#include <math.h>
int main()
{
double xa,ya,xb,yb,ra,rb,r3,x,y;
int n,a;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lf %lf %lf %lf %lf %lf",&xa,&ya,&ra,&xb,&yb,&rb);
x=xa-xb;
y=ya-yb;
r3=sqrt(x*x+y*y);
if(r3+rb<ra){
a=2;
}else if(r3+ra<rb){
a=-2;
}else if(r3<=ra+rb){
a=1;
}else{
a=0;
}
printf("%d\n",a);
}
}
0024 Physical Experiments
#include <stdio.h>
int main(void)
{
double h,t,h1;
while(scanf("%lf",&h)>0){
t=h/9.8;
h1=4.9*t*t;
printf("%d\n",(int)(h1/5.0)+2);
}
}
0025 Hit and Blow
#include<stdio.h>
#include <math.h>
int main(){
int a[4],h,b,n;
while(scanf("%d %d %d %d",&a[0],&a[1],&a[2],&a[3])!=EOF){
h=b=0;
for(int i=0;i<4;i++){
scanf("%d",&n);
for(int j=0;j<4;j++){
if(a[j]==n){
if(i==j) h++;
else b++;
}
}
}
printf("%d %d\n",h,b);
}
}
0026 Dropping Ink
#include<stdio.h>
int map[14][14]={0};
void s(int x,int y){
map[y][x]++;
map[y][x+1]++;
map[y+1][x]++;
map[y][x-1]++;
map[y-1][x]++;
}
void m(int x,int y){
map[y+1][x+1]++;
map[y+1][x-1]++;
map[y-1][x-1]++;
map[y-1][x+1]++;
s(x,y);
}
void l(int x,int y){
map[y][x+2]++;
map[y+2][x]++;
map[y][x-2]++;
map[y-2][x]++;
m(x,y);
}
int main(){
int x,y,t;
while(scanf("%d,%d,%d",&x,&y,&t)!=EOF){
x+=2;
y+=2;
if(t==3){
l(x,y);
}else if(t==2){
m(x,y);
}else{
s(x,y);
}
}
t=0;
y=0;
for(int i=2;i<12;i++){
for(int j=2;j<12;j++){
x=map[i][j];
t+=x>0?0:1;
y=y>x?y:x;
}
}
printf("%d\n%d\n",t,y);
}
0027 What day is today?
#include<stdio.h>
int main(){
int ms[]={0,31,60,91,121,152,182,213,244,274,305,335};
char w[7][10]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int m,d;
scanf("%d %d",&m,&d);
while(m!=0){
printf("%s\n",w[(2+ms[m-1]+d)%7]);
scanf("%d %d",&m,&d);
}
}
0028 Mode Value
#include<stdio.h>
#include<string.h>
int main(){
int s[101]={0};
int a=0,m;
while(scanf("%d",&m)!=EOF){
s[m]++;
a=a>s[m]?a:s[m];
}
for(int i=1;i<101;i++){
if(a==s[i]) printf("%d\n",i);
}
}
0029 English Sentence
stdのMapを理解しているなら基本機能だけで片がつく問題です。
#include <iostream>
#include<string>
#include<map>
using namespace std;
int main(){
map<std::string,int> memo;
string s,ans1,ans2;
int max=0;
while(cin>>s){
if(ans2.size()<s.size()) ans2=s;
if(memo.find(s)==memo.end()){
memo[s]=1;
}else{
memo[s]++;
}
if(max<memo[s]){
ans1=s;
max=memo[s];
}
}
cout<<ans1<<" "<<ans2<<"\n";
}
0030 Sum of Integers
#include<stdio.h>
int main(){
int m[12][50]={0};
int c,s,p,j;
for(int i=0;i<1024;i++){
j=c=s=0;
p=i;
while(p>0){
if(p&1==1){
c++;
s+=j;
}
p=p>>1;
j++;
}
m[c][s]+=1;
}
scanf("%d %d",&c,&s);
while(c!=0 || s!=0){
if(c>10 || s>45){
printf("0\n");
}else{
printf("%d\n",m[c][s]);
}
scanf("%d %d",&c,&s);
}
}
最終更新:2011年08月21日 18:09