青蛙爬井
有一口深度为 highhigh 米的水井,井底有一只青蛙,它每天白天能够沿井壁向上爬 upup 米,夜里则顺井壁向下滑 downdown 米。
若青蛙从某个早晨开始向外爬,当 high = 60405,up = 105,dow = 35,计算青蛙多少天能够爬出井口?
注意:不能简单地认为每天上升的高度等于白天向上爬的距离减去夜间下滑的距离,因为若白天能爬出井口,则不必等到晚上。
样例输入复制
样例输出复制
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std ;
int main()
{
int high ;
int up ;
int down ;
int ans = 0 ;
cin >>high >> up >>down ;
int n = 0 ;
while(n<=high){
ans++;
n+=up ;
if(n>high){
break ;
}
n-=down ;
}
cout<<ans ;
return 0;
}
倍数
一天蒜头君在想,[l,r]之间有多少个数字是 d的倍数呢?
但是区间 [l,r] 是 d 的倍数的数字太多,于是聪明的蒜头君便找到了你。
当 l = 1032,r=12302135942453,d=234,d 的倍数有多少个呢?
样例输入复制
样例输出复制
int main()
{
LL ans = 0 ;
LL l = 1032 , r = 12302135942453 ;
LL d = 234 ;
// [1,l] 满足是d的倍数的有 l/d
// [1,r] 满足是d的倍数的有 r/d
// [l,r] r/d - l/d ;
cout<<r/d - l/d;
return 0;
}
找质数
一天蒜头君猜想,是不是所有的偶数(除了 2),都可以用两个质数相加得到呢?于是聪明的蒜头君就找你来验证了。
输入格式
第一行输入一个整数 t表示测试组数。
接下来 t 行,每行一个整数 n。
输出格式
输出两个整数,因为答案可能有多个,所有要求输出的这两个整数是所有答案中字典序最小的。
数据范围
样例输入复制
样例输出复制
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std ;
const int MAX = 1000005 ;
typedef long long LL;
const int inf = 0x3f3f3f3f ;
bool is_prime[MAX] ;
int prime[MAX] ;
bool Prime[MAX];
int fac[MAX] ;
int euler(int n ){ // 欧拉筛
int cnt = 0 ;
memset(Prime,true,sizeof(Prime)) ;
Prime[0] = Prime[1] = false ;
for(int i = 2 ; i<n;i++ ){
if(Prime[i]){
fac[cnt++] = i ;
}
for(int j = 0 ; j<cnt &&fac[j]<=n/i ;j++ ){
Prime[i*fac[j]] = false;
if(i%fac[j]==0 ) {
break ;
}
}
}
return cnt ;
}
int sieve(int n ){
int cnt = 0 ;
for(int i = 0 ; i<=n+1 ;i++){
is_prime[i] = true;
}
is_prime[0] = is_prime[1] = false ;
for(int i = 2 ; i<=n ; i++ ) {
if(is_prime[i]){
prime[cnt++] = i ;
for(int j = 2*i ; j<=n ;j+=i){
is_prime[j] = false ;
}
}
}
return cnt;
}
int main()
{
int T ;
cin >>T;
int num = euler(MAX) ;
while(T-- ){
int n ;
scanf("%d",&n);
for(int i = 0 ; i<num ; i++){
if(Prime[n-fac[i]]){
printf("%d %d\n",fac[i] ,n-fac[i]);
break ;
}
}
}
return 0;
}
后缀字符串
一天蒜头君得到 n 个字符串 si,每个字符串的长度都不超过 10。
蒜头君在想,在这 n 个字符串中,以 si 为后缀的字符串有多少个呢?
输入格式
第一行输入一个整数 n。
接下来 n 行,每行输入一个字符串 s_isi。
输出格式
输出 n 个整数,第 i 个整数表示以 s_isi 为后缀的字符串的个数。
数据范围
对于 50%的数据,1 10^31≤n≤103。
对于 100% 的数据,1 10^51≤n≤105。
所有的字符串仅由小写字母组成。
样例输入复制
3
ba
a
aba
样例输出复制
2
3
1
这里向大家介绍一下substr()函数,这是专门用来处理strign类的字符串的:
用途:一种构造string的方法
形式:s.substr(pos, n)
解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
using namespace std ;
const int MAX = 1000005 ;
typedef long long LL;
const int inf = 0x3f3f3f3f ;
string str[MAX] ;
int main()
{
int t ;
map<string,int> q ;
cin >>t ;
for(int i = 0 ; i<t ; i++ ) {
cin>>str[i] ;
for(int j = 0 ; j<str[i].size() ; j++ ) {
q[str[i].substr(j)]++ ;
}
}
for(int i = 0 ;i<t ; i++){
cout<<q[str[i]]<<endl ;
}
return 0;
}