3.1 简单模拟

/*
问题 A: 剩下的树
题目描述
有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L+1棵树。
现在要移走一些树,移走的树的区间用一对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。
可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。

输入
两个整数L(1<=L<=10000)和M(1<=M<=100)。
接下来有M组整数,每组有一对数字。
输出
可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。

样例输入
4 2
1 2
0 2
11 2
1 5
4 7
0 0
样例输出
2
5
提示
*/

#include<cstdio>
const int maxn = 10010;
int main(){
	int L,M;
	while(scanf("%d%d",&L,&M)!=EOF){
		int tree[maxn] = {0};
		if(L==0 && M==0) break;
		for(int i=0;i<M;i++){
			int left,right;
			scanf("%d%d",&left,&right);
			
			for(int j=left;j<=right;j++){
				tree[j] = -1;
			}
		}
		int count=0;
		for(int i=0;i<=L;i++){
			//printf("%d ",tree[i]);
			if(tree[i]!=-1){
				count++;
			}
		}
		//printf("\n");
		printf("%d\n",count);
		
	}
} 
/*
问题 B: A+B
题目描述
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。

输入
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
输出
请计算A+B的结果,并以正常形式输出,每组数据占一行。

样例输入
-234,567,890 123,456,789
1,234 2,345,678
样例输出
-111111101
2346912
*/

#include<cstdio>
#include<iostream>
using namespace std;
int solve(string a){
	int num=0;
	if(a[0]=='-'){
		for(int i=1;i<a.length();i++){
			if(a[i]>='0' && a[i]<='9'){
				num = num*10 + a[i]-'0';
			} 	
		}
		num = -num;
	}else{
		for(int i=0;i<a.length();i++){
			if(a[i]>='0' && a[i]<='9'){
				num = num*10 + a[i]-'0';
			}
		}
	}
	return num;
} 

int main(){
	string a,b;
	while(cin>>a>>b){
		cout<<solve(a)+solve(b)<<endl;
	}
	return 0;
} 
/*
问题 C: 特殊乘法
题目描述
写个算法,对2个小于1000000000的输入,求结果。特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5

输入
 两个小于1000000000的数
输出
 输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。

样例输入
24 65
42 66666
3 67
样例输出
66
180
39
*/ 

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	string a,b;
	while(cin>>a>>b){
		int result=0;
		for(int i=0;i<a.length();i++){
			for(int j=0;j<b.length();j++){
				result += (a[i]-'0') * (b[j]-'0');
			}
		}
		cout<<result<<endl;
	}
} 
/*
问题 D: 比较奇偶数个数
题目描述
第一行输入一个数,为n,第二行输入n个数,这n个数中,如果偶数比奇数多,输出NO,否则输出YES。

输入
输入有多组数据。
每组输入n,然后输入n个整数(1<=n<=1000)。
输出
如果偶数比奇数多,输出NO,否则输出YES。

样例输入
1
67 
7
0 69 24 78 58 62 64 
样例输出
YES
NO
*/

#include<cstdio>
int main(){
	int n,x;
	while(scanf("%d",&n)!=EOF){
	    int count1=0,count2=0;
		for(int i=0;i<n;i++){
			scanf("%d",&x);
			if(x % 2 == 0) count1++;
			else count2++;
		}
		//printf("%d %d\n",count1,count2);
		if(count1>count2) printf("NO\n");
		else printf("YES\n");
	}	
}
/*
问题 E: Shortest Distance (20)
题目描述
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

输入
Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.
输出
For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

样例输入
5 1 2 4 14 9
3
1 3
2 5
4 1
样例输出
3
10
7
*/

#include<cstdio>
int main(){
	int N,M,x,y;
	int sum1 = 0;
	scanf("%d",&N);
	int dis[N],total[N];
	total[0]=0;
	for(int i=1;i<=N;i++){
		scanf("%d",&dis[i]);
		sum1 += dis[i];
		total[i] = sum1; //用数组存储较快 
	}
	scanf("%d",&M);
	for(int i=1;i<=M;i++){
		scanf("%d%d",&x,&y);
		if(x>y){
			int temp=x;
			x=y;
			y=temp;
		}
		//以下注释的方法的运行效率对于大数据来说太慢 
//		int sum2 = 0;
//		for(int i=x;i<=y-1;i++){
//			sum2 += dis[i];
//		}
//		if(sum2<=sum1-sum2) printf("%d\n",sum2);
//		else printf("%d\n",sum1-sum2);
        int count1 = total[y-1] - total[x-1];
        int count2 = total[N] - count1;
        if(count1<=count2) printf("%d\n",count1);
        else printf("%d\n",count2);
	}
}
/*
问题 F: A+B和C (15)
题目描述
给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。

输入
输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。
输出
对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

样例输入
4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647
样例输出
Case #1: false
Case #2: true
Case #3: true
Case #4: false
*/ 

