codeforces 977
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
int n,k;
while (scanf("%d%d",&n,&k)!=EOF)
{
for (int i=0;i<k;i++){
if (n%10 == 0)
{
n = n/10;
}
else
n -- ;
}
printf("%d",n);
}
return 0 ;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
int n;
char a[200];
int mm;
while (scanf("%d%s",&n,a)!=EOF)
{
int cnt = 0;
mm = 0;
for (int i=0;i<n-1;i++)
{
int num = 0;
for (int j = i+1;j<n-1;j++)
{
if (a[i] == a[j] && a[i+1] == a[j+1])
num++;
}
if (num > cnt)
{
cnt = num ;
mm = i;
}
}
printf("%c%c\n",a[mm],a[mm+1]);
}
return 0 ;
}
双重循环匹配,对每一个两个单词的组合,计算与它相同的组合个数。如果是同一个组合出现的先后顺序不同,则只统计它后面出现的个数,所以最先出现的组合的个数最大。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
int n,k;
long long a[200001];
while (cin>>n>>k)
{
for (int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
if (!k)
{
// k==0时
long t = a[0]-1>=1?a[0]-1:-1;
cout << t << endl;
}
else if (a[k]==a[k-1])
cout << -1 << endl;
else
cout << a[k-1] << endl;
}
return 0 ;
}
英文阅读理解题:x的取值范围为[1…], 当K = 0时,可以取最小值-1, 但若此时的x不在取值范围内,则输出-1.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
int n;
long long a[101];
bool visited[101];
long long f[101];
void dfs(int &k)
{
if (k==n)
{
for (int i=0;i<n;i++)
cout << f[i] <<' ';
cout << endl;
}
long long x = f[k-1];
for (int i=0;i<n;i++)
{
if (visited[i])
continue;
if (a[i] == x/3 || a[i] == x*2)
{
f[k++] = a[i];
visited[i] = true;
dfs(k);
visited[i] = false;
k--;
}
}
}
int main()
{
int k;
while (cin>>n)
{
for (int i=0;i<n;i++)
{
cin >> a[i];visited[i] = false;
}
k = 0;
for(int i=0;i<n;i++)
{
visited[i] = true;
f[k++] = a[i];
dfs(k);//确定了k个数
visited[i] = false;
k--;
}
}
return 0 ;
}
wrong answer ?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int maxn = 200001;
bool visited[maxn];
int num = 0;
vector<int> v[maxn]; // v[i]�����i����㣬v[i]Ϊһ���ڽ�i��������
bool flag;
void dfs(int v0)
{
visited[v0] = true;
if (v[v0].size()!=2)
{
flag = true;
}
for (int i=0;i<v[v0].size();i++)
{
int t = v[v0][i];
if (!visited[t])
dfs(t);
}
}
int main()
{
int n,m;
int a,b;
while (cin>>n>>m)
{
num = 0;
for (int i=0;i<m;i++)
{
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for (int i=1;i<=n;i++)
visited[i] = false;
for (int i=1;i<=n;i++)
{
flag = false;
if(!visited[i])
{
dfs(i);
if (!flag)
num++;
}
}
cout << num << endl;
}
return 0 ;
}