ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

本周题解

2021-01-23 19:29:24  阅读:145  来源: 互联网

标签:case int 题解 idnum number c% 本周 test


@本周题解

A

 Do you own an ID card?You must have a identity card number in your family's Household Register. From the ID card you can get specific personal information of everyone.

The number has 18 bits,the first 17 bits contain special specially meanings:the first 6 bits represent the region you come from,then comes the next 8 bits which stand for your birthday.What do other 4 bits represent?You can Baidu or Google it.
Here is the codes which represent the region you are in.

However,in your card,maybe only 33 appears,0000 is replaced by other numbers.
Here is Samuel's ID number 331004198910120036 can you tell where he is from?The first 2 numbers tell that he is from Zhengjiang Province,number 19891012 is his birthday date (yy/mm/dd).

Input
Input will contain 2 parts:
A number n in the first line,n here means there is n test cases. For each of the test cases,there is a string of the ID card number.
Output
Based on the table output where he is from and when is his birthday. The format you can refer to the Sample Output.
Sample Input

1
330000198910120036

Sample Output

He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.

根据数字确认地址 。代码如下。

#include<stdio.h>
int main()
{

	char idnum[18];
	int n,i,j;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		for(i=0;i<18;i++)
		{
		scanf("%c",&idnum[i]);
	}
	getchar();
		if(idnum[0]=='3'&&idnum[1]=='3')
		printf("He/She is from Zhejiang,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		if(idnum[0]=='8'&&idnum[1]=='2')
		printf("He/She is from Macao,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		if(idnum[0]=='1'&&idnum[1]=='1')
		printf("He/She is from Beijing,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		if(idnum[0]=='5'&&idnum[1]=='4')
		printf("He/She is from Tibet,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		if(idnum[0]=='7'&&idnum[1]=='1')
		printf("He/She is from Taiwan,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		if(idnum[0]=='2'&&idnum[1]=='1')
		printf("He/She is from Liaoning,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		if(idnum[0]=='8'&&idnum[1]=='1')
		printf("He/She is from Hong Kong,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		if(idnum[0]=='3'&&idnum[1]=='1')
		printf("He/She is from Shanghai,and his/her birthday is on %c%c,%c%c,%c%c%c%c based on the table.\n",idnum[10],idnum[11],idnum[12],idnum[13],idnum[6],idnum[7],idnum[8],idnum[9]);
		
	}
	return 0;
}

B Assistance Required

 After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty dishes. The contestants would line up in a queue, one behind the other. Each contestant got a number starting with 2 for the first one, 3 for the second one, 4 for the third one, and so on, consecutively.
The first contestant in the queue was asked for his number (which was 2). He was freed from the washing up and could party on, but every second contestant behind him had to go to the kitchen (those with numbers 4, 6, 8, etc). Then the next contestant in the remaining queue had to tell his number. He answered 3 and was freed from assisting, but every third contestant behind him was to help (those with numbers 9, 15, 21, etc). The next in the remaining queue had number 5 and was free, but every fifth contestant behind him was selected (those with numbers 19, 35, 49, etc). The next had number 7 and was free, but every seventh behind him had to assist, and so on.

Let us call the number of a contestant who does not need to assist with washing up a lucky number. Continuing the selection scheme, the lucky numbers are the ordered sequence 2, 3, 5, 7, 11, 13, 17, etc. Find out the lucky numbers to be prepared for the next contest party.

Input
The input contains several test cases. Each test case consists of an integer n. You may assume that 1 <= n <= 3000. A zero follows the input for the last test case.
Output
For each test case specified by n output on a single line the n-th lucky number.
Sample Input

1
2
10
20
0

Sample Output

2
3
29
83

模拟,刚开始以为是素数集,结果不对。按方式标记就行。

#include <stdio.h>
#define N 35000
int a[N] = {0};
int s[N];
int main() {
    int k = 0, j = 0;
    int cnt = 0;
    int n;
    for (int i = 2; i < N; i++)
        if(!a[i]) {
            s[cnt++] = i;
            j = 0;
            k = i+1;
            while(k < N) {
                if (!a[k])
                    j++;
                if (j == i) {
                    a[k] = 1;
                    j = 0;
                }
                k++;
            } 
        } 
    while(scanf("%d", &n),n)
        printf("%d\n", s[n-1]);
    return 0;
}

E Train Problem I

 As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
Output
The output contains a string “No.” if you can’t exchange O2 to O1, or you should output a line contains “Yes.”, and then output your way in exchanging the order(you should output “in” for a train getting into the railway, and “out” for a train getting out of the railway). Print a line contains “FINISH” after each test case. More details in the Sample Output.
Sample Input

3 123 321
3 123 312

Sample Output

Yes.
in
in
in
out
out
out
FINISH
No.
FINISH


        
  
For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".

Hint

Hint
        
 小火车,用栈模拟,做好出入栈的记录就好。ac代码如下。
   #include<utility>
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
using namespace std;
char a[100000];
int main()
{
	int n,z=0;
	string o1,o2;
	while(cin>>n>>o1>>o2)
	{z=0;
	
		stack<char>c;
		int point=0,m=0;
		for(int i=0;i<n;i++)
		{
			c.push(o1[i]);
			a[z++]=1;
			while((c.empty()!=1)&&(o2[point]==c.top()))
			{
				a[z++]=0;
				c.pop();
				point++;
			}
		}
		if(c.empty()==0)
		cout<<"No."<<endl;
		else
		{
			cout<<"Yes."<<endl;
			for(int j=0;j<z;j++)
			{
				if(a[j]==1)
				cout<<"in"<<endl;
				if(a[j]==0)
				cout<<"out"<<endl;
			}
		}
		cout<<"FINISH"<<endl;
		
	}
	return 0;
	
}

O Killjoy

A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).

Killjoy’s account is already infected and has a rating equal to x
. Its rating is constant. There are n accounts except hers, numbered from 1 to n. The i-th account’s initial rating is ai

. Any infected account (initially the only infected account is Killjoy’s) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.

Contests are regularly held on Codeforces. In each contest, any of these n

accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.

Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.

It can be proven that all accounts can be infected in some finite number of contests.

Input

The first line contains a single integer t

(1≤t≤100) — the number of test cases. The next 2t

lines contain the descriptions of all test cases.

The first line of each test case contains two integers n
and x (2≤n≤103, −4000≤x≤4000

) — the number of accounts on Codeforces and the rating of Killjoy’s account.

The second line of each test case contains n
integers a1,a2,…,an (−4000≤ai≤4000)

 — the ratings of other accounts.

Output

For each test case output the minimal number of contests needed to infect all accounts.

Example
Input

3
2 69
68 70
6 4
4 4 4 4 4 4
9 38
-21 83 50 -59 -77 15 -71 -78 20

Output

1
0
2

Note

In the first test case it's possible to make all ratings equal to 69

. First account’s rating will increase by 1, and second account’s rating will decrease by 1

, so the sum of all changes will be equal to zero.

In the second test case all accounts will be instantly infected, because all ratings (including Killjoy’s account’s rating) are equal to 4
.

先把复杂问题简单化,其实只有三种情况,

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int t;
    cin >> t;
    while(t--){
        ll n,x;
        cin >> n >> x;
        ll a[n];
        ll sum=0,f1=0;
        for(ll i=0;i<n;++i){
            cin >> a[i];
            if(a[i]==x) f1++;
            sum+=a[i]-x;
        }
        if(f1==n) cout << 0 << endl;
        else if(f1>=1 || sum==0) cout << 1 << endl;
        else cout << 2 << endl;
    }
    return 0;
}

M Jumps

You are standing on the OX-axis at point 0 and you want to move to an integer point x>0

.

You can make several jumps. Suppose you’re currently at point y
(y may be negative) and jump for the k

-th time. You can:

either jump to the point y+k

or jump to the point y−1

.

What is the minimum number of jumps you need to reach the point x

?

Input

The first line contains a single integer t

(1≤t≤1000

) — the number of test cases.

The first and only line of each test case contains the single integer x
(1≤x≤106

) — the destination point.

Output

For each test case, print the single integer — the minimum number of jumps to reach x

. It can be proved that we can reach any integer point x

.

Example
Input

5
1
2
3
4
5

Output

1
3
2
3
4

Note

In the first test case x=1

, so you need only one jump: the 1-st jump from 0 to 0+1=1

.

In the second test case x=2

. You need at least three jumps:

the 1

-st jump from 0 to 0+1=1
;
the 2
-nd jump from 1 to 1+2=3
;
the 3
-rd jump from 3 to 3−1=2

;

Two jumps are not enough because these are the only possible variants:

the 1

-st jump as −1 and the 2-nd one as −1 — you’ll reach 0−1−1=−2
;
the 1
-st jump as −1 and the 2-nd one as +2 — you’ll reach 0−1+2=1
;
the 1
-st jump as +1 and the 2-nd one as −1 — you’ll reach 0+1−1=0
;
the 1
-st jump as +1 and the 2-nd one as +2 — you’ll reach 0+1+2=3

;

In the third test case, you need two jumps: the 1
-st one as +1 and the 2-nd one as +2, so 0+1+2=3

.

In the fourth test case, you need three jumps: the 1
-st one as −1, the 2-nd one as +2 and the 3-rd one as +3, so 0−1+2+3=4.

第一眼感觉是贪心,结果不是,每一个点都是由an=n的数列的sn和sn+1决定的,除了x+1的情况,其他都可以由缺少其中一步来达到。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int j=0,k=1;
		long long x,now=0;
		cin>>x;
		for(;now<x;k++)
		now=now+k;
		if(now==x)
		{
		cout<<k-1<<endl;
		continue;
		}
		if(now==x+1)
		{
			cout<<k<<endl;
			continue;
		}
		else
		{
			cout<<k-1<<endl;
			continue;
		}
	}
	return 0;
}

P Saving the City

Bertown is a city with n

buildings in a straight line.

The city’s security service discovered that some buildings were mined. A map was compiled, which is a string of length n
, where the i-th character is “1” if there is a mine under the building number i

and “0” otherwise.

Bertown’s best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered x
is activated, it explodes and activates two adjacent mines under the buildings numbered x−1 and x+1 (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes a

coins. He can repeat this operation as many times as you want.

Also, a sapper can place a mine under a building if it wasn’t there. For such an operation, he takes b

coins. He can also repeat this operation as many times as you want.

The sapper can carry out operations in any order.

You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.

Input

The first line contains one positive integer t

(1≤t≤105) — the number of test cases. Then t

test cases follow.

Each test case begins with a line containing two integers a
and b (1≤a,b≤1000

) — the cost of activating and placing one mine, respectively.

The next line contains a map of mines in the city — a string consisting of zeros and ones.

The sum of the string lengths for all test cases does not exceed 105

.

Output

For each test case, output one integer — the minimum number of coins that the sapper will have to pay.

Example
Input

2
1 1
01000010
5 1
01101110

Output

2
6

Note

In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, b=1

coin for placing a mine and a=5 coins for activating.

这题就是贪心了,每个10···1判断一次。




#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,a,b;
	cin>>t;
	while(t--)
	{
		long long sum=0,len;
		string s;
		cin>>a>>b>>s;
		len=s.length();
		int flag=0,num0=0;
		for(int i=0;i<len;i++)
        {
            if(flag==0)
            {
                if(s[i]=='1')
                {
                    sum+=a;
                    flag++;
                }
            }
            else
            {
                if(s[i]=='0')
                    num0++;
                else
                {
                    if(s[i-1]=='0')
                        sum+=min(a,b*num0);
                    num0=0;
                }
            }
	
	}
	cout<<sum<<endl;
}
}

训练赛A - Wizard of Orz

There are n digital panels placed in a straight line. Each panel can show any digit from 0 to 9. Initially, all panels show 0

.

Every second, the digit shown by each panel increases by 1
. In other words, at the end of every second, a panel that showed 9 would now show 0, a panel that showed 0 would now show 1, a panel that showed 1 would now show 2

, and so on.

When a panel is paused, the digit displayed on the panel does not change in the subsequent seconds.

You must pause exactly one of these panels, at any second you wish. Then, the panels adjacent to it get paused one second later, the panels adjacent to those get paused 2
seconds later, and so on. In other words, if you pause panel x, panel y (for all valid y) would be paused exactly |x−y|

seconds later.

For example, suppose there are 4
panels, and the 3-rd panel is paused when the digit 9

is on it.

The panel 1

pauses 2 seconds later, so it has the digit 1
;
the panel 2
pauses 1 second later, so it has the digit 0
;
the panel 4
pauses 1 second later, so it has the digit 0

.

The resulting 4
-digit number is 1090. Note that this example is not optimal for n=4

.

Once all panels have been paused, you write the digits displayed on them from left to right, to form an n
digit number (it can consist of leading zeros). What is the largest possible number you can get? Initially, all panels show 0

.

Input

The first line of the input contains a single integer t

(1≤t≤100) — the number of test cases. Each test case consists of a single line containing a single integer n (1≤n≤2⋅105

).

It is guaranteed that the sum of n
over all test cases does not exceed 2⋅105

.

Output

For each test case, print the largest number you can achieve, if you pause one panel optimally.

Example
Input

2
1
2

Output

9
98

Note

In the first test case, it is optimal to pause the first panel when the number 9

is displayed on it.

In the second test case, it is optimal to pause the second panel when the number 8
is displayed on it.
刚开始以为987是最大的,然后就一直不对,后来才反应过来,按第二个,是989.

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	long long n;
	cin>>n;
	int m=0;
	if(n==1)
	cout<<9<<endl;
	else if(n==2)
	cout<<98<<endl;
	else if(n==3)
	cout<<989<<endl;
	else if(n>3)
	{
	cout<<989;
	for(int i=1;i<=n-3;i++)
	{
		cout<<m++;
		if(m==10)
		m=0;
		}
		cout<<endl;	
	}}
	return 0;
}

