文章目录
- WY22 Fibonacci数列
- NC242 单词搜索
- BC140 杨辉三角
WY22 Fibonacci数列
WY22 Fibonacci数列
#include<iostream>
#include<cmath>
using namespace std;
int n;
int main()
{
cin>>n;
int a=0,b=1,c=1;
while(n>c)
{
a=b;
b=c;
c=a+b;
}
int ans=min(n-b,c-n);
cout<<ans<<endl;
return 0;
}
NC242 单词搜索
NC242 单词搜索
#include <utility>
#include <vector>
class Solution {
int m, n;
int dy[4] = {1, -1, 0, 0}, dx[4] = {0, 0, 1, -1};
bool vis[105][105] = {0};
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param board string字符串vector
* @param word string字符串
* @return bool布尔型
*/
bool exist(vector<string>& board, string word) {
m = board.size(), n = board[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == word[0]) {
if (dfs(board, i, j, word, 0)) return true;
}
}
}
return false;
}
bool dfs(vector<string>& board, int i, int j, string& word, int pos) {
if (pos == word.size() - 1) return true;
vis[i][j] = true;
for (int k = 0; k < 4; k++) {
int a = i + dx[k], b = j + dy[k];
if (a >= 0 && a < m && b >= 0 && b < n && !vis[a][b] &&
board[a][b] == word[pos + 1]) {
if (dfs(board, a, b, word, pos + 1)) return true;
}
}
vis[i][j] = false;
return false;
}
};
BC140 杨辉三角
BC140 杨辉三角
#include<iostream>
#include<cstdio>
using namespace std;
int n,dp[35][35];
int main()
{
cin>>n;
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
printf("%5d",dp[i][j]);
}
printf("\n");
}
return 0;
}