zzuli新生训练学习第三天题解

随声附和 提交于 2019-12-13 08:15:00

A - 素数判定

~~
原网址 http://acm.hdu.edu.cn/showproblem.php?pid=2012

对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

Sample Input
0 1
0 0
Sample Output
OK

暴力判断每一项是否为素数即可,有一项不是则跳出循环

#include<bits/stdc++.h>
using namespace std;
int prime(int n);
int main()
{
	int x,y,n,i;
	while(cin>>x>>y,x!=0||y!=0)
	{
		for(i = x; i <= y; i++)
		{
			n=i+i*i+41;
			if(prime(n)==0) break;
		}
		if(i > y) cout<<"OK\n";
		else cout<<"Sorry\n";
	}
	return 0;
} 
int prime(int n)
{
	int i,ret=1;
	for(i = 2; i <= sqrt(n); i++)
	{
		if(n%i == 0)
		{
			ret=0;break;
		}
	}
	if(n < 2) ret=0;
	return ret; 
}

B - 蟠桃记

oj链接 http://acm.hdu.edu.cn/showproblem.php?pid=2013

喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵-
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?

Input
输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。

Output
对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Sample Input
2
4
Sample Output
4
22

从桃子数量为1开始,逆推n-1次就得到结果

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	while(cin>>n!=NULL)
	{
		int peach=1;
		while(--n)
			peach=++peach*2;
		cout<<peach<<endl;
	}
	return 0;
}

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

oj链接 http://acm.hdu.edu.cn/showproblem.php?pid=2014

青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。

Input
输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。

Output
对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。

Sample Input
3 99 98 97
4 100 99 98 97
Sample Output
98.00
98.50

循环输入时判断更新最高最低分,总分累加每一项,结果减去最高最低分后,除以n-2即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i;
	double score[110],ret;
	while(cin>>n!=NULL)
	{
		double max=0,min=100;
		ret=0.0;
		for(i = 0; i < n; i++)
		{
			cin>>score[i];
			if(score[i] < min) min=score[i];
			if(score[i] > max) max=score[i];
			ret+=score[i];
		}
		printf("%.2f\n",1.0*(ret-max-min)/(n-2));
	}
	return 0;
}

D - 偶数求和

oj网址http://acm.hdu.edu.cn/showproblem.php?pid=2015

有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。

Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。

Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。

Sample Input
3 2
4 2
Sample Output
3 6
3 7

设n=6
由 2 4 6 8 10 12易得规律,第一个m项平均数为m+1,下面每m项中,每一位数比前一m项对应位置差m位,则平均数多m*2,如m=3时,8 10 12各比2 4 6大6
则先输出n/m项,若n不能被m整除,单独求后几项平均数即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,i;
    while(cin>>n>>m!=NULL)
    {
        int num=m+1;
        for(i = 0; i < n/m; i++)
        {
            printf(i!=n/m-1?"%d ":"%d",num);
            num+=2*m;
        }
        if(n%m==0) printf("\n");
        else{
            num=0;
            for(i = n/m*m*2+2; i <= n*2; i++)    num+=i;
            printf(" %d\n",num/(n%m));
        }
    }
    return 0;
}

E - 数据的交换输出

oj网址http://acm.hdu.edu.cn/showproblem.php?pid=2016

输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。

Input
输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。

Output
对于每组输入数据,输出交换后的数列,每组输出占一行。

Sample Input
4 2 1 3 4
5 5 4 3 2 1
0
Sample Output
1 2 3 4
1 4 3 2 5

循环输入时记录最小值及对应下标,然后与第一项交换,循环输出即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,jojo[100];
    while(cin>>n,n!=0)
    {
        cin>>jojo[0];
        int min=jojo[0],minid=0;
        for(i = 1; i < n; i++)
        {
            cin>>jojo[i];
            if(jojo[i] < min)
            {
                min=jojo[i];
                minid=i;
            }
        }
        if(i!=0) swap(jojo[0],jojo[minid]);
        for(i = 0; i < n; i++)
            printf(i!=n-1?"%d ":"%d\n",jojo[i]);
    }
    return 0;
}