#include<cstdio>
#include<iostream>
using namespace std;
/*
	int取值范围:-2^31 ~ (2^31-1)
	注意本题:给定区间[-2^31, 2^31] 
*/
int main(){
	int T;
	long long A,B,C;
	scanf("%d",&T);
	for(int i=1;i<=T;i++){
		//如果long long型赋大于2^31-1的初值,则需要在初值后面加上LL,否则会编译错误
		//这里用cin避免分类讨论 
		cin>>A>>B>>C;
		if(A+B>C) printf("Case #%d: true\n",i);
		else printf("Case #%d: false\n",i);
	}
} 

/*
问题 G: 数字分类 (20)
题目描述
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
A1 = 能被5整除的数字中所有偶数的和;
A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
A3 = 被5除后余2的数字的个数;
A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
A5 = 被5除后余4的数字中最大数字。

输入
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。
输出
对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。
若其中某一类数字不存在,则在相应位置输出“N”。

样例输入
13 1 2 3 4 5 6 7 8 9 10 20 16 18
8 1 2 4 5 6 7 9 16
样例输出
30 11 2 9.7 9
N 11 2 N 9
*/

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
	int N;
	while(cin>>N){
		int num[N+1];
		for(int i=0;i<N;i++){
			cin>>num[i];
		}
		
		int A1=0,A2=0,A3=0,A5=0;
		double A4=0.0;
		int flag1=0,flag2=0,flag3=0,flag4=0,flag5=0; 
		int count1=0,count2=0,sum=0;
		for(int i=0;i<N;i++){
			int temp = num[i]%5;
			if(temp==0 && num[i]%2==0){
				flag1=1;
				A1 += num[i];
			}
			if(temp==1){
				flag2=1;
				A2 += pow(-1,count1)*num[i];
				count1++;
			}
			if(temp==2){
				flag3=1;
				A3++;
			}
			if(temp==3){
				flag4=1;
				sum += num[i];
				count2++;
			}
			
			if(temp==4){
				flag5=1;
				if(A5<num[i]){
					A5=num[i];
				}
			}
		}
		if(sum){
			A4=(double)sum/count2;
		}
		
		if(flag1) printf("%d ",A1);
		else printf("%c ",'N');
		if(flag2) printf("%d ",A2);
		else printf("%c ",'N');
		if(flag3) printf("%d ",A3);
		else printf("%c ",'N');
		if(flag4) printf("%.1f ",A4);
		else printf("%c ",'N');
		if(flag5) printf("%d\n",A5);
		else printf("%c\n",'N');
	}
}
/*
问题 H: 部分A+B (15)
题目描述
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。
现给定A、DA、B、DB,请编写程序计算PA + PB。

输入
输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。
输出
在一行中输出PA + PB的值。

样例输入
3862767 6 13530293 3
3862767 1 13530293 8
样例输出
399
0
*/

//Way1: 
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
	long long A,B;
	int DA,DB;
	while(cin>>A>>DA>>B>>DB){
		int sum1=0,sum2=0;
		int result1=0,result2=0;
		while(A>0){
			int temp1=A%10;
			if(temp1==DA) sum1++;
			A = A/10;
		}
		while(B>0){
			int temp2=B%10;
			if(temp2==DB) sum2++;
			B = B/10;
		}
		for(int i=0;i<sum1;i++){
			result1 += DA * pow(10, i);
		}
		for(int i=0;i<sum2;i++){
			result2 += DB * pow(10, i);
		}
		cout<<result1+result2<<endl;
	}
} 

//Way2:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
    char A[11], B[11];
    char DA, DB;
    while(cin>>A>>DA>>B>>DB){
    	int count1 = 0, count2 = 0;
		int sum1 = 0, sum2 = 0;
    	for(int i=0;i<strlen(A);i++){
    		if(A[i]==DA){
            	count1++;
        	}
    	}
    	for(int i=0;i<strlen(B);i++){
    		if(B[i]==DB){
            	count2++;
        	}
    	}
		for(int i=0;i<count1;i++){
			sum1 += (int)(DA-'0') * pow(10, i);
		}
		for(int i=0;i<count2;i++){
			sum2 += (int)(DB-'0') * pow(10, i);
		}
		cout<<sum1+sum2<<endl; 
	}
    return 0;
}
/*
问题 I: 锤子剪刀布 (20)
题目描述
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入
输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。
输出
输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯一,则输出按字母序最小的解。

样例输入
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J
样例输出
5 3 2
2 3 5
B B
*/

