洛谷刷题C语言:小球、硬币翻转、数学1(math1)- 加减算式、Roads Around The Farm S、Bovine Bones G

记录洛谷刷题QAQ


一、小球

题目描述

有 R 个红色盒子和 B 个蓝色盒子,还有 R 个红色小球和 B 个蓝色小球。每个盒子只能装一个小球,每个小球都要放在一个盒子里。

如果把一个红色小球放在一个红色盒子里,那么得分是 C。如果把一个蓝色小球放在一个蓝色盒子里,那么得分是 D。如果把一个红色小球放在一个蓝色盒子里,那么得分是 E。如果把一个蓝色小球放在一个红色盒子里,那么得分也是 E。

现在给出 R,B,C,D,E。应该如何放置这些小球进盒子,才能使得总得分最大?输出最大的总得分。

输入格式

一行,5 个整数,分别是 R,B,C,D,E。

输出格式

一个整数,最大总得分。

样例 #1

样例输入 #1

2 3 100 400 200

样例输出 #1

1400

样例 #2

样例输入 #2

2 3 100 400 300

样例输出 #2

1600

提示

【数据规模】

1 ≤ R ≤ 100,1 ≤ B ≤ 100, -1000 ≤ C,D,E ≤ 1000。

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>




int main()
{
	int R, B;//R是红色的个数、B是蓝色的个数 
	int C, D, E;//C是红进红,D是蓝进蓝,E是不同色
	scanf("%d%d%d%d%d",&R,&B,&C,&D,&E);
	
	
	int small = 0;
	if(R > B)		small = B;
	else if(B >= R) 	small = R;//找到R和B中比较小的 
	int sum = 0;
	double middle = (C+D)/2;
	if(E >= middle)
	{
		sum = small*E*2 + (B - small)*D + (R- small)*C; 
	}
	
	else if(E < middle)
	{
		 sum = R*C + B*D;
	}
	
	printf("%d\n",sum);
	return 0;
}

二、硬币翻转

题目描述

有很多个硬币摆在一行,有正面朝上的,也有背面朝上的。正面朝上的用1表示,背面朝上的用0表示。

现在要求从这行的第一个硬币开始,将从第一个硬币开始的前若干个硬币同时翻面,求如果要将所有硬币翻到正面朝上,最少要进行这样的操作多少次?

输入格式

一个字符串,由0和1组成,表示硬币状态

输出格式

一个整数,表示要翻转的最少次数

样例 #1

样例输入 #1

10

样例输出 #1

2

提示

【样例1说明】:

第1次翻转:把第一个硬币翻到反面,字符串为00

第2次翻转:把第一、二个硬币一起翻到正面,字符串为11,翻转完成,输出2

【数据范围】:

对于20%的数据,硬币个数 ≤ 10 \leq10 10

对于50%的数据,硬币个数 ≤ 1 0 4 \leq10^4 104

对于100%的数据,硬币个数 ≤ 1 0 6 \leq10^6 106

update(2020.10.28):更新了数据和数据范围

代码如下


#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>




int main()
{
	char num[1000000];
	scanf("%s",num);
		

	int len = strlen(num);//字符串的长度 
	
	int sum = len - 1;
	for(int i = 1;i < len;++i)
		if(num[i] == num[i-1])	sum--;	
		if(num[len - 1] == '0') 	sum++;
	printf("%d\n",sum);
	return 0;
}

三、数学1(math1)- 加减算式

题目背景

蒟蒻HansBug在数学考场上,挠了无数次的头,可脑子里还是一片空白。

题目描述

好不容易啊,HansBug终于熬到了做到数学最后一题的时刻了,眼前是一堆杂乱的加减算式。显然成功就在眼前了。可是他脑细胞和RP已经消耗殆尽,所以这个重要的任务就交给你们啦。

输入格式

一行,包含一个字符串形式的加减法多项式(每一项数字范围为0-32767)。

输出格式

一个整数,为计算所得的结果(保证计算结果不会超过长整型范围)。

样例 #1

样例输入 #1

1+2-3

样例输出 #1

0

提示

字符串长度不超过60000

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>




int main()
{
	int num[30001];
	char NUM[30001];
	int len;
	for(len = 1;;len++)
	{
		scanf("%d%c",&num[len],&NUM[len]);
		if(NUM[len] != '+'&&NUM[len]!='-')
			break;
	}
	
	long long sum = num[1];
	for(int i =2;i <= len;i++)
	{
		if(NUM[i-1] == '-')
			sum-=num[i];
		else
			sum+=num[i];
	}
	printf("%lld\n",sum);
	return 0;
}

四、[USACO08OPEN]Roads Around The Farm S

题目描述

Farmer John’s cows have taken an interest in exploring the territory around the farm. Initially, all N (1 <= N <= 1,000,000,000) cows commence traveling down a road in one big group. Upon encountering a fork in the road, the group sometimes chooses to break into two smaller (nonempty) groups with each group continuing down one of the roads. When one of those groups arrives at another fork, it might split again, and so on.