B

 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题。
亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。

Input
本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的长度均小于100000。
Output
如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。
Sample Input

AABCD
CDAA
ASD
ASDF

Sample Output

yes
no
感觉是kmp算法运用,但是一直超时,可能我板子有问题?
最后用strstr过的,下面是超时代码。
#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[100000],b[100000];
int nex[100000];
void get()
{
	int i,j,len=strlen(b);
	nex[0]=-1;
	for(i=0,j=-1;i<len;)
	if(j==-1||b[i]==b[j])
	nex[++i]=++j;
	else
	j=nex[j];
}
int kmp()
{
	int i,j,n,len;
	get();
	n=strlen(a);
	len=strlen(b);
	for(i=j=0;i<n;)
	{
		if(j==-1||a[i]==b[j])
		i++,j++;
		else
		j=nex[j];		
		if(j>=len)
		return 1;
	}
	return 0;	
}
int main()
{
	while(scanf("%s%s",&a,&b)!=EOF){
	strcat(a,a);
	if(kmp())
	cout<<"yes"<<endl;
	else 
	cout<<"no"<<endl;
	}
}

C - String Equality CodeForces - 1451C

Ashish has two strings a and b, each of length n, and an integer k

. The strings only contain lowercase English letters.

He wants to convert string a
into string b by performing some (possibly zero) operations on a