#include<cstdio>
#include<iostream>
using namespace std;
/*
	C:锤子
	J:剪刀
	B:布 
*/
int main(){
	int N;
	while(cin>>N){
		int xwin=0,ywin=0,tie=0;
		int xmax[91]={0},ymax[91]={0};
		char x,y;
		for(int i=0;i<N;i++){
			cin>>x>>y;
			if((x=='C' && y=='J') || (x=='J' && y=='B') || (x=='B' && y=='C') ){
				xwin++;
				xmax[x]++;
			}
			if((x=='C' && y=='C') || (x=='J' && y=='J') || (x=='B' && y=='B') ){
				tie++;
			}
			if((y=='C' && x=='J') || (y=='J' && x=='B') || (y=='B' && x=='C') ){
				ywin++;
				ymax[y]++;
			}
		}
			
		cout<<xwin<<" "<<tie<<" "<<ywin<<endl;
		cout<<ywin<<" "<<tie<<" "<<xwin<<endl;
		
		if(xmax['B']>=xmax['C']){
			if(xmax['B']>=xmax['J']) cout<<"B"<<" ";
			else cout<<"J"<<" ";
		}else{
			if(xmax['C']>=xmax['J']) cout<<"C"<<" ";
			else cout<<"J"<<" ";
		}
		
		if(ymax['B']>=ymax['C']){
			if(ymax['B']>=ymax['J']) cout<<"B"<<" ";
			else cout<<"J"<<endl;
		}else{
			if(ymax['C']>=ymax['J']) cout<<"C"<<" ";
			else cout<<"J"<<endl;
		}
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【问题描述】 客户业务分为两种。第一种是申请从银行得到一笔资金,即取款或借款。第二种是向银行投入一笔资金,即存款或还款。银行有两个服务窗口,相应地有两个队列。客户到达银行后先排第一个队。处理每个客户业务时,如果属于第一种,且申请额超出银行现存资金总额而得不到满足,则立刻排入第二个队等候,直至满足时才离开银行;否则业务处理完后立刻离开银行。每接待完一个第二种业务的客户,则顺序检查和处理(如果可能)第二个队列中的客户,对能满足的申请者予以满足,不能满足者重新排到第二个队列的队尾。注意,在此检查过程中,一旦银行资金总额少于或等于刚才第一个队列中最后一个客户(第二种业务)被接待之前的数额,或者本次已将第二个队列检查或处理了一遍,就停止检查(因为此时已不可能还有能满足者)转而继续接待第一个队列的客户。任何时刻都只开一个窗口。假设检查不需要时间。营业时间结束时所有客户立即离开银行。 写一个上述银行业务的事件驱动模拟系统,通过模拟方法求出客户在银行内逗留的平均时间。 【基本要求】 利用动态存储结构实现模拟。 【测试数据】 一天营业开始时银行拥有的款额为10000(元),营业时间为600(分钟)。其他模拟参 量自定,注意测定两种极端的情况:一是两个到达事件之间的间隔时间很短,而客户的交易时间很长,另一个恰好相反,设置两个到达事件的间隔时间很长,而客户的交易时间很短。 【实现提示】 事件有两类:到达银行和离开银行。初始时银行现存资金总额为total。开始营业后的第一今事件是客户到达,营业时间从0到closetime。到达事件发生时随机地设置此客户的交易时间和距下一到达事件之间的时间间隔。每个客户要办理的款额也是随机确定的,用负值和正值分别表示第一类和第二类业务。变量total,closetime以及上述两个随机量的上下界均交互地从终端读入,作为模拟参数。 两个队列和一个事件表均要用动态存储结构实现。注意弄清应该在什么条件下设置离开事件,以及第二个队列用怎样的存储结构实现时可以获得较高的效率。注意:事件表是按时间顺序有序的。 【选作内容】 自己实现动态数据类型。例如对于客户结点,定义pool为 CustNodepoolfMAX]; // 结构类型CustNode含四个域:aITHIne,durtime,amount,next 或者定义四个同样长的,以上述域名为名字的数组。初始时,将所有分量的next域链接起来,形成一个静态链找,设置一个楼顶元素下标指示量top,top=0表示找空。动态存储分配函数可以取名为myMalloc,其作用是出钱,将钱顶元素的 下标返回。若返回的值为0,则表示无空间可分配。归还函数可取名为myFree,其作用是把该分量入钱。用FOR-TRAN和BASIC等语言实现时只能如此地自行组织。 自己手写的数据结构作业,选做部分也实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值