The cows have crafted a peculiar way of splitting: if they can split into two groups such that the sizes of the groups differ by exactly K (1 <= K <= 1000), then they will split in that way; otherwise, they stop exploring and just start grazing peacefully.

Assuming that there will always be new forks in the road, compute the final number of groups of peacefully grazing cows.

约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土地.她们将沿着一条路走,一直走到三岔路口(可以认为所有的路口都是这样的).这时候,这一群奶牛可能会分成两群,分别沿着接下来的两条路继续走.如果她们再次走到三岔路口,那么仍有可能继续分裂成两群继续走. 奶牛的分裂方式十分古怪:如果这一群奶牛可以精确地分成两部分,这两部分的牛数恰好相差K(1≤K≤1000),那么在三岔路口牛群就会分裂.否则,牛群不会分裂,她们都将在这里待下去,平静地吃草. 请计算,最终将会有多少群奶牛在平静地吃草.

输入格式

* Line 1: Two space-separated integers: N and K

输出格式

* Line 1: A single integer representing the number of groups of grazing cows

样例 #1

样例输入 #1

6 2

样例输出 #1

3

提示

There are 6 cows and the difference in group sizes is 2.

There are 3 final groups (with 2, 1, and 3 cows in them).

6
/
2 4
/
1 3

代码如下

#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
int K;
//递归函数
int D(int a)
{
	if(a-K>0&&(a-K)%2 == 0){
		return D((a+K)/2)+D((a-K)/2);
	}
	else return 1;	
} 


int main()
{
	int N;
	scanf("%d%d",&N,&K);
	
	printf("%d\n",D(N));
	return 0;
}

五、[USACO08OCT]Bovine Bones G

题面翻译

贝茜喜欢玩棋盘游戏和角色扮演游戏,所以她说服了约翰开车带她去小商店.在那里她买了三个骰子。这三个不同的骰子的面数分别为 s 1 , s 2 , s 3 s_1,s_2,s_3 s1,s2,s3

对于一个有 S S S 个面的骰子每个面上的数字是 1 , 2 , 3 , … , S 1,2,3,\ldots,S 1,2,3,,S。每个面(上的数字)出现的概率均等。贝茜希望找出在所有“三个面上的数字的和”中,哪个和的值出现的概率最大。

现在给出每个骰子的面数,需要求出哪个所有“三个面上的数字的和”出现得最频繁。如果有很多个和出现的概率相同,那么只需要输出最小的那个。

数据范围: 2 ≤ s 1 ≤ 20 2\le s_1\leq 20 2s120 2 ≤ s 2 ≤ 20 2 \leq s_2\leq 20 2s220 2 ≤ s 3 ≤ 40 2 \leq s_3\leq 40 2s340

题目描述

Bessie loves board games and role-playing games so she persuaded Farmer John to drive her to the hobby shop where she purchased three dice for rolling. These fair dice have S1, S2, and S3 sides

respectively (2 <= S1 <= 20; 2 <= S2 <= 20; 2 <= S3 <= 40).

Bessie rolls and rolls and rolls trying to figure out which three-dice sum appears most often.

Given the number of sides on each of the three dice, determine which three-dice sum appears most frequently. If more than one sum can appear most frequently, report the smallest such sum.

POINTS: 70

输入格式

* Line 1: Three space-separated integers: S1, S2, and S3

输出格式

* Line 1: The smallest integer sum that appears most frequently when the dice are rolled in every possible combination.

样例 #1

样例输入 #1

3 2 3

样例输出 #1

5

提示

Here are all the possible outcomes.

1 1 1 -> 3  
1 2 1 -> 4  
2 1 1 -> 4  
2 2 1 -> 5  
3 1 1 -> 5  
3 2 1 -> 6 
1 1 2 -> 4  
1 2 2 -> 5  
2 1 2 -> 5  
2 2 2 -> 6  
3 1 2 -> 6  
3 2 2 -> 7 
1 1 3 -> 5  
1 2 3 -> 6  
2 1 3 -> 6  
2 2 3 -> 7  
3 1 3 -> 7  
3 2 3 -> 8

Both 5 and 6 appear most frequently (five times each), so 5 is the answer.

代码如下

//一个很傻的算法
#include<string.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>

int main()
{	
	int s1, s2, s3;
	scanf("%d%d%d",&s1,&s2,&s3);
	
	int sum = 0;
	int m = s1 + s2 + s3;
	int num[m+1];
	for(int i = 3;i <= m;i++)
	{
		num[i] = 0;
	}
	for(int i = 1;i <= s1;i++)
	{
		for(int j = 1;j <= s2;j++)
		{
			for(int t = 1;t <= s3;t++)
			{
				
				sum = i + j + t;
				num[sum]++;

			}
		}
	}
	int min = 0;
	int ans = 0;
	for(int i = 3;i <= m;i++)
	{
		if(num[i] > min)
		{
			min = num[i];
			ans = i;
		}
	}
	
	printf("%d\n",ans);
	return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值