今日练习

今日练习

Gym 100722C 二分答案 未测试;

UVA11520 AC;

UVA 1267 待做;

Gym 100722C

//
// Created by 21122 on 2022/5/27/0027.
//
#include<iostream>
#include<algorithm>
using namespace std;
const double PI = acos(-1.0);
double arr[10005];
int N, F;
bool is_okay(double area)
{
    int total_pieces = 0;
    for(int i = 0; i < N; i++)
    {
        total_pieces += floor(arr[i] / area);
    }
    if(total_pieces >= F + 1)
    {
        return true;
    }

    return false;

}

int main()
{
    //int N, F;
    cin >> N >> F;

    double max_area = 0;
    for(int i = 0; i < N; i++)
    {
        int r ;
        cin >> r;
        arr[i] = PI * r * r;
        max_area = std::max(max_area, arr[i]);
        //cin >> arr[i];
    }

    //通过二分答案法来找出最合适的面积X
    double left = 0;
    double right = max_area;
    while(right > left)
    {
        double mid = (left + right) / 2;
        //
        if(is_okay(mid))
        {
            left = mid;

        }
        else
        {
            right = mid;
        }
    }

    return 0;
}

UVA11520

//
// Created by 21122 on 2022/5/27/0027.
//
#include<iostream>
#include<algorithm>

using namespace std;

int n;
char arr[15][15];
char dict[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G','H',
                 'I', 'J', 'K','L', 'M', 'N', 'O', 'P',
                 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                  'Y', 'Z'};
int main()
{
    int T;
    cin >> T;
    int Case = 1;
    for(int i = 1; i <= T; i++)
    {
        cin >> n;
        //从1开始,避免越界时进行特判(空间换时间和方便
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cin >> arr[i][j];
            }
        }

        //暴力枚举法 会有更好的方法么

        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {   //注意避免  - ’A' = -65 越界
                int temp[26] = {0};
                if(arr[i][j] == '.')
                {   //无数据,待修改
                    //上
                    if(arr[i - 1][j] - 'A' >= 0)
                        temp[arr[i - 1][j] - 'A'] = 1;

                    //下
                    if(arr[i + 1][j] - 'A' >= 0)
                        temp[arr[i + 1][j] - 'A'] = 1;

                    //左
                    if(arr[i][j - 1] - 'A' >= 0)
                        temp[arr[i][j - 1] - 'A'] = 1;

                    //右
                    if(arr[i][j + 1] - 'A' >= 0)
                        temp[arr[i][j + 1] - 'A'] = 1;


                    for(int k = 0; k < 26; k ++)
                    {
                        if(temp[k] == 0)
                        {   //贪心,取最优解
                            arr[i][j] = dict[k];
                            break;
                        }
                    }

                }
            }
        }

        cout << "Case " << Case ++ << ":" << endl;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cout << arr[i][j];
            }
            cout << endl;
        }

    }


    return 0;
}