《算法笔记》3.1-简单模拟-题解

第一章和第二章较简单,不再赘述。

算法笔记3.1-A题-剩下的树

题目描述

#include 
#include 
int a[10005];
int main(void)
{
	int L, M, cnt;
	while(scanf("%d %d", &L, &M) != EOF , L || M)
	{
		memset(a, -1, sizeof(a));
		cnt = 0;
		while(M--)
		{
			int open, end;
			scanf("%d %d", &open, &end);
			for(int i = open; i <= end; i++)
				if(a[i]) {a[i] = 0; cnt++;}
		}
		printf("%d\n", L + 1 - cnt);	//注意有L+1棵树
	}
	return 0;
}

算法笔记3.1-B题-A+B

题目描述

#include 
#include 
#include 
int main(void)
{
	char strA[100], strB[100];
	long long A = 0, B = 0, sum;
	while(scanf("%s %s", strA, strB) != EOF)
	{
		A = 0;			
		B = 0;			//需要循环加减的变量一定记得初始化
		int lenA = strlen(strA), lenB = strlen(strB);
		for(int i = 0; i < lenA; i++)
			if(isdigit(strA[i])) {A *= 10; A += strA[i] - '0';}
		if(strA[0] == '-') 
		    A *= -1;
		for(int j = 0; j < lenB; j++)
			if(isdigit(strB[j])) {B *= 10; B += strB[j] - '0';}
		if(strB[0] == '-') 
		    B *= -1;
		printf("%lld\n", A + B);
		memset(strA, 0, sizeof(strA));
		memset(strB, 0, sizeof(strB));
	}
	return 0;
}

算法笔记3.1-C题-特殊乘法

题目描述

#include 
#include 
int main(void)
{
	char a[20], b[20];
	long long sum = 0;
	while(scanf("%s %s", &a, &b) != EOF)    //这里两个%s之间的空格不能省略
	{
		int lena = strlen(a), lenb = strlen(b);
		sum = 0;	//维护
		for(int i = 0; i < lena; i++)
		{
			for(int j = 0; j < lenb; j++)
				{sum += (a[i] - '0') * (b[j] - '0');}
		}
		printf("%lld\n", sum);
	}
	return 0;
}

算法笔记3.1-D题-比较奇偶数个数

题目描述

#include 
int a[1005];
int main(void)
{
	int n, cnt_even = 0, cnt_odd = 0;
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
			if(a[i] %2 == 0) cnt_even++;
			else cnt_odd++;
		}
		//注意YES和NO都是大写
		if(cnt_even > cnt_odd) {printf("NO\n");}
		else {printf("YES\n");}
	}
	return 0;
}

算法笔记3.1-E题-Shortest Distance

题目描述

//把路口抽象成环,类似于前缀和的思想
#include 
int a[100005], dis[100005];
int main(void)
{
	int N, M, dx, dy, disa, disb, sum = 0;
	scanf("%d", &N);
	for(int i = 1; i <= N; i++)
	{
		scanf("%d", &a[i]);		//i和i+1路口间的距离
		sum += a[i];  			//一整圈的距离
		dis[i] = sum;			//1到i+1路口的距离
	}
	scanf("%d", &M);
	for(int j = 1; j <= M; j++)
	{
		scanf("%d %d", &dx, &dy);
		if(dx < dy) disa = dis[dy-1] - dis[dx-1];
		else disa = dis[dx-1] - dis [dy-1];
		disb = sum - disa;
		printf("%d\n", disa < disb ? disa : disb);
	}
	return 0;
}

算法笔记3.1-F题-A+B和C

题目描述

#include 
using namespace std;
int main(void)
{
	int T, kase = 0;;
	long long A, B, C;
	cin >> T;
	while(T--)
	{
		cin >> A >> B >> C;
		if(A + B > C) cout << "Case #" << ++kase << ":" << " " << "true" <<endl;
		else cout << "Case #" << ++kase << ":" << " " << "false" <<endl;
	}
	return 0;
}

算法笔记3.1-G题-数字分类

题目描述

