练习打卡(C++)

本文介绍了编程题目,涉及多米诺骨牌在给定棋盘上的最优布局问题,矩阵的行列交换使其美观,以及不区分大小写的字符串比较,展示了基础算法和技巧的应用。
摘要由CSDN通过智能技术生成

目录

简介

A.Domino piling

A.Beautiful Matrix

A.Petya and Strings

A.Helpful Maths


简介

寒冬奖励计划现在开始哈哈,正式打卡的第一周~(^0^)~,逐渐开始四处吃席哈哈

A.Domino piling

嘿咻!

You are given a rectangular board of M × N squares. Also you are given an unlimited number of standard domino pieces of 2 × 1 squares. You are allowed to rotate the pieces. You are asked to place as many dominoes as possible on the board so as to meet the following conditions:

1. Each domino completely covers two squares.

2. No two dominoes overlap.

3. Each domino lies entirely inside the board. It is allowed to touch the edges of the board.

Find the maximum number of dominoes, which can be placed under these restrictions.

给你一个由 M × N 个方格组成的长方形棋盘。同时,您还会得到数量不限的 2 × 1 格标准多米诺骨牌。您可以旋转骨牌。你需要在棋盘上放置尽可能多的骨牌,以满足以下条件:

1.每张骨牌完全覆盖两个方格。

2.没有两张骨牌重叠。

3.每张骨牌完全位于棋盘内。允许接触棋盘边缘。

请找出在这些限制条件下可以放置的骨牌的最大数量。

Input

In a single line you are given two integers M and N — board sizes in squares (1 ≤ M ≤ N ≤ 16).

在一行中给出两个整数 M 和 N --棋盘的方格大小( 1 ≤ M ≤ N ≤ 16 )。

Output

Output one number — the maximal number of dominoes, which can be placed.

一个数字 - 可以摆放的骨牌的最大数量。

Examples

input

2 4

output

4

input

3 3

output

4
#include<iostream>
using namespace std;

int n,m;
int main()
{
	cin>>n>>m;
	if(n%2==0||m%2==0)//如果n,m有一个数为偶数
		cout<<n*m/2;
	else cout<<(n*m-1)/2;//n,m都为奇数
	return 0;//养成好习惯从return开始
}

画了几张图,就发现只要n和m其中有一个是偶数的话就直接套公式n*m/2,如果n和m都是奇数的话套公式(n*m-1)/2就行咯^0^。

还有还有,记得return!这是一个好习惯(绝对不会承认我之前忘记打了嘿嘿)

A.Beautiful Matrix

~('-')~!

You've got a 5 × 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix:

  1. Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≤ i < 5).
  2. Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≤ j < 5).

You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.

你有一个 5 × 5 矩阵,由 24 个 0 和一个数字 1 组成。我们用从上到下从 1 到 5 的数字来索引矩阵的行,用从左到右从 1 到 5 的数字来索引矩阵的列。在一次移动中,您可以对矩阵进行以下两种变换之一:

  1. 将矩阵中相邻的两行,即索引为 i 和 i + 1 的行交换为某个整数 i 和 (1 ≤ i < 5) 。 (1 ≤ i < 5) .
  2. 对调两个相邻的矩阵列,即索引为 j 和 j + 1 的列对调某个整数 j (1 ≤ j < 5) 。 (1 ≤ j < 5) .

如果矩阵的单数 1 位于矩阵的中间(位于第三行和第三列的交叉点上的单元格中),那么您认为矩阵看起来漂亮。请计算使矩阵漂亮所需的最少移动次数。

Input

The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.

输入由五行组成,每行包含五个整数:输入 i 行中的 j 个整数代表矩阵中位于 i 行和 j 列交点上的元素。保证矩阵由 24 个零和一个数字 1 组成。

Output

Print a single integer — the minimum number of moves needed to make the matrix beautiful.

打印一个整数 - 使矩阵漂亮所需的最少移动次数。

Examples

input

0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

output

3

input

0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0

output

1
#include<iostream>
#include<cmath>
using namespace std;

int n,m,a[5][5];
int main()
{
	for(int i=0;i<5;i++){
		for(int j=0;j<5;j++){
			cin>>a[i][j];
			if(a[i][j]==1)
				n=i,m=j;
		}		
	}
	cout<<abs(2-n)+abs(2-m);//算出起始坐标和终点坐标的横纵坐标差之和,输出
    return 0;
}

这道题可以先把矩阵看成一个坐标轴上的点

可以看出咱们只需要找到数字为1的点和中点的横纵坐标差的绝对值之和就行咯,有兴趣的可以了解一下曼哈顿距离求值(简单来说就是只能往上下左右四个方向^-^)。

对了对了,要求绝对值的话咱们就用得上abs()和它的头文件cmath。

A.Petya and Strings

要是真的有个哆啦a梦就好了^0^

Little Petya loves presents. His mum bought him two strings of the same size for his birthday. The strings consist of uppercase and lowercase Latin letters. Now Petya wants to compare those two strings lexicographically. The letters' case does not matter, that is an uppercase letter is considered equivalent to the corresponding lowercase letter. Help Petya perform the comparison.

小佩佳喜欢礼物。他妈妈给他买了两串同样大小的手串作为生日礼物。字符串由大写和小写拉丁字母组成。现在,Petya 想要对这两个字符串进行自动比较。字母的大小写并不重要,即大写字母等同于相应的小写字母。帮助 Petya 进行比较。

Input

Each of the first two lines contains a bought string. The strings' lengths range from 1 to 100 inclusive. It is guaranteed that the strings are of the same length and also consist of uppercase and lowercase Latin letters.

前两行各包含一个买入字符串。字符串的长度从 1 到 100 不等。保证字符串长度相同,并且由大写和小写拉丁字母组成。

