HDU 2004 - 2020

ACM题集:https://blog.csdn.net/weixin_39778570/article/details/83187443
题目集合:http://acm.hdu.edu.cn/listproblem.php?vol=11
2004 等级判断

#include<iostream>
#include<cstdio>

using namespace std;

double a;
int main(){
	while(scanf("%lf", &a)!=EOF){
		if(90<=a&&a<=100){
			printf("A\n");
		}else if(80<=a&&a<=89){
			printf("B\n");	
		}else if(70<=a&&a<=79){
			printf("C\n");
		}else if(60<=a&&a<=69){
			printf("D\n");
		}else if(0<=a&&a<=59){
			printf("E\n");
		}else{
			printf("Score is error!\n");
		}
	}
} 

2005 第几天?

#include<iostream>
#include<cstdio>

#define rep(i,x) for(int i=0; i<x; i++)
#define REP(i,x) for(int i=1; i<=x; i++)
using namespace std;

char p[20];
int a, b, c;

void chuli(char *s){
	int x = 0;
	const char *ptr = s;
	while(*ptr != '/'){
		x = x*10 + *ptr - '0';
		ptr++;
	}
	a = x;
	ptr++;
	//printf("%d\n", x);
	x = 0;
	while(*ptr != '/'){
		x = x*10 + *ptr - '0';
		ptr++;
	}	
	b = x;
	ptr++;
	//printf("%d\n", x);
	x = 0;
	while(*ptr != 0){
		x = x*10 + *ptr - '0';
		ptr++;
	}
	c = x;
	//printf("%d\n", x);
}

void solve(){
	int ans = 0;
	if(a%400==0 || (a%4==0&&a%100!=0)){
		for(int i=1; i<b; i++){
			if(i==2){
				ans += 29;
			}else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
				ans += 31;
			}else{
				ans += 30;
			}
		}		
	}else{
		for(int i=1; i<b; i++){
			if(i==2){
				ans += 28;
			}else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
				ans += 31;
			}else{
				ans += 30;
			}
		}
	}
	ans += c;
	printf("%d\n", ans);
}

int main(){
	while(scanf("%s", p)!=EOF){
		chuli(p);
		solve();
	}
	return 0;
}

2006 求奇数的乘积

#include<iostream>
#include<cstdio>

#define rep(i,x) for(int i=0; i<x; i++)
#define REP(i,x) for(int i=1; i<=x; i++)
#define LL long long 

using namespace std;

LL n, a, ans=1;
int main(){
	while(scanf("%lld", &n) != EOF){
		ans = 1;
		while(n--){
			scanf("%lld", &a);
			if(a&1){
				ans *= a;
			}
		}
		printf("%lld\n", ans);
	}
}

2007 平方和与立方和

#include<iostream>
#include<cstdio>

#define rep(i,x) for(int i=0; i<x; i++)
#define REP(i,x) for(int i=1; i<=x; i++)
#define LL long long
using namespace std;
int a, b;
LL ans1, ans2;
int main(){
	while(scanf("%d%d", &a, &b) != EOF){
		ans1 = 0;
		ans2 = 0;
		if(a>b) swap(a,b); // 需要交换m,n 
		for(int i=a; i<=b; i++){
			if(i&1){
				ans2 += i*i*i; 
			}else{
				ans1 += i*i;
			}
		}
		printf("%lld %lld\n", ans1, ans2);
	}
	return 0;
}

2008 数值统计

#include<iostream>
#include<cstdio>

#define LL long long 
using namespace std;
int n,ans1, ans2, ans3;
double a;
int main(){
	while(scanf("%d", &n) && n!=0){
		ans1=0,ans2=0,ans3=0;
		for(int i=0; i<n; i++){
			scanf("%lf", &a);
			if(a<0) ans1++;
			else if(a==0) ans2++;
			else ans3++;
		}
		printf("%d %d %d\n", ans1,ans2,ans3);
	}
	return 0;
} 

2009求数列的和

#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
int n, m;
double ans;
int main(){
	while(scanf("%d%d", &n,&m) != EOF){
		ans = n;
		double t = n;
		for(int i=2; i<=m; i++){
			t = sqrt(t);
			ans += t;	
		}	
		printf("%.2f\n", ans);
	}
	return 0;
}

2010 水仙花数

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

bool a[1000] = {0}; 
bool ok(int x){
	int t = x;
	int ans = 0;
	ans += pow((x%10),3);
	x /= 10;
	ans += pow((x%10),3);
	x /= 10;
	ans += pow((x%10),3);
	return ans == t;
}
void init(){
	for(int i=100; i<=999; i++){
		if(ok(i)) a[i] = 1;
	}
}
int n, m;
int main(){
	init();
	while(scanf("%d%d", &m, &n)!=EOF){
		int t = 0;
		for(int i=m; i<=n; i++){
			if(a[i]){
				t==0? printf("%d", i) : printf(" %d", i);
				t = 1;
			}
		}
		if(t==0)printf("no\n");
		else printf("\n");
	}
	
	return 0;
}

2011 多项式求和

#include<iostream>
#include<cstdio>
#include<algorithm>

#define LL long long

using namespace std;

int t, q;
double ans[1001];
void init(){
	double now = 0;
	for(int i=1; i<=1000; i++){
		if(i&1){
			now += 1.0/i;
			ans[i] = now;
		}else{
			now -= 1.0/i;
			ans[i] = now;
		}
	}
}
int main(){
	init();
	scanf("%d", &t);
	while(t--){
		scanf("%d", &q);
		printf("%.2f\n", ans[q]); 
	}
	return 0;
} 

2012 素数判定

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long 
#define MAXN 3000

using namespace std;

bool flat[MAXN] = {0};

