A. My First Sorting Problem
You are given two integers x x x and y y y.
Output two integers: the minimum of x x x and y y y, followed by the maximum of x x x and y y y.
分析:
即先输出两个数中的较小值,再输出较大值即可。
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tt;
cin >> tt;
while(tt--)
{
int a,b;
cin >> a >> b;
cout << min(a,b) << ' ' << max(a,b) << endl;
}
return 0;
}
B. Different String
You are given a string s s s consisting of lowercase English letters.
Rearrange the characters of s s s to form a new string r r r that is not equal to s s s, or report that it’s impossible.
分析:
首先判断字符串中字符个数,若仅有一个字符,则一定不能通过交换字符改变这个字符串。
否则对这个字符串进行循环,当出现不相同的两个字符时,交换这两个字符,即可得到与原字符不相等的新字符串,输出交换后的字符串,若字符串中所有字符全部相等,则一定不能通过交换得到新字符串,输出No。
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tt;
cin >> tt;
while(tt--)
{
char a[1010];
cin >> a;
int flag=0,n=strlen(a);
for(int i=0;i<n-1;i++)
{
if(a[i]!=a[i+1])
{
char temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
flag=1;
break;
}
}
if(flag==0)
{
cout << "NO" << endl;
}
else
cout << "YES" << endl << a << endl;
}
return 0;
}
C. Clock and Strings
There is a clock labeled with the numbers 1 1 1 through 12 12 12 in clockwise order, as shown below.
In this example,
(
a
,
b
,
c
,
d
)
=
(
2
,
9
,
10
,
6
)
(a,b,c,d)=(2,9,10,6)
(a,b,c,d)=(2,9,10,6), and the strings intersect.
Alice and Bob have four distinct integers a a a, b b b, c c c, d d d not more than 12 12 12. Alice ties a red string connecting a a a and b b b, and Bob ties a blue string connecting c c c and d d d. Do the strings intersect? (The strings are straight line segments.)
分析:
按照圆环以a,b为界划分为两部分,如果从,c,d在同一部分则两条连线不会相交,即如果c,d均大于a小于b,或同时满足小于a或大于b,那么两天连线不相交,否则一定相交。
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tt;
cin >> tt;
while(tt--)
{
int a,b,c,d;
cin >> a >> b >> c >> d;
if(a>b){
int temp=a;
a=b;
b=temp;
}
if(c>d)
{
int temp=c;
c=d;
d=temp;
}
if(c>a&&c<b&&d>a&&d<b)
{
cout << "NO" << endl;
}
else if((c<a||c>b)&&(d<a||d>b))
{
cout << "NO" << endl;
}
else
cout << "YES" << endl;
}
return 0;
}
D. Binary Cut
You are given a binary string † ^{\dagger} †. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.
Note that:
- each character must lie in exactly one of the pieces;
- the pieces must be contiguous substrings of the original string;
- you must use all the pieces in the rearrangement.
† ^{\dagger} † A binary string is a string consisting of characters 0 \texttt{0} 0 and 1 \texttt{1} 1. A sorted binary string is a binary string such that all characters 0 \texttt{0} 0 come before all characters 1 \texttt{1} 1.
分析:
我们的目的是把原字符串分解成若干个部分,再重新组合,将其组成01……1的形式。即我们需要做的是将出现在1后面的0分解。我们要找到所有的01,10部分,除了一个01部分可作为一个衔接处不用划分,其余部分均需进行分解。最终形成的字串数量即为分解数加1.
以11010为例
首先我们要找出所有的10,01子字符串,分别记为ans1,ans2,在本例中ans1=2,ans2=1,01部分可作为一个衔接处不用划分,若存在多个则ans2-1;最终结果即为ans1+ans2+1。
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int tt;
cin >> tt;
while(tt--)
{
char a[1100];
int ans1=0,ans2=0;
cin >> a;
for(int i=0;a[i]!='\0';i++)
{
if(a[i]=='1'&&a[i+1]=='0')
ans1++;
else if(a[i]=='0'&&a[i+1]=='1')
ans2++;
}
if(ans2!=0)
ans2--;
cout << ans1+ans2+1 << endl;
}
return 0;
}