Output

If the first string is less than the second one, print "-1". If the second string is less than the first one, print "1". If the strings are equal, print "0". Note that the letters' case is not taken into consideration when the strings are compared.

如果第一个字符串小于第二个字符串,则打印"-1"。如果第二个字符串小于第一个字符串,则打印 "1"。如果字符串相等,则打印 "0"。请注意,比较字符串时不考虑字母的大小写。

Examples

input

aaaa
aaaA

output

0

input

abs
Abz

output

-1

input

abcdefg
AbCdEfF

output

1

Note

If you want more formal information about the lexicographical order (also known as the "dictionary order" or "alphabetical order"), you can visit the following site:

如果您想了解有关词典顺序(也称为"字典顺序"或"字母顺序")的更多正式信息,可以访问以下网站:

#include<iostream>
#include<string>
using namespace std;

string s1,s2;
int main()
{
	cin>>s1>>s2;
	for(int i=0;i<s1.length();i++){//统一大小写,全部转换为小写
		if(s1[i]>='A'&&s1[i]<='Z')
			s1[i]+=32;
	}
	for(int i=0;i<s2.length();i++){
		if(s2[i]>='A'&&s2[i]<='Z')
			s2[i]+=32;
	}
	if(s1>s2)//比较大小
		cout<<"1";
	else if(s1==s2)
		cout<<"0";
	else cout<<"-1";
	return 0;
}

这道题咱们可以先依次输入两个字符串,然后统一所有字符的大小写(方便做比较^-^),最后按顺序一一对比大小就好咯。

注意在ASCII码表中,大小写之间差了32(大写字母A~Z的ASCII码值为65~90,小写字母a~z的ASCII码值从97~122,当然咱们只需要记住a和A就行)。

当然咯,如果知道towlower()以及strcmp()这俩函数就事情就更加简单了(其实也就是看着代码行数少了哈哈,咱们平时还可以注意一下每一次调用函数也是要耗费时间的,在估算运行时间时可不能忘记把函数的时间算上)

towlower():若参数为大写字母则将该对应的小写字母返回,使用头文件cwctype。

strcmp():根据ASCII码表,依次比较 s1 和 s2 的每个字符直到出现不到的字符,或者到达字符串末尾\0(如果s1<s2返回-1,s1=s2返回0,s1>s2返回1,具体有涉及到指针呢,有兴趣可以深入了解一下'0')。

使用俩函数后:

#include<iostream>
#include<cstring>//注意这个头文件
#include<cwctype>
using namespace std;

int ans;
string s1,s2;
int main()
{
	cin>>s1>>s2;
	for(int i=0;i<s1.length();i++)
		s1[i]=towlower(s1[i]);//字符串大小写转换,要使用头文件cwctype
	for(int i=0;i<s2.length();i++)
		s2[i]=towlower(s2[i]);
	ans=strcmp(s1.c_str(),s2.c_str());//比较字符串大小,c++要使用头文件cstring,一开始使用的string就过不了
	if(ans!=0){//有一个大佬直接使用ans==0?0:(ans>0?1:-1)输出,太牛了
		if(ans>0)
			cout<<"1";
		else cout<<"-1";
	}
	else cout<<"0";
	return 0;
}

使用ans==0?0:(ans>0?1:-1)直接输出可太妙了,这练题可谓是道阻且长哇\('0')/!!!

A.Helpful Maths

出来吧任意门!

Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.

The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.

You've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.

初学数学家秋莎是一名小学三年级学生。她现在正在学习加法运算。

老师写下了多个数字的和。学生应该计算出这个和。为了便于计算,和只包含数字 1、2 和 3。但这对秋莎来说还不够。她才刚刚开始算数,所以只有当和的顺序不递减时,她才能计算出和。例如,她不能计算和 1+3+2+1,但她可以计算和 1+1+2 和 3+3。

你已经得到了写在黑板上的和。重新排列这些和,并打印出 Xenia 可以计算的和。

Input

The first line contains a non-empty string s — the sum Xenia needs to count. String s contains no spaces. It only contains digits and characters "+". Besides, string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long.

第一行包含一个非空字符串 s - Xenia 需要计算的总和。字符串 s 不包含空格。它只包含数字和字符"+"。此外,字符串 s 是数字 1、2 和 3 的正确和。字符串 s 长度最多为 100 个字符。

Output

Print the new sum that Xenia can count.

打印 Xenia 可以计算的新总和。

Examples

input

3+2+1

output

1+2+3

input

1+1+3+1+3

output

1+1+1+3+3

input

2

output

2
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int a[105],y;
string s;
int main()
{
	cin>>s;
	for(int i=0;i<s.length();i++){
		if(s[i]!='+'){
			y++;
			a[y]=s[i]-'0';//将字符转换为整数并且储存
		}
	}
	sort(a,a+y+1);
	for(int i=1;i<y;i++)
		cout<<a[i]<<"+";
	cout<<a[y];
	return 0;
}

曾有李华写信,现有秋莎做数学,始终是逃不了帮他们写的命运嘞。

思路还是很简单!咱们直接先不看"+",找出所有的数字存起来,然后使用sort()进行排序后输出就行。

sort():类似于快速排序的方法,时间复杂度为n*log2(n),执行效率较高,需要使用到头文件algorithm(经典的排序方法,极度建议深入了解一下^0^)/*)。

/*注意:字符变量实际上是以ASCII码的形式存储的。而在ASCII码表中,数字字符’0’对应的ASCII码值是48。所以,通过将一个数字字符减去字符’0’的ASCII码值,我们可以将该字符转换为对应的整数值。*/

芜湖,这次就先记录这几个题咯,坚持坚持坚持,加油加油加油!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值