Application of stack (stack legitimacy / Integer Decomposition into several items and / Full Permutation / prefix expression) data structure C language version

7-16 legality of stack operation (20 points)

Suppose s and x represent the stack in and stack out operations respectively. If an empty stack is operated according to a sequence consisting only of S and X, the corresponding operations are feasible (e.g. the stack is empty when there is no deletion) and the final state is also empty, the sequence is said to be a legal stack operation sequence. Please write a program and input the s and X sequences to judge whether the sequence is legal.

Input format:

The first input line gives two positive integers N and M, where N is the number of sequences to be tested and M (≤ 50) is the maximum capacity of the stack. Then N lines, each of which gives a sequence consisting only of S and X. The sequence is guaranteed not to be empty and its length does not exceed 100.

Output format:

For each sequence, output YES in one line if the sequence is a legal stack operation sequence, or NO if not.

Input example:

4 10
SSSXXSXXSX
SSSXXSXXS
SSSSSSSSSSXSSXXXXXXXXXXX
SSSXXSXXX

Output example:

YES
NO
NO
NO

AC Code:

#include<iostream>
#include<cstdio>
using namespace std;
int nn;
int cnt = 0;
int zz[35];
int k = 1;
void print(int len){
    for(int i = 0;i<len;i++){
        if(!i)
            printf("%d",zz[i]);
        else
            printf("+%d",zz[i]);
    }
}
void F(int n,int len){
	if(n<=0){
		if(cnt==4){
			printf("\n");
			cnt = 0;
		}
		cnt++;
		if(cnt==1){
            printf("%d=",nn);
            print(len);
		}
		else{
            printf(";%d=",nn);
            print(len);
		}
		return;
	}
	for(int i = 1;i<=n;i++)
        if(i>=k){
            zz[len] = i;
            k = i;
            F(n-i,len+1);//Call next
            k = i;//Fallback, update recursive work stack
        }
}
int main(){
	int n;
	scanf("%d",&n);
	nn = n;
	F(n,0);
	return 0;
}

Analysis report: digging

7-1 integer decomposition into the sum of several items (20 points)

There are many decomposition methods for decomposing a positive integer N into several positive integers, such as 7 = 6 + 1, 7 = 5 + 2, 7 = 5 + 1 + 1. Programming to find all integer decomposition formulas of positive integer N.

Input format:

Each input contains a test case, i.e. positive integer n (0 < n ≤ 30).

Output format:

Output all integer factorizations of N in ascending order. The increasing order means that for two decomposition sequences n1 = {n1, N2,...} and N2 = {m1, m2,...}, if there is i such that n1 = m1,..., ni = mi, but ni+1 < mi + 1, n1 sequence must be output before N2 sequence. Each formula is added from small to large, separated by semicolons, and line breaks after every 4 formulas are output.

Input example:

7

Output example:

7=1+1+1+1+1+1+1;7=1+1+1+1+1+2;7=1+1+1+1+3;7=1+1+1+2+2
7=1+1+1+4;7=1+1+2+3;7=1+1+5;7=1+2+2+2
7=1+2+4;7=1+3+3;7=1+6;7=2+2+3
7=2+5;7=3+4;7=7

AC Code:

#include<iostream>
#include<cstdio>
using namespace std;
int nn;
int cnt = 0;
int zz[35];
int k = 1;
void print(int len){
    for(int i = 0;i<len;i++){
        if(!i)
            printf("%d",zz[i]);
        else
            printf("+%d",zz[i]);
    }
}
void F(int n,int len){
	if(n<=0){
		if(cnt==4){
			printf("\n");
			cnt = 0;
		}
		cnt++;
		if(cnt==1){
            printf("%d=",nn);
            print(len);
		}
		else{
            printf(";%d=",nn);
            print(len);
		}
		return;
	}
	for(int i = 1;i<=n;i++)
        if(i>=k){
            zz[len] = i;
            k = i;
            F(n-i,len+1);//Call next
            k = i;//Fallback, update recursive work stack
        }
}
int main(){
	int n;
	scanf("%d",&n);
	nn = n;
	F(n,0);
	return 0;
}

Analysis report: digging

7-2 output full arrangement (20 points)

Please write the full arrangement of the first n positive integers of the program output (n < 10), and observe the running time of the program when n gradually increases through 9 test cases (i.e. n from 1 to 9).

Input format:

The input gives a positive integer n (< 10).

Output format:

Full arrangement of outputs 1 to n. Each arrangement occupies one line, and there is no space between the numbers. The output order of the arrangement is dictionary order, that is, the sequences a1, a2,..., an are arranged before the sequences b1, b2,..., bn. If k exists, a1 = b1,..., AK = Bk   also   ak+1​<bk+1​.

