A - 蟠桃记(非迭代效率更高)
B - 水仙花数(打表)
C - 多项式求和(C语言基础题)
D - 进制转换(模板题目)
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
char a[20]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void fun(int n,int r) {
string s;
if(n<0){
cout<<'-';
n=-n;
}
if(n<r) printf("%d\n",n); //特况判断
else {
while(n) {
s+=a[n%r]; //先取余后移位
n=n/r;
}
reverse(s.begin(),s.end()); //注意该函数特殊之处
cout<<s<<endl;
}
}
int main() {
int n,r;
while(~scanf("%d%d",&n,&r)) {
fun(n,r);
}
return 0;
}
E - Magic Numbers
A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.
You're given a number. Determine if it is a magic number or not.
Way1:字符接收,正着判断
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int main() {
string s;
s.reserve(15);
cin>>s;
int flag=0;
for(int i=0;i<s.size();){
if(s[i]=='1'&&s[i+1]=='4'&&s[i+2]=='4')
i+=3;
else if(s[i]=='1'&&s[i+1]=='4')
i+=2;
else if(s[i]=='1')
i++;
else{
flag=1;
break;
}
}
if(flag) cout<<"NO\n";
else cout<<"YES\n";
return 0;
}
Way2: 数字接收(long long),倒着判断
#include<iostream>
using namespace std;
int main(){
long long x;
cin>>x;
int flag=0;
while(x){
if(x%10!=1&&x%100!=14&&x%1000!=144){ //这个创意我给满分!
flag=1;
break;
}
x/=10;
}
if(flag) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
F - Cakeminator(为避免重复,先找evil strawberry,标记该行该列,最后统计)
You are given a rectangular cake, represented as an r × c grid. Each cell either has an evil strawberry, or is empty. For example, a 3 × 4cake may look as follows:
The cakeminator is going to eat the cake! Each time he eats, he chooses a row or a column that does not contain any evil strawberries and contains at least one cake cell that has not been eaten before, and eats all the cake cells there. He may decide to eat any number of times.
Please output the maximum number of cake cells that the cakeminator can eat.
Input
The first line contains two integers r and c (2 ≤ r, c ≤ 10), denoting the number of rows and the number of columns of the cake. The next r lines each contains c characters — the j-th character of the i-th line denotes the content of the cell at row i and column j, and is either one of these:
- '.' character denotes a cake cell with no evil strawberry;
- 'S' character denotes a cake cell with an evil strawberry.
Output
Output the maximum number of cake cells that the cakeminator can eat.
Examples
Input
3 4 S... .... ..S.
Output
8
Note
For the first example, one possible way to eat the maximum number of cake cells is as follows (perform 3 eats).
#include<iostream> #include<cstring> #include<string> #include<cmath> #include<algorithm> using namespace std; char mp[15][15]; int main() { int r,c; cin>>r>>c; for(int i=0;i<r;i++) scanf("%s",&mp[i]); for(int i=0;i<r;i++){ int flag=0; for(int j=0;j<c;j++){ if(mp[i][j]=='S'){ flag=1; break; } } if(flag!=1){ for(int k=0;k<c;k++) if(mp[i][k]=='.') mp[i][k]='*'; } } for(int j=0;j<c;j++){ int flag=0; for(int i=0;i<r;i++){ if(mp[i][j]=='S'){ flag=1; break; } } if(flag!=1){ for(int k=0;k<r;k++) if(mp[k][j]=='.') mp[k][j]='*'; } } int cnt=0; for(int i=0;i<r;i++) for(int j=0;j<c;j++) if(mp[i][j]=='*') cnt++; cout<<cnt; return 0; }
G - Down the Hatch!
题意:n个人,你是0号,从0开始到n-1循环做动作,只要你前面三个人动作一样,你就喝一杯橙汁,问你能喝多少杯
思路:一轮后从n开始,看其前三个人是否动作一致(这三人是上一轮的)
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
int cnt=0,n;
cin>>n>>s;
for(int i=n;i<s.size();i+=n)
if(s[i-1]==s[i-2]&&s[i-2]==s[i-3]) cnt++;
cout<<cnt;
return 0;
}
H - Candy Bags
Way1:充分利用该题特点:偶数
#include<iostream>
#include<string>
using namespace std;
int main() {
int n;
cin>>n;
int head=1;
for(int i=1;i<=n;i++){
int nn=n;
while(nn){
cout<<head<<' '<<n*n-head+1<<' ';
nn-=2;
head++;
}
cout<<endl;
}
return 0;
}
Way2:标准做法,无论是否偶数
(版权:狗壮的,原文链接:https://blog.youkuaiyun.com/qq_41938789/article/details/86682379)
#include<iostream>
using namespace std;
int main() {
int n,i,j,flag=0;
int a[102][102];
while(cin>>n) {
for(i=0; i<n; i++)
for(j=1; j<=n; j++)
a[i][j-1]=i*n+j;//1~n^2赋值给二维数组
for(i=0; i<n; i++) {
flag=0;
for(j=0; j<n; j++) {
if(flag==0) {
flag=1;
cout<<a[j][(j+i)%n];//按照相对位置的不同输出
} else
cout<<" "<<a[j][(j+i)%n];
}
cout<<endl;
}
}
return 0;
}
I - Key Set(快速幂)
题意:给出一个数n,表示一个集合中有n个数1~n,找出这个集合中符合情况的子集合——非空且所有元素相加为偶数。
思路:n个数的非空子集共2^n-1个,剔除原集合中的1,剩下的集合有2^n-1个。那么剩下的集合中,元素相加之和为奇数的加上1为偶数,剩下为偶数的不加1.所以结果就是2的n次方-1。
方法:数据大——快速幂。
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#define ll long long
using namespace std;
int MOD=1000000007;
//快速幂模板
ll quick_pow(ll a,ll n,ll mod){
ll ans=1;
a=a%mod;
while(n){
if(n&1) ans=(ans*a)%mod;
n/=2;
a=(a*a)%mod; //是幂在自乘啊小傻瓜
}
return ans;
}
int main() {
int t,n,ans;
cin>>t;
while(t--){
scanf("%d",&n);
ans=quick_pow(2,n-1,MOD)-1;
printf("%d\n",ans);
}
return 0;
}
J - Candies(自己想想就好了嘛)
题意:n个糖果分给m个人,不能剩余且尽量匀
#include<iostream>
#include<string>
using namespace std;
int main() {
int n,m;
cin>>n>>m;
int tmp=n/m;
int sp=n-tmp*m;
for(int i=1;i<=sp;i++) cout<<tmp+1<<' ';
for(int i=1;i<=m-sp;i++) cout<<tmp<<' ';
return 0;
}
K - Eugeny and Array
题意:给定一个只由-1,1组成的数列,在给定起始位置和结束位置后,问在可以对数列中数字进行任意排列的情况下能否让从给定的起始位置到结束位置之间的数字之和为0.
思路:1.统计出数列中1和-1的个数,2.判断起始位置和结束位置中间包含多少个数字,奇数——和肯定不为0,偶数——判断数列中1和-1的个数能否达到该区间数字个数的一半,只有一半1和一半-1时才能确保加起来为0,否则也是不可以的
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int main() {
int n,m,x,minn,l,r,tmp;
cin>>n>>m; //按多组数据当然不算错
int a1=0,a2=0;
for(int i=0; i<n; i++) {
scanf("%d",&x);
if(x>0) a1++;
else a2++;
}
minn=min(a1,a2);
for(int i=0; i<m; i++) {
scanf("%d%d",&l,&r);
tmp=r-l+1;
if(tmp%2!=0) printf("0\n");
else {
if(minn>=tmp/2) printf("1\n"); //注意是谁的一半
else printf("0\n");
}
}
return 0;
}
L - Cure(打表,观察数据收敛1.64493)
Given an integer n, we only want to know the sum of 1/k^2 where k from 1 to n.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
long double a[1000100]; //long double
char s[30];
int main(){
a[0]=0.0;
for(int i=1;i<=1000000;i++){
a[i]=a[i-1]+1.0/(1.0*i*i); //一定要分母*1.0,否则会inf
// printf("%.5f\n",(double)a[i]); //一定强制类型转换! 否则画面太美我不敢看
}
string s;
while(cin>>s){
int len=s.size();
if(len>=7)printf("1.64493\n"); //收敛在这里!
else {
int n=0;
for(int i=0;i<len;i++) n=n*10+s[i]-'0';
printf("%.5f\n",(double)a[n]);
}
}
return 0;
}
M - Easy Math
题意:给定n个数,判断它们的开方和是否为整数
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int a[100005];
int main() {
int n,tmp,x;
while(~scanf("%d",&n)){
int flag=0;
for(int i=0;i<n;i++){
scanf("%d",&x);
if(flag) continue;
tmp=sqrt(x);
if(tmp*tmp!=x) flag=1;
}
if(flag==0) printf("Yes\n");
else printf("No\n");
}
return 0;
}
附:在看别人题解的时候看到一个比较全的头文件模板如下
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
typedef long long ll;
typedef long double ld;