在字符串中找出连续最长的数字串
cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 500;
char s[N];
bool check(char c) {
return c >= '0' && c <= '9';
}
int main() {
scanf("%s", s);
int l = -1, r = -1;
int n = strlen(s);
int left = 0, right = -1;
while (left < n) {
if (check(s[left])) {
int ll = left;
while (left < n && check(s[left]))++left;
right = left;
if (right - ll > r - l) {
l = ll, r = right;
}
}
++left;
}
for (int i = l; i < r; ++i)cout << s[i];
return 0;
}
岛屿数量
cpp
class Solution {
private:
int dx[4] = {1,-1,0,0},dy[4] = {0,0,1,-1};
bool vis[210][210]{};
int n,m;
void dfs(int i,int j,vector<vector<char> >&grid)
{
vis[i][j] = true;
for(int k = 0;k<4;++k)
{
int x = i+dx[k],y = j+dy[k];
if(x>=0&&x<n&&y>=0&&y<m&&grid[x][y]=='1'&&vis[x][y]==false)
{
dfs(x,y,grid);
}
}
}
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 判断岛屿数量
* @param grid char字符型vector<vector<>>
* @return int整型
*/
int solve(vector<vector<char> >& grid) {
// write code here
int cnt = 0;
n = grid.size(),m =grid[0].size();
for(int i = 0;i<n;++i)
{
for(int j = 0;j<m;++j)
{
if(grid[i][j] == '1' && vis[i][j]==false)
{
dfs(i,j,grid);
++cnt;
}
}
}
return cnt;
}
};
拼三角
cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int arr[N];
int main()
{
int t;
cin>>t;
while(t--)
{
for(int i = 0;i<6;++i)
cin>>arr[i];
sort(arr,arr+6);
if(arr[0] + arr[1] > arr[2] && arr[3] + arr[4] > arr[5] ||
arr[0] + arr[2] > arr[3] && arr[1] + arr[4] > arr[5] ||
arr[0] + arr[3] > arr[4] && arr[1] + arr[2] > arr[5] ||
arr[0] + arr[4] > arr[5] && arr[1] + arr[2] > arr[3])
cout << "Yes" << endl;
else cout<<"No"<<endl;
}
return 0;
}