A. Strange Functions
直接輸出輸入字串的長度,
B. Jumps
題意:一個數可以變成兩種數,設數值為y,可以變成y - 1和y + k,k是目前為止進行過上面操作的次數,
只用寫幾個數就能感覺到是一道打表找規律的題目,這種題一定不能手推,一定得打表!!
首先由每次當前數字可以轉移到兩個狀態,想到bfs,用一個pair存當前的數和k,之后用map記錄到當前數所用的步數,打表之后輸出前100個數,就能看出規律,然后就可以根據輸入的n直接判斷到這個數的步數應該是多少,
代碼:
#include<bits/stdc++.h>
#define endl '\n'
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int N = 1e6 + 100;
typedef pair<int,int> PII;
queue<pair<int,int>> Q;
map<int, int> MP;
int vis[N];
void bfs() //bfs
{
Q.push({0, 1});
while(!Q.empty())
{
PII head = Q.front();
if(head.first > 100) break; //防止時間太久,
//實際上找規律并不用找
//很大的數字
Q.pop();
if(MP[head.first] == 0)
MP[head.first] = head.second - 1;
Q.push({head.first + head.second, head.second + 1});
Q.push({head.first - 1, head.second + 1});
}
}
int main()
{
// bfs();
// for(int i = 1; i <= 100; i ++) // 打表
// cout << "i = " << i << "a[i] = " << MP[i] << endl;
int t;
cin >> t;
while(t --)
{
int n;
int ans = 1, kns = 3, pre = 1;
cin >> n;
while(n > ans) //首先找到n所在的區間
{
pre = ans;
ans += kns;
kns ++;
}
if(n == 1) cout << 1 << endl;
else if(n - pre == 2) cout << kns - 2 << endl;
else cout << kns - 1 << endl;
}
return 0;
}
C. Ping-pong
因為題目要求首先保證win的和最大化,其次要求讓對方盡可能少贏,
設先手有x點精力,后手有y點精力,那么后手可以讓先手先贏x - 1個球,直到先手發球后無體力了,就開始反擊,那么后手可以贏y個球,所以比分就是x -1:y,
這里沒讓先手贏最后一個球就是保證了讓對方盡可能贏的少,
所以不管x和y的大小情況是什么,都可以先讓先手贏x -1個球,然后后手再贏y個球,所以最終得分是x - 1:y,
代碼:
#include<bits/stdc++.h>
#define endl '\n'
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int N = 1e6 + 100;
int main()
{
int t;
cin >> t;
while(t --)
{
int a, b;
cin >> a >> b;
cout << a - 1 << " " << b << endl;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/229307.html
標籤:其他
上一篇:Unity 導航系統(初級)
下一篇:2020-12-02
