《算法笔记》3.1小节——入门模拟->简单模拟

目录

问题 A: 剩下的树

问题 B: A+B

问题 C: 特殊乘法

问题 D: 比较奇偶数个数

问题 E: Shortest Distance (20)

问题 F: A+B和C (15)

问题 G: 数字分类 (20)

问题 H: 部分A+B (15)

问题 I: 锤子剪刀布 (20)


问题 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>
int main(){
	int i;
	int L, M;
	while(scanf("%d%d", &L, &M)){
		int tree[100000] = {0};//初始化标记数组
		if(L == 0 && M == 0){
			break;
		}
		int count = 0;
		int start, end;
		while(M--){
			scanf("%d%d", &start, &end);
			for(i = start; i <= end; i++){
				tree[i] = 1;//移走树
			}
		}
		for(i = 0; i <= L; i++){
			if(tree[i] == 0){
				count++;
			}
		}
		printf("%d\n", count);
	}
	return 0;
}

问题 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 <string.h>
int main(){
	char str1[100], str2[100];
	while(scanf("%s %s", &str1, &str2) != EOF){
		int i;
		long long A = 0, B = 0;
		for(i = 0; i < strlen(str1); i++){
			if(str1[i] >= '0' && str1[i] <= '9'){
				A = A * 10 + str1[i] - '0';
		    }
		    
		}
		if(str1[0] == '-'){
		   	A = -A;
		}
		for(i = 0; i < strlen(str2); i++){
			if(str2[i] >= '0' && str2[i] <= '9'){
				B = B * 10 + str2[i] - '0';
		    } 
		}
		if(str2[0] == '-'){
		   	B = -B;
		}
  		printf("%lld\n", A + B);
	}
	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 <string.h>
int main(){
	char str1[100], str2[100];
	while(scanf("%s %s", &str1, &str2) != EOF){
		int i, j;
		int sum = 0;
		for(i = 0; i < strlen(str1); i++){
			for(j = 0; j < strlen(str2); j++){
				sum = sum + (str1[i] - '0') * (str2[j] - '0');
		    }
		}
  		printf("%d\n", sum);
	}
	return 0;
}

问题 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>
#include <string.h>
int main(){
	int n;
	while(scanf("%d", &n) != EOF){
		int a[100000];
		int i;
		int odd = 0, even = 0;//记录奇数、偶数的个数
		for(i = 0; i < n; i++){
			scanf("%d", &a[i]);
			if(a[i] % 2 == 0){
				even++;
			}
			else{
				odd++;
			}
		}
		if(even > odd){
			printf("NO\n");
		}
		else{
			printf("YES\n");
		}
	}
	return 0;
}

问题 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

翻译

给出N个点之间的距离,N个点构成了一个环,需要计算某两点之间的最短距离。

题解

#include <cstdio>
#include <string.h>
int main(){
	int N;
	scanf("%d", &N);
	int distance[N + 1];//各点之间的距离
	int i;
	int sum = 0;
	for(i = 1; i <= N; i++){
		scanf("%d", &distance[i]);
		sum = sum + distance[i];//所有点距离之和
	}
	int n;
	scanf("%d", &n);
	while(n--){
		int start, end;
		scanf("%d%d", &start, &end);
		int temp;
		if(start > end){//起点需小于终点
			temp = start;
			start = end;
			end = temp;
		}
		int dis = 0;//正向距离之和
		for(i = start; i < end; i++){
			dis = dis + distance[i];
		}
		printf("%d\n", dis > sum - dis ? sum - dis : dis);//比较正向和逆向距离之和的大小
	}
	return 0;
}

问题 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 <string.h>
int main(){
	long long A, B, C;
	int T;
	scanf("%d", &T);
	int i = 1;
	while(T--){
		scanf("%lld%lld%lld", &A, &B, &C);
		if((A + B) > C){
			printf("Case #%d: true\n", i++);
		}
		else{
			printf("Case #%d: false\n", i++);
		}
	}
	return 0;
}

问题 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

题解

本题坑的地方是对A2进行交错求和,如果为0,应该输出0而不是N。(下面代码排版错乱,直接复制即可!)

#include <cstdio>
#include <string.h>
int main(){
	int N;
	scanf("%d", &N);
	int data;
	int sum1 = 0, sum2 = 0, count2 = 0, count3 = 0, sum4 = 0, count4 = 0, max5 = 0, flag = 1;
	int i;
	for(i = 0; i < N; i++){
		scanf("%d", &data);
		switch(data % 5){
			case 0 :
				if(data % 2 == 0){
					sum1 += data;
				}
				break;
            case 1 :
				sum2 += flag * data;
            	flag = -flag;
            	count2++;
            	break;
			case 2 :
				count3++;
				break;
			case 3 :
				sum4 += data;
            	count4++;
            	break;
            case 4 :
            	if(data > max5){
            		max5 = data;
				}
            	break;
		}
	}
	if(sum1 > 0 && sum2 > 0 && count3 > 0 && count4 > 0 && max5 > 0){
		printf("%d %d %d %.1f %d\n", sum1, sum2, count3, 1.0 * sum4 / count4, max5);
	}
    else{
        if(sum1 != 0){
        	printf("%d ", sum1);
		}
        else{
        	printf("N ");
		}
        if(sum2 != 0){
        	printf("%d ", sum2);
		}
        else{
        	if(count2 != 0){//交错求和为0
        		printf("%d ", sum2);
			}
			else{//本来为0
				printf("N ");
			}
		}
        if(count3 != 0){
        	printf("%d ", count3);
		}
        else{
        	printf("N ");
		}
        if(sum4 != 0){
        	printf("%.1f ", 1.0 * sum4 / count4);
		}
        else{
        	printf("N ");
		}
        if(max5 != 0){
        	printf("%d", max5);
		}
        else{
         	printf("N");
		}
    }
	return 0;
}

问题 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

题解

#include <cstdio>
#include <string.h>
int main(){
	char str1[100], str2[100];
	char A, B;
	int sum1 = 0, sum2 = 0;
	scanf("%s %c %s %c", str1, &A, str2, &B);
	int i;
	for(i = 0; i < strlen(str1); i++){
		if(str1[i] == A){
			sum1 = sum1 * 10 + (str1[i] - '0');
		}
	}
	for(i = 0; i < strlen(str2); i++){
		if(str2[i] == B){
			sum2 = sum2 * 10 + (str2[i] - '0');
		}
	}
	printf("%d\n", sum1 + sum2);
	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

题解

本题坑的地方是可能存在一次都没有赢的情况,此时获胜次数最多的手势为B。

使用scanf读取有效字符时一定要在前面加空格,因为:对于scanf()而言,%c是个较为特殊的说明符。 %c前没空格,scanf()将读取标准输入流中的第一个字符,%c前有空格,scanf()则读取标准输入流中第一个非空白字符。

由于scanf使用%c的时候是会将换行符\n读入的,因此需要在合适的地方getchar一下以吸收空格,否则读入会不按自己想法来的。如果程序输入数据后闪退,那么基本上就是这个问题。

#include <cstdio>
#include <string.h>
int main(){
	int N;
	scanf("%d", &N);
	int i;
	char A, B;
	int victory1[3] = {0}, victory2[3] = {0};//甲、乙获胜手势J、C、B的次数
	int count1 = 0, count2 = 0;//甲、乙获胜次数
	int temp = N;
	while(N--){
		getchar();//吸收\n换行符
		scanf("%c %c", &A, &B);
		if(A == 'J' && B == 'C'){
			count2++;
			victory2[1]++;
		}
		else if(A == 'J' && B == 'B'){
			count1++;
			victory1[0]++;
		}
		else if(A == 'C' && B == 'J'){
			count1++;
			victory1[1]++;
		}
		else if(A == 'C' && B == 'B'){
			count2++;
			victory2[2]++;
		}
		else if(A == 'B' && B == 'J'){
			count2++;
			victory2[0]++;
		}
		else if(A == 'B' && B == 'C'){
			count1++;
			victory1[2]++;
		}
	}
	int max1 = victory1[0];
	int temp1;
	for(i = 0; i < 3; i++){
		if(victory1[i] >= max1){
			temp1 = i;
		}
	}
	int max2 = victory2[0];
	int temp2;
	for(i = 0; i < 3; i++){
		if(victory2[i] >= max2){
			temp2 = i;
		}
	}
	printf("%d %d %d\n", count1, temp - count1 - count2, count2);
	printf("%d %d %d\n", count2, temp - count1 - count2, count1);
	if(temp1 == 0){
		printf("J ");
	}
	else if(temp1 == 1){
		printf("C ");
	}
	else{
		printf("B ");
	}
	if(temp2 == 0){
		printf("J\n");
	}
	else if(temp2 == 1){
		printf("C\n");
	}
	else{
		printf("B\n");
	}
	return 0;
}
发布了84 篇原创文章 · 获赞 97 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/wyatt007/article/details/103781013