杭电OJ第11页2085~2089算法题(C语言)

2085.核反应堆

Problem Description
某核反应堆有两类事件发生:
高能质点碰击核子时,质点被吸收,放出3个高能质点和1个低能质点;
低能质点碰击核子时,质点被吸收,放出2个高能质点和1个低能质点。
假定开始的时候(0微秒)只有一个高能质点射入核反应堆,每一微秒引起一个事件发生(对于一个事件,当前存在的所有质点都会撞击核子),试确定n微秒时高能质点和低能质点的数目。
 
Input
输入含有一些整数n(0≤n≤33),以微秒为单位,若n为-1表示处理结束。
 
Output
分别输出n微秒时刻高能质点和低能质点的数量,高能质点与低能质点数量之间以逗号空格分隔。每个输出占一行。
 
Sample Input
5 2
-1
 
Sample Output
571, 209
11, 4

提示
可以使用long long int对付GNU C++,使用__int64对付VC6

分析:根据题目中的条件,直接使用for循环即可。

#include <stdio.h>

void NuclearReactor(){
	int n,i;
	__int64 high,low,temp;
	while(scanf("%d",&n)!=EOF && n!=(-1) && (n>=0 && n<=33)){
		//0微妙时,只有一个高能质子 
		high=1;
		low=0;
		for(i=1;i<=n;i++){
			temp=high;
			high=3*high+2*low;
			low+=temp;
		}
		printf("%I64d, %I64d\n",high,low);
	}
}

2086.A1 = ?

Problem Description
有如下方程:Ai = (Ai-1 + Ai+1)/2-Ci (i = 1, 2, 3, .... n).
若给出A0, An+1 , 和 C1, C2, .....Cn.
请编程计算A1 = ?
 
Input
输入包括多个测试实例。
对于每个实例,首先是一个正整数n,(n <= 3000); 然后是2个数a0, an+1.接下来的n行每行有一个数ci(i = 1, ....n);输入以文件结束符结束。
 
Output
对于每个测试实例,用一行输出所求得的a1(保留2位小数).
 
Sample Input
1
50.00
25.00
10.00
2
50.00
25.00
10.00
20.00
 
Sample Output
27.50
15.00

分析:可以将本题看成一个找规律的数学题:
An = (1/2)An-1 + (1/2)An+1 - Cn
An-1 = (2/3)An-2 + (1/3)An+1 - (2/3)Cn - (4/3)Cn-1
An-2 = (3/4)An-3 + (1/4)An+1 - (1/2)Cn - Cn-1 - (3/2)Cn-2
An-3 = (4/5)An-4 + (1/5)An+1 - (2/5)Cn - (4/5)Cn-1 - (6/5)C~n-2 - (8/5)Cn-3

所以可以得到
A1 = (n/(n+1))A0 + (1/(n+1))An+1 - (2/(n+1))Cn - (4/(n+1))Cn-1 - … -(2n/(n+1))C1
= [ nA0+ An+1 - 2(Cn + 2Cn-1 + 3Cn-2 + … + nC1) ]/(n+1)
最后在根据结论编写代码即可。

#include <stdio.h>

void A1(){
	int n,i;
	double A0,An,A1,temp;
	//C[i]=Ci 
	double C[3001];
	while(scanf("%d",&n)!=EOF){
		scanf("%lf%lf",&A0,&An);
		for(i=1;i<=n;i++){
			scanf("%lf",&C[i]);
		}
		A1=1.0*n/(n+1)*A0+1.0/(n+1)*An;
		temp=1.0*n*2;
		for(i=1;i<=n;i++){
			A1-=temp/(n+1)*C[i];
			temp-=2;
		}
		printf("%.2lf\n",A1);
	}
}

2087.剪花布条

Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?

Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。

Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。

Sample Input
abcde a3
aaaaaa  aa
#
 
Sample Output
0
3

分析:使用串的模式匹配算法——KMP算法,不过本题需要统计匹配成功的次数,即能从花纹布中剪出的最多小饰条个数。

#include <stdio.h>

//KMP算法
void GetNextval(char t[],int nextval[]){
	int length=strlen(t);
	int j=0,k=-1;
	nextval[0]=-1;
	while(j<length){
		if(k==-1 || t[j]==t[k]){
			j++;
			k++;
			if(t[j]!=t[k]){
				nextval[j]=k;
			}else{
				nextval[j]=nextval[k];
			}
		}else{
			k=nextval[k];
		}
	}
} 

void CutDown(){
	char cloth1[1001],cloth2[1001];
	int length1,length2,count;
	int i,j;
	int nextval[1001]={0};
	while(scanf("%s %s",cloth1,cloth2)!=EOF){
		if(cloth1[0]=='#'){
			break;
		}
		GetNextval(cloth2,nextval);
		length1=strlen(cloth1);
		length2=strlen(cloth2);
		i=0;
		count=0;
		for(i=0;i<length1;){
			j=0;
			while(i<length1 && j<length2){
				if(j==-1 || cloth1[i]==cloth2[j]){
					i++;
					j++;
				}else{
					j=nextval[j];
				}
			}
			if(j>=length2){
				count++;
			}
		}
		printf("%d\n",count);
	}
}

2088.Box of Bricks

Problem Description
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. “Look, I've built a wall!, he tells his older sister Alice. “Nah, you should make all stacks the same height. Then you would have a real wall., she retorts. After a little consideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?

 
Input
The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1≤n≤50 and 1≤hi≤100.

The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.

The input is terminated by a set starting with n = 0. This set should not be processed.

Output
For each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line between each set.

Sample Input
6
5 2 4 1 7 5
0

Sample Output
5

在这里插入图片描述

分析:先求出平均值,然后再将平均值与所有低于平均值的栈高度作差,将所有差相加即可以得到结果。

#include <stdio.h>

void BoxofBricks(){
	//n:栈的个数
	int n,sum,bricks[101]={0},count;
	int i;
	while(scanf("%d",&n)!=EOF && n){
		sum=0;
		for(i=0;i<n;i++){
			scanf("%d",&bricks[i]);
			sum+=bricks[i];
		}
		count=0;
		sum/=n;
		for(i=0;i<n;i++){
			if(sum>bricks[i]){
				count+=(sum-bricks[i]);
			}
		}
		printf("%d\n",count);
	}
}

2089.不要62

Problem Description
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有462的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有62,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。

Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。

Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。

Sample Input
1 100
0 0

Sample Output
80

分析:可以单独写一个判断是否为吉利数字的函数,然后再对n~m之间的数分别进行判断,统计不是不吉利数字的个数即可。

#include <stdio.h>

//判断是否为不吉利数字,若是则返回1,否则返回0 
int Check(int n){
	int temp;
	while(n){
		temp=n%10;
		if(temp%10==4){
			return 1;
		}else if(temp==2 && (n%100)/10==6){
			return 1;
		}
		n/=10;
	}
	return 0;
}

void No62(){
	int n,m,i,count;
	while(scanf("%d %d",&n,&m)!=EOF){
		if(n==0 && m==0){
			break;
		}
		count=0;
		for(i=n;i<=m;i++){
			if(Check(i)==0){
				count++;
			}
		}
		printf("%d\n",count);
	}
}

杭电OJ第11页2090~2094算法题(C语言)

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值