HDU_6154 CaoHaha's staff

找規律。
先找到小於等於n的最大的菱形的面積,然後按規律慢慢擴展。沒增加一條斜邊,面積增大1.5
代碼:

#include <iostream>
#include <string>
#include <string.h>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1e5;
double s[maxn];
int main()
{
    double x1 = 1.5, x2 = 4;
    s[4] = 2;
    s[5] = 2.5;
    s[6] = 4;
    for(int i = 7; i <= maxn ; ++ i)
    {
        if(i % 4 == 1) s[i] = s[i - 1] + x1, x1 += 1.0;
        if(i % 4 == 2) s[i] = s[i - 2] + x2, x2 += 2.0;
        if(i % 4 == 3) s[i] = s[i - 1] + x1;
        if(i % 4 == 0) s[i] = s[i - 2] + x2;
    }
    int t; cin >> t;
    while(t --)
    {
        double n; cin >> n;
        for(int i = 4; i <= maxn; ++ i)
        {
            if(s[i] >= n)
            {
                cout << i << endl; break;
            }
        }
    }
    return 0;
}


發佈了41 篇原創文章 · 獲贊 12 · 訪問量 8576
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章