第十一届蓝桥杯大赛软件类省赛第二场 C&&C++大学 B组
试题A:门牌制作
思路:
1到2020之间的数字少,可直接采用一个循环,然后对里面的每一个数字进行查找
identifying code
#include<iostream>
using namespace std;
int main()
{
int sum=0;
for(int i=1;i<=2020;i++){
int x=i,t;
//对里面的每个数字进行判断
while(x>0){
t=x%10;
x/=10;
if(t==2)
sum++;
}
}
cout<<sum<<endl;
return 0;
}
答案:624
试题B:寻找2020
解题思路
就是采用两个循环进行分子分母的遍历,然后判断分字分母是否是互为质数,是则 ans++;
Identifying code
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int ans=0;
for(int i=1;i<=2020;i++)
{
for(int j=1;j<=2020;j++)
{
if(__gcd(i,j)==1)
ans++;
}
}
cout<<ans<<endl;
return 0;
}
答案:2481215
试题C:蛇形填数
解题思路
identifying code
#include<iostream>
using namespace std;
int main()
{
int s[100][100];
int x=0,y=0;
int data=1;
s[x][y]=data++;
while(s[19][19]==0){
if(x==0){
y++;//向右
s[x][y]=data++;
while(y!=0){//左下移动
x++;
y--;
s[x][y]=data++;
}
}else if(y==0){
x++;//垂直向下
s[x][y]=data++;
while(x!=0){ //右上移动
x--;
y++;
s[x][y]=data++;
}
}
}
cout<<s[19][19]<<endl;
return 0;
}
答案:761
试题D:跑步锻炼
解题思路
identifying code
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int start=5;//0星期一,1星期二,2星期三,3星期三,4星期四,5星期六,6星期日
int ans=0,m=0,year,day;
year=2000;
while(year!=2020 || month[m]!=10){
if(year%400==0 || (year%4==0&&year%100!=0))
month[2]=29;
else month[2]=28;
for(m=1;m<=12;m++){
if(year==2020 && m==10) goto mark;
int day=month[m]-(7-start); //舍弃初始的一周
ans++;
ans+=day/7; //一个月剩下时间有多少个周一
start=day%7;
if(start!=0) ans++; //这个月剩下的残缺周还有一个星期一
ans+=month[m];
}
year++;
}
mark:
ans+=2;
cout<<ans<<endl;
return 0;
}
答案:8879