void init(){
	flat[0] = 1; flat[1] = 1;
	for(int i=2; i<MAXN; i++){
		if(!flat[i]){
			for(int j=i<<1; j<MAXN; j+=i){
				flat[j] = 1;
			}
		}
	}
}
int a, b;
int main(){
	init();
	while(scanf("%d%d",&a,&b)){
		if(a==0 && b==0) break;
		bool f = 0;
		for(int i=a; i<=b; i++){
			int x = i*i+i+41;
			if(flat[x]){
				f = 1;
				printf("Sorry\n");
				break;
			}
		}
		if(!f) printf("OK\n");
	}
	return 0;
}

2013蟠桃记
x为前一天蟠桃数, x - (x/2+1) = now(表示当天蟠桃树) 所以 x/2-1 = now
f[n]/2 - 1 = f[n-1] 故 f[n] = (f[n-1]+1)*2

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define LL long long
using namespace std;

int p[100],s[100];
// 递归 
int PT(int x){
	if(x==1) return p[1] = 1;
	return p[x] = (PT(x-1)+1) * 2;
}
// 递推
void dp(){
	s[1] = 1;
	for(int i=2; i<=32; i++) s[i] = (s[i-1]+1)*2;
} 
int main(){
	//PT(32);
	dp();
	int a;
	while(scanf("%d", &a)!=EOF) printf("%d\n", s[a]);
	return 0;
} 

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;

int n;
double a[105];
int main(){
	while(scanf("%d", &n) != EOF){
		for(int i=0; i<n; i++) scanf("%lf", &a[i]);
		sort(a, a+n);
		double sum = 0;
		for(int i=1; i<n-1; i++) sum += a[i];
		printf("%.2f\n", sum/(n-2));
	}
}

2015 偶数求和

#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long

using namespace std;
int a[101], n, m;
void init(){
	for(int i=1; i<=100; i++){
		a[i] = 2*i;
	}
}

int main(){
	init();
	while(scanf("%d%d", &n,&m)!=EOF){
		int x = 1, f=1;
		int sum = 0, count = 0;
		for(int i=1; i<=n; i++){
			sum += a[i];
			count ++; 
			if(i%m==0){ // m个了 
				if(f) { // 打印开头 
					printf("%d", sum/m);
					f = 0;
				}
				else  printf(" %d", sum/m);
				sum = 0;
				count = 0;
			}
		}
		if(sum!=0) printf(" %d", sum/count); // 不足m个 
		f = 1;
		printf("\n");
	}
	return 0;
}

2016 数据的交换输出

#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int a[105];
int n;
int main(){
	while(scanf("%d", &n) && n!=0){
		int min = INF;
		int index = -1;
		for(int i=0; i<n; i++){
			scanf("%d", &a[i]);
			if(a[i]<min){
				min = a[i];
				index = i;
			}
		}
		swap(a[0], a[index]);
		for(int i=0; i<n-1; i++) printf("%d ", a[i]);
		printf("%d\n", a[n-1]);
	}
	return 0;
}

2017 字符串统计

#include<iostream>
#include<cstdio>
#include<cstring>

#define LL long long

using namespace std;

char p[1000000];
int main(){
	int t, len, ans;
	scanf("%d", &t);
	while(t--){
		scanf("%s", p);
		len = strlen(p);
		ans=0;
		for(int i=0; i<len; i++){
			if('0'<=p[i] && p[i]<='9'){
				ans++;
			}
		}
		printf("%d\n", ans);
	}
	
	return 0;
}

2018 母牛的故事
n + n-4 + n-8 + n-12 +… n-4k>=0
*菲波拉契数列是是十分有用的,可以用来描述动物的繁衍过程。 *
f(n) = f(n-1) + f(n-3)
f(n)表示第n年的牛下一年会生牛,
f(n-1)与f(n)是同一头牛,她还会继续生,
f(n-3)表示f(n)那头牛在n-1的时候生的,到了n-3的时候,下一年(第4年)她会生牛
也就是说f(n)等于下一年的自己再加上下一年她生的牛

#include<iostream>
#include<cstdio>
#include<cstring>

#define LL long long

using namespace std;
int dp[105];
int f(int x){
	if(dp[x]!=0)  return dp[x];
	if(x-4<=0)    return dp[x] = x ;
	else          return dp[x] = f(x-1) + f(x-3);
}
int main(){
	dp[1] = 1;
	f(55);
	int n;
	while(scanf("%d",&n) && n!=0){
		printf("%d\n",dp[n]);
	}
	return 0;
}  

2019 数列有序!

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>

#define LL long long 
#define REP(i,n) for(int i=0; i<n; i++)
#define REPP(i,n) for(int i=1; i<=n; i++)
using namespace std;
int n,m, a[200];

int main(){
	while(scanf("%d%d", &n,&m) && (n!=0 || m!=0)){
		REP(i, n) scanf("%d", &a[i]);
		int index = lower_bound(a,a+n,m) - a;
		REP(i, index) printf("%d ",a[i]);
		printf("%d ", m); 
		for(int i=index; i<n-1; i++) printf("%d ", a[i]);
		printf("%d\n", a[n-1]);
	}
} 

2020 绝对值排序

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define REP(i,n) for(int i=0; i<n; i++)
#define REPP(i,n) for(int i=1; i<=n; i++)
#define P pair<int,int>

bool cmp(P a, P b){
	return a.first>b.first;
}
vector<P> ver;
int main(){
	int n;
	while(scanf("%d", &n) && n!=0){
		ver.clear();
		REP(i, n){
			int a;
			scanf("%d", &a);
			ver.push_back(P(abs(a), a));
		}
		sort(ver.begin(),ver.end(), cmp);
		REP(i,n-1){
			printf("%d ", ver[i].second);
		}
		printf("%d\n",ver[n-1].second);
	}
	return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页