Input example:

3

No blank lines at the end

Output example:

123
132
213
231
312
321

AC Code:

#include<stdio.h>
int m;
int cnt=0;
int zz[35];

void print()
{
    int i;
    for(i=0;i<m;i++)
    {

            printf("%d",zz[i]);
    }
}
int check(int i,int l)
{
    int n;
    int w;
    for(w=0;w<l;w++)
    {
        if(zz[w]==i)
            return 0;

    }
    return 1;

}
int F(int n,int k,int l)
{
    int i;

    if(n==0)
    {
        print();
        printf("\n");
        return 0;
    }
    for(i=1;i<=m;i++)
    {
        if(check(i,l))
        {
            zz[l]=i;
            F(n-i,i,l+1);

        }

    }

}
int add(int n)
{
    int i;
    int sum=0;
    for(i=0;i<=n;i++)
    {
        sum+=i;
    }
    return sum;
}
int main()
{

    int n;
    int sum;
    scanf("%d",&n);
    sum=add(n);
    m=n;
    F(sum,0,0);
    return 0;
}

Analysis report: digging

7-3 find the value of prefix expression (25 points)

Arithmetic expressions have prefix representation, infix representation and suffix representation. Prefix expression refers to the binary operator before two operands. For example, the prefix expression of 2 + 3 * (7-4) + 8 / 4 is: + + 2 * 3 - 7 4 / 8 4. Ask the designer to evaluate the resulting value of the prefix expression.

Input format:

Enter a prefix expression with no more than 30 characters in one line, including only +, -, *, / and operands. Different objects (operands and operands) are separated by spaces.

Output format:

Output the operation result of prefix expression, keep 1 digit after the decimal point, or the ERROR message ERROR.

Input example:

+ + 2 * 3 - 7 4 / 8 4

Output example:

13.0

AC Code:

#include<stdio.h>
#Include < bits / STDC + +. H > / / negative number, zero cannot be divisor
typedef struct nStack
{
    double *top;
    double *bottom;
} nStack;
int main()
{
     nStack mf;
    mf.bottom=(double*)malloc(sizeof(double)*60);
    mf.top=mf.bottom;
    char s[30][30];
    char k[30];
    char c;
    double a,b;
    int flag=0;
    double sum=0;
    int i=0;
    int j;
   // double a,b;
    while(1)  //input
    {
        scanf("%s",k);
        c=getchar();
     //   printf("%s\n",k);
        strcpy(s[i],k);
        i++;
        if(c=='\n')
        {
          //  printf("*****\n");
               break;

        }

    }
    i--;
    for(j=i;j>=0;j--)
    {
      //  printf("++++%c\n",s[j][0]);
        switch(s[j][0])
        {                                                  //Monitoring point 4: positive numbers are expressed as + N and negative numbers are expressed as - n
            case '+':
                 if(s[j][1]>47&&s[j][1]<58)
                {
                    goto s;
                }
                if(mf.top-2>=mf.bottom)
                {
                     a=*(--mf.top);
                    b=*(--mf.top);
                    *(mf.top++)=a+b;
                }
                else
                {
                    flag=1;
                    goto sign;
                }


                break;
            case '-':
                if(s[j][1]>47&&s[j][1]<58)
                {
                    goto s;
                }
                if(mf.top-2>=mf.bottom)
                {
                     a=*(--mf.top);
                    b=*(--mf.top);
                    *(mf.top++)=a-b;
                }
                else
                {
                    flag=1;
                    goto sign;
                }
                break;
            case '*':
                  if(mf.top-2>=mf.bottom)
                {
                     a=*(--mf.top);
                    b=*(--mf.top);
                    *(mf.top++)=a*b;
                }
                else
                {
                    flag=1;
                    goto sign;
                }
                break;
            case '/':
                if(mf.top-2>=mf.bottom)
                {
                     a=*(--mf.top);
                    b=*(--mf.top);
                    if(b!=0)
                        *(mf.top++)=a/b;
                    else
                    {
                        flag=1;
                         goto sign;
                    }

                }
                else
                {
                    flag=1;
                    goto sign;
                }

                break;
            default:
    s:            *(mf.top++)=atof(s[j]);
               // printf("***%lf\n",*(mf.top-1));
                break;

        }
    //    printf("stack bottom: %lf\n",*(mf.top-1));
    }
    if(mf.top-1==mf.bottom)                                //It is not detected here, but there may be a number remaining after the operation
        printf("%.1lf",*(mf.bottom));
    else
    {

        flag=1;
    }
sign: if(flag==1)
{
    printf("ERROR\n");
}
}

Analysis report: digging

Tags: C data structure stack

Posted on Tue, 05 Oct 2021 14:54:20 -0400 by Skippy93