Touge Experiment Greedy Algorithm

Level 1: Find change


mission details

The task of this level: Design a greedy algorithm to find the least number of coins.

A shop assistant finds n yuan for a customer, using the following seven denominations of banknotes: 100 yuan, 50 yuan, 20 yuan, 10 yuan, 5 yuan, 2 yuan, 1 yuan.

Thinking: If a shop assistant gives 140 yuan to a customer, suppose there are nine denominations of coins: 100 yuan, 70 yuan, 50 yuan, 20 yuan, 10 yuan, 7 yuan, 5 yuan, 2 yuan, and 1 yuan. Is the greedy algorithm the optimal solution to the problem?

programming requirements

Please supplement the code in the editor on the right Begin-Endto complete the task of this level. Note that students need to get the money n they found.

void main()
{
    /**********  Begin  **********/
int j,GZ,A,B[8]={0,100,50,20,10,5,2,1},S[8]={0,0,0,0,0,0,0,0};
    scanf("%d",&GZ);
    for(j=1;j<=7;j++)
    {
        A=GZ/B[j];
        S[j] = A;
        GZ = GZ - A*B[j];
        printf("%d元 %d张\n",B[j],A);
    }




    /**********  End  **********/
}

Level 2: Find the range of a sequence

mission details

The task of this level: make a sequence of n positive integers, and perform the following operations: delete two numbers a and b in it each time, and then add a number to the sequence, and so on until there is only one number left in the sequence a×b+1.

Among all the numbers finally obtained in this way, the largest is recorded as max, and the smallest is recorded as min, then the range of the sequence is defined as, please use the greedy algorithm to design and program to output their range M=max-min.

programming requirements

Begin-EndPlease supplement the code in the editor on the right to complete the task of this level (note that the input data has been obtained for you).

#include <stdio.h>

/*********  Begin  **********/
int s1,s2;
void max2(int a[],int n)
{
    int  j;
    if(a[1]>=a[2])
    {
        s1=1;
        s2=2;
    }
    else
    {
        s1=2;
        s2=1;
    }
    for (j=3;j<=n;j++)
    {
        if(a[j]>a[s1])
        {
            s2=s1;
            s1=j;
        }
        else if(a[j]>a[s2])
            s2=j;
    }
}
int calculatemin(int a[],int n)
{
    while (n>2)
    {
        max2(a,n);
        a[s1]= a[s1]* a[s2]+1;
        a[s2]=a[n];
        n=n-1;
    }
    return(a[1]* a[2]+1);
}
void min2(int a[ ],int n)
{
    int  j;
    if(a[1]<=a[2])
    {
        s1=1;
        s2=2;
    }
    else
    {
        s1=2;
        s2=1;
    }
    for (j=3;j<=n;j++)
    {
        if (a[j]<a[s1])
        {
            s2=s1;
            s1=j;
        }
        else  if (a[j]<a[s2])
          s2=j;
    }
}
int calculatemax(int a[],int n)
{
    while (n>2)
    {
        min2(a,n);
        a[s1]= a[s1]* a[s2]+1;
        a[s2]=a[n];
        n=n-1;
    }
     return(a[1]* a[2]+1);
}
int length(int a[])
{
    int i=1;
    while(a[i]!=0)
    {
        i++;
    }
    return i-1;
}
int main()
{
    int i,n,b[100],max,min,num;
    scanf("%d",&num);
    int a[num+1];
    for (i=1;i<=num;i++)
        scanf("%d",&a[i]);
    for (i=1;i<=num;i++)
       b[i]=a[i];
    min= calculatemin(a,num);
    max= calculatemax(b,num);
    printf("Max=max-min=%d-%d=%d\n",max,min,max-min);

}
/*********  End **********/

Level 3: Express the true fraction as the sum of Egyptian fractions


mission details

The task of this level: Design an algorithm to express a true fraction F as the sum of Egyptian fractions.

programming requirements

Please supplement the code in the editor on the right Begin-Endto complete the task of this level. Note that students need to obtain real scores before programming.

#include "stdio.h"

void main()
{
    /**********  Begin  **********/
int a,b,c;
    scanf("%d %d",&a,&b);
    if(a>=b)
        printf("输入错误");
    else
        if(a==1 || b%a==0)
        {
            printf("%d/%d=1/%d",a,b,b/a);
        }
        else
        {
            printf("%d/%d=",a,b);
            while(a!=1)
            {
                c = b/a+1;
                a = a*c - b;
                b = b*c;
                printf("1/%d",c);
                if(a>=1)
                    printf("+");
                if(b%a ==0 || a==1)
                {
                    printf("1/%d",b/a);
                    a=1;
                }
            }
        }
     printf("\n");


    /**********  End  **********/
}

Level 4: Find the number with the most occurrences

mission details

The task of this level: Given n positive integers, write an experimental program to find the number that occurs most frequently among them. If there are multiple such numbers, output the smallest one.

programming requirements

Please supplement the code in the editor on the right Begin-Endto complete the task of this level.

#include <stdio.h>
using namespace std;
#include<algorithm>

/**********  Begin  **********/

int find(int n,int * a)
{
    int maxn=0,bestd,num=1,i=1;
    sort(a,a+n);
    int pred=a[0];
    while(i<n)
    {
        while(i<n&&a[i]==pred)
        {
            num++;
            i++;
        }
        if(num>maxn)
        {
            bestd=pred;
            maxn=num;
        }
        pred=a[i];
        num=1;
        i++;
    }
    return bestd;
}
int main()
{
    int n,bestd,i;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    bestd=find(n,a);
    printf("出现次数最多的且最小的数为%d\n",bestd);
    return 0;
}

/**********  End  **********/

Level 5: Remove any number of digits from the given integer and recompose the smallest integer

mission details

The task of this level: input a high-precision positive integer n with the keyboard, remove any s numbers in it, and the remaining numbers will form a new positive integer in the original left and right order.

Programming For given n and s, find a solution that minimizes the new number formed by the remaining numbers.

programming requirements

Please supplement the code in the editor on the right Begin-Endto complete the task of this level.

#include <bits/stdc++.h>
using namespace std;

int main() {
    /*********  Begin  ********/
int k;
    string s;
    cin >> s >> k;
    if (k > s.size()) {
        cout << "Invalid Input.";
    }
    while (k) {
        int i;
        for (i = 0; i < s.size() - 1 && s[i] <= s[i + 1]; i++);
        s.erase(i, 1);
        k--;
    }
    if (s.empty()) {
        cout << 0 << endl;
    }
    int i = 0;
    for (i = 0; i < s.size()-1;) {
        if (s[i] == '0') i++;
        else break;
    }
    cout << s.substr(i);
    return 0;



    return 0;
    /*********  End  ********/
}

Guess you like

Origin blog.csdn.net/m0_56267074/article/details/130761791