马上要比赛了,刷一些基础题练练手,就不刷难题为难自己了哈哈哈哈/菜狗
一、 统计单词(字符串处理)
华中科技大学上机题任意门
题意:读取一行,统计每个单词的字母个数,然后输出出来。
有一坑点就是可能两个空格之间的空格不止一个。
双指针写法:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin,s);//读取一行
int cnt=0;
for(int i=0;i<s.size();i++)
{
if(s[i]==' '||s[i]=='.'){cout<<cnt<<" ";cnt=0;
while(i<s.size()&&s[i+1]==' ')i++;
continue;}
if(s[i]!='.')cnt++;
}
return 0;
}
cin写法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string str;
while (cin >> str)
{
if (str.back() == '.') str.pop_back();//删除.
cout << str.size() << ' ';
}
return 0;
}
二、进制转换(进位制)
北京大学考研机试题任意门
题意:将十六进制转化为十进制
前面有一道每日一题是任意进制转化为任意进制,所以这一道题还算是比较简单的
但是这个可能输入的时候会有负号,所以还是要考虑清楚。
写法一:cin>>hex>>x,表示后面的数为十六进制
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
while(cin>>hex>>x)cout<<x<<endl;
return 0;
}
写法二:手动转进制
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
while(cin>>str)
{
int sign=1;
if(str[0]=='-')
{
sign=-1;
str=str.substr(1);
}
str=str.substr(2);
int res=0;
for(auto c:str)
{
int t=0;
if(c>='A'&&c<='F')t=c-'A'+10;
else if(c>='a'&&c<='f')t=c-'a'+10;
else t=c-'0';
//cout<<t<<endl;
res=res*16+t;
}
cout<<res*sign<<endl;
}
return 0;
}
三、切木棍(推公式)
中南大学考研上机题任意门
题意:给你一个长度为n的木棍,然后问你有多少钟情况,让它能够拼成一个矩形,该矩形不能是正方形。
所以就是推公式
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
if(n&1){cout<<"0"<<endl;continue;}
if(n/2%2==0)cout<<(n/2-1)/2<<endl;
else cout<<n/4<<endl;
}
return 0;
}
四、 根能抵达的点(树的遍历)
南京大学考研机试题任意门
题意:建立一个带边权树,起初所有的节点都能从根节点出发遍历到,然后我们删除边权比x小的点,使根节点能够抵达的节点数目(包括自己)不超过 Y。求x。
这里我们可以用二分来做,二分一定是有序的,但有序的不一定能用二分。
树的遍历—dfs,bfs,并查集
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20010, M = N * 2;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
int dfs(int u, int mid, int fa)
{
int res = 1;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (j == fa || w[i] < mid) continue;
res += dfs(j, mid, u);
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
idx = 0;
for (int i = 0; i < n - 1; i ++ )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
int l = 0, r = 1e8;
while (l < r)
{
int mid = (l + r) >> 1;
if (dfs(0, mid, -1) <= m) r = mid;
else l = mid + 1;
}
printf("%d\n", r);
}
return 0;
}
五、平方因子(试除法、枚举)
任意门
题意:给定一个数 n,判定它是否有一个不为 1 的完全平方数因子。
也就是说,是否存在某个 k,k>1,使得 k2 能够整除 n。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
bool res=false;
for(int i =2;i*i<=n;i++)
{
if(n%(i*i)==0)
{
res=true;
break;
}
}
if(res)puts("Yes");
else puts("No");
}
return 0;
}
六、数字根(同余)
北京大学机试题任意门
题意:一个正整数的数字根是通过对该整数的各位数字求和得到的。
如果得到的结果是一个一位数字,则该这个数字就是所求的正整数的数字根。
注意这道题目的数据范围很大,所以要用string来处理。
性质:一个数除余 9 的余数 和 其各位数字之和除余 9 的余数相同,即同余。
写法一:同余
#include <iostream>
using namespace std;
int main()
{
string str;
while (cin >> str, str != "0")
{
int sum = 0;
for (auto c: str)
sum += c - '0';
sum %= 9;
if (!sum) sum = 9;
cout << sum << endl;
}
return 0;
}
写法二:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string s;
int main()
{
while (cin >> s, s != "0") //如果是0的话就直接跳过
{
int res = 0; //数字根。
while (res == 0 || res > 9) //如果是第一个,或者是当前的数字根不是一位
{
res = 0; //每次初始化数字根
for (char t : s) //那么累加当前数的每一位数
res += t - '0';
if (res > 9) s = to_string(res); //如果当前的数字根不是一位数,将res转成字符串赋值给s
}
cout << res << endl; //最后输出数字根
}
return 0;
}
七、素数(筛质数)
北京航空航天大学机试题任意门
题意:给定一个数,让你筛出1到这个数中所有以1结尾的素数。
#include<bits/stdc++.h>
using namespace std;
bool prime(int x)
{
for(int i=2;i<=x/i;i++)
if(x%i==0)return 0;
return 1;
}
int main()
{
int n,i;
while(cin>>n){
bool ok=0;
for(int i=2;i<=n-1;i++)
if(prime(i)&&i%10==1)cout<<i<<' ',ok=1;
if(ok==0)cout<<-1;
cout<<'\n';
}
}