F - 字符串统计

oj网址http://acm.hdu.edu.cn/showproblem.php?pid=2017

对于给定的一个字符串,统计其中数字字符出现的次数。

Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Sample Input
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf
Sample Output
6
9

循环,遇到数字计数加一即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;
    char jojo[1010];
    cin>>n;
    getchar();
    while(n--)
    {
        int cut=0;
        gets(jojo);
        for(i = 0; jojo[i]!='\0'; i++)
            if(jojo[i] >= '0'&&jojo[i] <= '9')
                cut++;
        cout<<cut<<endl;
    }
    return 0;
}

G - 母牛的故事

oj网址http://acm.hdu.edu.cn/showproblem.php?pid=2018

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Sample Input
2
4
5
0
Sample Output
2
4
6

递推,第一年一头成熟牛,第二年该牛生下一头小牛,这头小牛在第五年成熟并也能生下小牛,所以前四年为1 2 3 4,以后每一年,总牛数为前一年牛加新增牛,即jojo[i] = jojo[i-1] + jojo[i-3]。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,i,jojo[55];
    for(i = 0; i < 5; i++) jojo[i]=i;
    for(i; i < 55; i++) jojo[i]=jojo[i-1]+jojo[i-3];
    while(cin>>n,n!=0)
        cout<<jojo[n]<<endl;
    return 0;
}

H - 数列有序!

oj网址http://acm.hdu.edu.cn/showproblem.php?pid=2019

有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output
对于每个测试实例,输出插入新的元素后的数列。

Sample Input
3 3
1 2 4
0 0
Sample Output
1 2 3 4

循环输入数组时,找到不小于x的数,在其前面把x插入数组即可

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,i,jojo[110];
    while(cin>>n>>m,n!=0||m!=0)
    {
        int flag=1;
        for(i = 0; i < n; i++)
        {
            cin>>jojo[i];
            if(jojo[i] >= m&&flag)
            {
                jojo[i]=jojo[i++];
                jojo[i-1]=m;
                n++;flag=0;
            }
        }
        for(i = 0; i < n; i++)
            printf(i!=n-1?"%d ":"%d\n",jojo[i]);        
    }
    return 0;
}

I - There Are Two Types Of Burgers

oj网址http://codeforces.com/contest/1207/problem/A

There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet.
You have b buns, p beef patties and f chicken cutlets in your restaurant. You can sell one hamburger for h dollars and one chicken burger for c dollars. Calculate the maximum profit you can achieve.

You have to answer t independent queries.

Input
The first line contains one integer t (1≤t≤100) – the number of queries.

The first line of each query contains three integers b, p and f (1≤b, p, f≤100) — the number of buns, beef patties and chicken cutlets in your restaurant.

The second line of each query contains two integers h and c (1≤h, c≤100) — the hamburger and chicken burger prices in your restaurant.

Output
For each query print one integer — the maximum profit you can achieve.

Example
Input
3
15 2 3
5 10
7 5 2
10 12
1 100 100
100 100
Output
40
34
0

判断hamburger和chicken cutlet单价谁高,先从最高的开始做,相同则随意哪个开始都行,每做一个减去2片面包1块对应的肉,若材料有剩余,做另一个即可

#include<iostream>
using namespace std;
int main()
{
	int t,b,p,f,h,c;
	cin>>t;
	while(t--)
	{
		cin>>b>>p>>f;
		cin>>h>>c;
		int max=0,num=b/2;
		if(h >= c){
			while(p&&b >= 2)
			{
				max+=h;
				p--;
				b-=2;
			}
			while(f&&b >= 2)
			{
				max+=c;
				f--;
				b-=2;
			}
		}else{
			while(f&&b >= 2)
			{
				max+=c;
				f--;
				b-=2;
			}
			while(p&&b >= 2)
			{
				max+=h;
				p--;
				b-=2;
			}
		}
		cout<<max<<endl;
	}
	return 0;
}

el psy congroo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!