.

In one move, he can either

choose an index i

(1≤i≤n−1) and swap ai and ai+1
, or
choose an index i
(1≤i≤n−k+1) and if ai,ai+1,…,ai+k−1 are all equal to some character c (c≠ ‘z’), replace each one with the next character (c+1)

, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on.

Note that he can perform any number of operations, and the operations can only be performed on string a

.

Help Ashish determine if it is possible to convert string a
into b

after performing some (possibly zero) operations on it.

Input

The first line contains a single integer t

(1≤t≤105

) — the number of test cases. The description of each test case is as follows.

The first line of each test case contains two integers n
(2≤n≤106) and k (1≤k≤n

).

The second line of each test case contains the string a
of length n

consisting of lowercase English letters.

The third line of each test case contains the string b
of length n

consisting of lowercase English letters.

It is guaranteed that the sum of values n
among all test cases does not exceed 106

.

Output

For each test case, print "Yes" if Ashish can convert a

into b

after some moves, else print "No".

You may print the letters of the answer in any case (upper or lower).

Example
Input

4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc

Output

No
Yes
No
Yes

Note

In the first test case it can be shown that it is impossible to convert a

into b

.

In the second test case,

“abba” −→inc
“acca” −→inc … −→inc

“azza”.

Here “swap” denotes an operation of the first type, and “inc” denotes an operation of the second type.

