HDOJ 2011-2020

 

 

2011 多项式求和

#include<stdio.h>
int main(){
	int m,n;
	double sum;
	while(scanf("%d",&m)!=EOF){
		int i;
		for(i = 0;i < m;i ++){
			scanf("%d",&n);
			sum = 0;
			int j;
			for(j = 1;j <= n;j ++){
				if(j % 2 == 0){
					sum += -1*(1.0/j);
				}else{
					sum += (1.0/j);
				}
			}
			printf("%.2lf\n",sum);
		}
	}
	return 0;
}

2012 素数判定

#include<stdio.h>
int main() {
	int x,y,i,j,t,k;
	while(scanf("%d %d",&x,&y)!=EOF){
		if(x==0&&y==0) break;
		k=0;
		for(i=x;i<=y;i++){
			t = i*i+i+41;
			for(j=2;j<t;j++){
				if(t%j==0){
					k=1;
				}
			}
		}
		if(k==1){
			printf("Sorry\n");
		}else{
			printf("OK\n");
		}
	}
	return 0;
}

2013 蟠桃记

#include<stdio.h>
int main(){
	int n,sum,i;
	while(scanf("%d",&n)!=EOF){
		sum=1;
		for(i=0;i<n-1;i++){
			sum=(sum+1)*2;
		}
		printf("%d\n",sum);
	}
	return 0;
} 

2014 青年歌手大奖赛_评委会打分

#include<stdio.h>
int main(){
	int n;
	int min, max;
	double sum;
	while(scanf("%d",&n) != EOF){
		int i;
		int s;
		sum = 0;
		min = 100;max = 0;
		for(i = 0;i < n;i ++){
			scanf("%d",&s);
			if(s < min) min = s;
			if(s > max) max = s;
			sum += s;
		}
		sum = sum-max-min;
		double avg = sum/(n-2);
		printf("%.2lf\n",avg);
	}
	return 0;
}

2015 偶数求和

#include<stdio.h>
int main(){
	int n,m,i;
	int s[100];
	while(scanf("%d %d",&n,&m)!=EOF){
		for(i=0;i<n;i++){
			s[i]=(i+1)*2;
			if((i+1)%m==0){
				printf("%d",(s[i]+(m-1)*(-2)/2));
				if(i != n-1){
					printf(" ");
				}else{
					printf("\n");
				}
			}
		}
		if(n%m!=0){
			printf("%d\n",s[n-1]+(n%m-1)*(-2)/2);
		}
		
	}
	return 0;
}

2016 数据的交换输出

#include<stdio.h>
int main(){
	int n,min,t,i,x=0;
	int s[100];
	while(scanf("%d",&n)!=EOF){
		if(n==0) break;
		for(i=0;i<n;i++){
			scanf("%d",&s[i]);
			if(i==0) min=s[i];
			else{
				if(s[i]<min){
					min=s[i];
					x=i;
				}
			}
		}
		if(x != 0){
			t=s[x];
			s[x]=s[0];
			s[0]=t;
		}
		for(i=0;i<n;i++){
			if(i==0) printf("%d",s[i]);
			else printf(" %d",s[i]);
		}
		printf("\n");
	}
	return 0;
}

2017 字符串统计

#include<stdio.h>
int main(){
	int n;
	char t;
	while(scanf("%d %t",&n,&t)!=EOF){
		char c[200];
		while(n --){
			int sum = 0;
			int i = 0;
			scanf("%s",&c);
			while (c[i] != '\0'){
				if(c[i] <= '9'&&c[i] >= '0')
                	sum++;
                i++;
			}
			printf("%d\n",sum);
		}
	}
	return 0;
} 

2018 母牛的故事

#include<stdio.h>

int cow(n){
	if(n <= 4) return n;
	else{
		return cow(n-1)+cow(n-3);
	}
}


int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		if(n == 0){
			break;
		}else{
			printf("%d\n",cow(n));
		}
	}
	return 0;
}

2019 数列有序!

#include<stdio.h>
int main(){
	int n,m,i,t,p;
	while(scanf("%d %d",&n,&m)!=EOF){
		if(n==0&&m==0) break;
		p=0;
		for(i=0;i<n;i++){
			scanf("%d",&t);
			if(t < m){
				printf("%d",t);
			}else if(t > m && p==0){
				printf("%d %d",m,t);
				p=1;
			}else{
				printf("%d",t);
			}
			if(i==n-1){
				if(p==0){
					printf(" %d\n",m);
				}else{
					printf("\n");
				}
			}else{
				printf(" ");
			}
		}
	}
	return 0;
} 

2020 绝对值排序

#include<stdio.h>
#include<math.h>
int main(){
	int n,i,j,t;
	int s[100];
	while(scanf("%d",&n)!=EOF){
		if(n==0) break;
		for(i=0;i<n;i++){
			scanf("%d",&s[i]);
		}
		for(i=0;i<n-1;i++){
			for(j=1;j<n;j++){
				if(abs(s[j-1])<abs(s[j])){
					t=s[j];
					s[j]=s[j-1];
					s[j-1]=t;
				}
			}
		}
		for(i=0;i<n;i++){
			if(i==0) printf("%d",s[i]);
			else printf(" %d",s[i]);
		}
		printf("\n");
	}
	return 0;
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页