练习课

· · 个人记录

1.便宜的车牌

这道题需要理清楚情况 1.全部买特殊票 2.卖完特殊后买普通

#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,sum; //n次乘车次数
int main()
{ 
    cin>>n>>m>>a>>b;
    double xjb=b*1.0/m;计算性价比
    if(a<=xjb)//第一种情况
    {
        cout<<a*n;
    }
    else //第二种情况:特殊票更便宜(else 部分)
    {
        int s1=ceil(n*1.0/m)*b;
        int s2=n/m*b+(n-n/m*m)*a;

        cout<<min(s1,s2);//最便宜的
    }
    return 0;
}

2.好数

要看清题目可得条件:

好数:一个数必须包含 0 - k 所有数字(缺一不可)

重点:如果输入中有其他数字,但是已经成为好数依然是好数,需要标记

#include<bits/stdc++.h>
using namespace std;
int n,k,t[15];
int f(int x)//// 判断x是不是好数,是返回1,不是返回0
{
    memset(t,0,sizeof(t));
    while(x)
    {
        t[x%10]++;//寻找
        x/=10;
    }
//// 检查 0—k 每一个数字是否都出现过
    for(int i=0;i<=k;i++)
    {
        if(t[i]==0)
        {
            return 0;
        }
    }
    return 1;
}

int main()
{
    cin>>n>>k;
    int cnt=0;
    while(n--)
    {
        int x;
        cin>>x; 
        if(f(x)==1)
        {
            cnt++;//// 如果是好数
        }
    }
    cout<<cnt; //输出

    return 0;
}


3.石头坠落

将程序分清楚
1.从下往上扫
2.只要石头下面有空位,就让它掉一格(按列处理因为石头只竖直下落)
3.重复直到石头不动

明白这三点就行
```cpp
#include<bits/stdc++.h>
using namespace std
char a[111][111]; 
int main()
{
    int t;
    cin>>t;
    while(t--)
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];//输入
        }
    }
        for(int l=1;l<=m;l++)//一列一列处理,让石头下落
        {
            while(l)
            {
                int f=0;//现在是没动过的
                for(int h=n;h>1;h--)//从上往下扫
                {
            if(a[h][l]=='.'&&a[h-1][l]=='*')
                    {
                        swap(a[h][l],a[h-1][l]);/// 交换,石头掉下来
                        f=1;//标记
                    }
                }
                if(f==0)
                {
                break;//没石头动了,这一列结束
                }
            }

        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cout<<a[i][j];
            }
            cout<<endl;//输出
        }
    }

    return 0;
}