//根据题意模拟即可,注意格式
#include 
int a[1005];
int main(void)
{
	int N, sum_A1 = 0, sum_A2 = 0, cnt_A3 = 0, cnt_A4 = 0, max_A5, flag = 1, cnt_A2 = 0;
	double sum_A4 = 0, aver_A4;
	scanf("%d", &N);
	for(int i = 0; i < N; i++)
		{scanf("%d", &a[i]);}
	max_A5 = 0;
	for(int j = 0; j < N; j++)
	{
		if(a[j] %10 == 0) 
		    sum_A1 += a[j];
		else if(a[j] %5 == 1) 
		    {sum_A2 += a[j] * flag; flag *= -1; cnt_A2++;}
		else if(a[j] %5 == 2) 
		    cnt_A3++;
		else if(a[j] %5 == 3) 
		    {cnt_A4++; sum_A4 += a[j];}
		else if(a[j] %5 == 4)
			{if(a[j] > max_A5) {max_A5 = a[j];}}
	}
	if(cnt_A4) aver_A4 = sum_A4 / cnt_A4;
	    else aver_A4 = 0;
	if(sum_A1) printf("%d ", sum_A1);
	    else printf("N ");
	if(sum_A2 || cnt_A2) printf("%d ", sum_A2);
	    else printf("N ");
	if(cnt_A3) printf("%d ", cnt_A3);
	    else printf("N ");
	if(aver_A4) printf("%.1f ", aver_A4);
	    else printf("N ");
	if(max_A5) printf("%d\n", max_A5);
	    else printf("N\n");
	return 0;
}

算法笔记3.1-H题-部分A+B

题目描述

#include
#include
char A[1000], B[1000];
int main(void)
{
	int Da, Db, Na, Nb, Pa = 0, Pb = 0, sum = 0;
	scanf("%d", &Na);
	sprintf(A, "%d", Na);
	scanf("%d", &Da);
	scanf("%d", &Nb);
	sprintf(B, "%d", Nb);
	scanf("%d", &Db);
	int lenA = strlen(A), lenB = strlen(B);
	for(int i = 0; i < lenA; i++)
	{
		if(A[i] - '0' == Da) Pa = Pa * 10 + Da;
		else continue;
	}
	for(int j = 0; j < lenB; j++)
	{
		if(B[j] - '0' == Db) Pb = Pb * 10 + Db;
		else continue;
	}
	sum = Pa + Pb;
	printf("%d\n", sum);
	return 0;
}

算法笔记3.1-I题-锤子剪刀布

题目描述

//思路(来源于教材题解):
//甲方赢的局数等于乙方输的局数,又因为平局数对于双方来说都必定相等
//所以只需要一个数组统计输赢情况
//对于输入,可以直接预处理为数字,用数字大小顺序代替字母序
//由于输赢是循环相克的,用数字来处理也会简化代码
#include 
const char pose[3] = {'B', 'C', 'J'};
int win_a[3], win_b[3];	    //记录甲乙分别靠什么手势赢,序号已经对应成了字典序
int tot_a[3];		//由于甲乙输赢局数存在的关系,一个数组就行了,三个数分别对应胜、平、负
int max(int a[])
{
	int t = 0;
	for(int i = 0; i < 3; i++)
		{if(a[i] > a[t]) t = i;}
	return t;
}
int change(char ch)		//将手势转换成数字,并且字典序也与之对应
{
	if(ch == 'B') return 0;
	else if(ch == 'C') return 1;
	else if(ch == 'J') return 2;
}
int main(void)
{
	int N, numa, numb;
	char cha, chb;
	scanf("%d", &N);
	while(N--)
	{
		getchar();
		scanf("%c %c", &cha, &chb);
		numa = change(cha);
		numb = change(chb);
		if(numa == (numb + 2) % 3)	//甲赢
		{
			win_a[numa]++;
			tot_a[0]++;
		}
		else if(numb == (numa + 2) % 3)	//乙赢
		{
			win_b[numb]++;
			tot_a[2]++;
		}
		else {tot_a[1]++;}
	}
	printf("%d %d %d\n", tot_a[0], tot_a[1], tot_a[2]);
	printf("%d %d %d\n", tot_a[2], tot_a[1], tot_a[0]);
	printf("%c %c\n", pose[max(win_a)], pose[max(win_b)]);
	return 0;
}

个人博客:https://blog.summerfire.cn/
微信公众号:白发童子自来也

你可能感兴趣的:(《算法笔记》3.1-简单模拟-题解)