Programming thinking and practice CSP-M2 supplement questions (3/4 / data class)

Programming thinking and practice CSP-M2 supplement questions (3/4 / data class)

A-sequence of HRZ

problem analysis

The title is that the sequence is made up of three different numbers at most. When it is composed of three kinds of numbers, it is required that these three numbers are sorted to form an arithmetic sequence; when it is composed of two or one kind of numbers, it is directly established.

You can use STL to settraverse the sequence, and each number is added to the collection. If the collection is sizegreater than 3, it is not true, output "NO". In other cases, use an iterator to take out each number and put it into the array to sort. For three digits, "YES" must be output in the arithmetic sequence, otherwise "NO".

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ll a;
	int t,n;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		set<ll>s;
		while(n--){
			scanf("%lld",&a);
			s.insert(a);
		}
		int var=s.size();
		switch(var){
			case 0:{
				return -1;
				break;
			}
			case 1:{
				printf("YES\n");
				break;
			}
			case 2:{
				printf("YES\n");
				break;
			}
			case 3:{
				ll a[3];
				int i=0;
				set<ll>::iterator iter;
				for(iter=s.begin();iter!=s.end();++iter){
					a[i++]=*iter;
				}
				sort(a,a+i);
				if(a[1]-a[0]==a[2]-a[1]){
					printf("YES\n");
				}
				else{
					printf("NO\n");
				}
				break;
			}
			default:{
				printf("NO\n");
				break;
			}
		}
	}
	return 0;
} 

B-HRZ Learn English

problem analysis

Starting with the first letter, a sequence of length 26 is determined.

Each capital letter is required to appear 0 times or 1 time. In each paragraph, the sum of the capital letters and “?” Is 26.

When outputting, from the beginning to the end, output in order, to ensure the smallest lexicographic order.

#include<bits/stdc++.h>
using namespace std;
int main(){
	string str;
	cin>>str;
	int l=0,r=25;
	int len=str.length();
	while(r<len){
		int letter[27]={};
		int p=l;
		while(p<=r){
			if(str[p]=='?'){
				letter[26]++;
			}
			else{
				letter[str[p]-'A']++;
				if(letter[str[p]-'A']>1){
					break;
				}
			}
			p++;
		}
		p--;
		if(p==r){
			int index=0;
			int i=l;
			while(i<=r){
				if(str[i]!='?'){
					cout<<str[i];
				}
				else{
					while(letter[index]!=0){
						index++;
					}
					cout<<(char)('A'+index);
					index++;
				}
				i++;
			}
			return 0;
		}
		l++;
		r++;
	}
	cout<<"-1"<<endl;
	return 0;
}

C-The wonderful sequence of Gugudong

problem analysis

Misunderstanding: At the beginning, I thought that the general formula of the question inspection series directly wrote the following code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void Query(ll n)
{
	ll group=0;
	while(group*(group+1)/2<n){
		group++;
	}
	group--;
	n-=(group*(group+1)/2);
	printf("%lld\n",n);
}
int main()
{
	ll n;
	ll d;
	scanf("%lld",&n);
	while(n--){
		scanf("%lld",&d);
		Query(d);
	}
	return 0;
} 

This code is for the index of a certain number, but the "number" required by the question refers to the number, for example ... 11 12 13
These, according to the above code, are considered to be three numbers. Think of this as six numbers.

Positive solution: There are many ways of understanding. Many people understand it as a trapezoid made of numbers, which is more intuitive.

The key is to determine the number of each group.

For example, a group has {11} {12} {13}, which is considered to be three items, and there are two numbers in each item. Find the nth number of the entire sequence, it is easy to determine which "group" it belongs to, find the ith number of this group, and then determine which "item" it belongs to. Although the previous code is wrong, it still contributes to solving the problem.

#include <bits/stdc++.h>
#define LL long long
using namespace std;
 
void Work() {
    LL n;
    scanf( "%lld", &n );
    LL LastLen = 0, Len, Count;
    for( Len = 1; ; ++Len ) {
        Count = 9;
        for( LL i = 1; i < Len; ++i ) 
            Count = Count * 10;
        LL Sum = ( LastLen + Len + LastLen + Count * Len ) * Count / 2;
        if( n <= Sum ) break;
        n -= Sum;
        LastLen += Count * Len;
    }
    LL Left = 1, Right = Count, Mid, Ans;
    while( Left <= Right ) {
        Mid = ( Left + Right ) >> 1;
        LL Sum = ( LastLen + Len + LastLen + Mid * Len ) * Mid / 2;
        if( Sum >= n ) {
            Ans = Mid;
            Right = Mid - 1;
        } else Left = Mid + 1;
    }
    --Ans;
    n -= ( LastLen + Len + LastLen + Ans * Len ) * Ans / 2;
    ++Ans;
    for( Len = 1; ; ++Len ) {
        Count = 9;
        for( LL i = 1; i < Len; ++i ) 
            Count = Count * 10;
        LL Sum = Count * Len;
        if( Sum >= n ) break;
        n -= Sum;
    }
    LL Num = ( n + Len - 1 ) / Len;
    n = n - ( Num - 1 ) * Len;
    LL T = 1;
    for( LL i = 1; i < Len; ++i ) T = T * 10;
    Num = T + Num - 1;
    T = Len - n + 1;
    for( LL i = 1; i < T; ++i ) Num = Num / 10;
    printf( "%lld\n", Num % 10 );
    return;
}
 
int main() {
    LL Query;
    scanf( "%lld", &Query );
    for( LL i = 1; i <= Query; ++i ) Work();
    return 0;
}

Guess you like

Origin www.cnblogs.com/master-cn/p/12726214.html