1881: 身份证校验
1 | /*直接模拟题目的意思就可以了,开始做的时候没注意“如果末位是X,则对应位为10”。 |
1887:欧拉与鸡蛋
……我是用暴力模拟,jyf学长口算出答案,强的一批。注意最后输出的时候看下大小,大在前小在后,比赛的时候因为没看到用逗号隔开,wa了两次……
1 | //直接根据条件整理,然后列出方程,暴力求解。 |
放麦子
这是一个等比数列求和,最后的答案是2^64-1。可以直接打开本机计算机自己手算,也可以用c++.
long long 的范围是[-2^63,2^63-1]
unsigned long long 的取值范围刚好是[0,2^64-1],最后的答案是unsigned long long的最大值。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/*pow函数的函数原型
double pow(double a,double b);
所以 pow的返回值类型是double,值越大,最后的结果越不精确
pow要慎用,比个大小还行,如果是值的话会有误差的,题目规定了误差范围也可以用
*/
using namespace std;
int main(){
unsigned long long a,b;
a=(unsigned long long)pow(2,63)-1;//没有强制类型转化会暴
b=2*a+1;
cout<<b<<endl;
return 0;
}
或者直接给a赋值成-1 ,输出a即为最后的结果
1890:取球博弈
方法一:
找规律,如果前面的判断没有错的话,每隔15个是一次循环,只要把前15个找对,后面取模输出就可以了。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16//找规律打表法
using namespace std;
int main(){
int t;
int a[16]={1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1};
cin>>t;
while(t--){
int n;
cin>>n;
int u=n%15;
cout<<a[u]<<endl;
}
return 0;
}
方法二:
这道题最好不要用递归写,但是可以递归出来然后打表,因为数据n<10000,所以如果n很大时,用递归耗时较大
可以用搜索1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23//默默打表:当一局中A输的话,加上1或3或7或8就一定会赢,先把前9种情况列出来,然后进行递推
using namespace std;
int num[10009]={0,0,1,0,1,0,1,0,1,1};/*这里第一个为0的数(即第一次a输)是1个球时,1+8=9,所以要把开始的递推的基础,算出前9个情况的结果,后面依次递推*/
int main(){
for(int i=9;i<=10009;i++){
num[i]=0;
if(num[i-1]==0)num[i]=1;
else if(num[i-3]==0)num[i]=1;
else if(num[i-7]==0)num[i]=1;
else if(num[i-8]==0){
num[i]=1;
}
}
int n;
cin>>n;
int a[1000];
for(int i=0;i<n;i++){
cin>>a[i];
cout<<num[a[i]]<<endl;
}
return 0;
}
1905: 我爱数学 威力加强版
这道题简单的分情况讨论就可以了,但是我比赛的时候没有按从小到大输出wa了很多次1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using namespace std;
int main(){
double a,b,c;
while(cin>>a>>b>>c){
if(a==0){
if(b==0&&c!=0)cout<<"No Solution."<<endl;
else if(b==0&&c==0)cout<<"Unlimited Answers."<<endl;
else if(b!=0){
double ans;
if(c==0)cout<<0.00<<endl;
else {
ans=-(c/b);
cout<<fixed<<setprecision(2)<<ans<<endl;
}
}
}
else {
double temp,kai,ans1,ans2;
temp=b*b-4*a*c;
double mm=-b;
if(temp<0){
cout<<"No Solution."<<endl;
}
else if(temp==0){
double ans3;
ans3=mm/(2*a);
cout<<fixed<<setprecision(2)<<ans3<<endl;
}
else if(temp>0){
kai=sqrt(temp);
ans1=(mm-kai)/(2*a);
ans2=(mm+kai)/(2*a);
cout<<fixed<<setprecision(2)<<min(ans1,ans2)<<" ";
cout<<fixed<<setprecision(2)<<max(ans1,ans2)<<endl;
//min max好用哇,以后再也不要星际比大小了
}
}
}
return 0;
}
正能量矩阵
直接很暴力的递归做,分层递归。
题面里没说,但是看样例的话,答案中的矩阵每个数字后面都是有一个空格的,输入1输出1时候输出的也是1[空格],注意了这一点应该就可以ac了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using namespace std;
int mapp[1003][1003]={0};
int tot=1;
void work(int n,int rec){
for(int i=rec;i<=n-rec+1;i++){
mapp[rec][i]=tot;
}//从左向右
tot++;
for(int i=rec+1;i<=n-rec+1;i++){
mapp[i][n-rec+1]=tot;
}//从上向下
tot++;
for(int i=rec;i<=n-rec;i++){
mapp[n-rec+1][i]=tot;
} //右向左
tot++;
for(int i=rec+1;i<=n-rec;i++){
mapp[i][rec]=tot;
}//上
tot++;
if(rec+1>(n+1)/2)return ;//循环边界
else work(n,rec+1);
return ;
}
int main(){
int n;
while(cin>>n){
memset(mapp,0,sizeof(mapp));
tot=1;
work(n,1);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<mapp[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
return 0;
}
微信红包
这个题目很水,直接输出中位数,中位数就是答案。
但是直接用cin输入的话,显示CPU时间超限,改成scanf直接过了,
如果用cin的话一定要关闭输入输出流哇……
还有就是cin scanf不能混用,最好一道统一用一种(输入输出都不能混用)
机器人行走
一道模拟题……
要注意的是每次转身后,面朝的方向都会改变,从而左右的相对位置也要改变。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57//设计函数控制头和操作之后的方向,然后对操作进行模拟
using namespace std;
int x=0,y=0;
char judge(char a,char b,int n){//开始的方向,进行的操作,步数
switch(a){
case 'N':
if(b=='L'){x=x-n;return 'W';}
else {x=x+n;return 'E';}
case'S':
if(b=='L'){x=x+n;return 'E';}
else {x=x-n;return 'W';}
case 'W':
if(b=='L'){y=y-n;return 'S';}
else{y=y+n;return 'N';}
case 'E':
if(b=='L'){y=y+n;return 'N';}
else {y=y-n;return 'S';}
}
}
int main(){
int n;
cin>>n;
while(n--){
x=0;y=0;
string a;
cin>>a;
int len=a.length(),i=0,sum=0;
//处理第一个的步数
if(i<len&&a[i]>='0'&&a[i]<='9'){
while(i<len&&a[i]<='9'&&a[i]>='0'){
sum=sum*10+a[i]-'0';
i++;
}
y=y+sum;
}
//开始
char now='N';
while(i<len){
char L=a[i];
i++;
sum=0;
while(i<len&&a[i]>='0'&&a[i]<='9'){
sum=sum*10+a[i]-'0';
i++;
}
now=judge(now,L,sum);
}
double ans;
ans=sqrt(x*x+y*y);
cout<<fixed<<setprecision(2)<<ans<<endl;
}
return 0;
}