In the fourth test case,

“aaabba” −→−−swap
“aaabab” −→−−swap “aaaabb” −→inc … −→inc “ddaabb” −→inc … −→inc “ddddbb” −→inc … −→inc “ddddcc”.

记录每个字母有多少个,然后再判断是否可以,最后才写完,交上去超时了,感觉可能是因为时间到了。

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int a[26],b[26],c[26];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(b));
		long long n,k;
		cin>>n>>k;
		char s1[n],s2[n];
		cin>>s1>>s2;
		for(int i=0;i<n;i++)
		{
		a[s1[i]-'a']++;
		b[s2[i]-'a']++;
		}
		int flag=0;
		int sumc=0;
		for(int i=0;i<26;i++)
		{
		c[i]=a[i]-b[i];
		if(a[i]%k!=0)
		{
			flag=1;
			break;
		}
		if(i==25&&c[i]>0)
		{
		flag=1;
		break;
		}
		sumc=sumc+c[i]/k;
		if(sumc<0)
		{
			flag=1;
			break;
		}
	}
	if(sumc!=0||flag==1)
	cout<<"No"<<endl;
	else
	cout<<"Yes"<<endl;
	
}
return 0;
}

标签:case,int,题解,idnum,number,c%,本周,test
来源: https://blog.csdn.net/weixin_51671731/article/details/113060303

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有