<> The second game of the 11th Blue Bridge Cup provincial competition C/C++ B group
A. House plate making
meaning of the title :
Make from 1 reach 2020 How many characters do you need to use in these numbers 2;
thinking :
for loop , And split the numbers , Judge from everyone .
code :
#include<bits/stdc++.h> using namespace std; int solve(int n) { int ant=0;
while(n)// Split the numbers { int x=n%10; if(x==2)ant++; n/=10; }return ant; } int main() {
int sum=0; for(int i=1;i<=2020;i++) { sum+=solve(i); }cout<<sum<<endl; }// answer 624
B. Approximate score
meaning of the title :
Judge from 1 reach 2020 How many of them are about scores .
thinking :
greatest common factor ,gcd Just judge , If it meets the conditions, it will accumulate ;
code
#include<bits/stdc++.h> #define ll long long using namespace std; ll gcd(ll a,
ll b)// greatest common factor { return b==0?a:gcd(b,a%b); } int main() { int sum=0; for(ll i=1;i
<=2020;i++){ for(ll j=1;j<=2020;j++){ if(gcd(i,j)==1)// Judge whether the conditions are met sum++;}} cout<<
sum<<endl; } // answer 2481215
C. Serpentine filling number
meaning of the title :
Follow the picture , Last paragraph 20 Line number 20 What is the number in the column ?
thinking :
Because it's a blank question , I directly find the law to solve it ( I'm sure all of you can write a program ).1,5,13,25…d1=4,d2=8,d3=12…dn=4*n;
code
#include<bits/stdc++.h> using namespace std; int main() { int sum,n; while(cin
>>n){ int d=1,sum=1; for(int i=1;i<n;i++) { d=4*i; sum+=d; }cout<<sum<<endl;} }
// answer 761
D. Running exercise
meaning of the title :
from 2000.01.01 Saturday to 2020.10.01 Thursday , During these times , If it's the beginning of the month or Monday, run 2 Kilometer , Run at other times 1 Kilometer .
code
#include<bits/stdc++.h> using namespace std; int main() { int year=2000,month=1
,day=1,week=6,sum=0; int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; while(
year!=2020||month!=10||day!=1) { if((year%400==0)||(year%4==0&&year%100!=0))
// Judge leap year {a[2]=29;} else {a[2]=28;}day++;// Days accumulation week=(week+1)%7;//weak==0 On behalf of Sunday if(
day>a[month])// Judge whether the number of days is greater than that of the current month { day=1; month++; } if(month>12)// Judge whether it exceeds 12 month { month
=1; year++; } if(day==1||week==1)// Judge whether it meets the conditions of the topic { sum++;// Add it again at the beginning of the month or on Monday } sum++; }
cout<<sum+2<<endl;// Plus the first and last days }// answer 8879
F. Achievement statistics
meaning of the title :
Enter some grades , Score greater than 60 The score is pass , greater than 85 The score is excellent , Output pass rate and excellent rate ( The answer needs to be rounded and the whole number and percentage sign should be kept )
thinking :
Write two if Sentence judgment , And add up , When the pass rate and excellent rate are finally output , Need to add 0.5, You can have the effect of rounding ,( Note that the output needs to be rounded ).
code
#include<bits/stdc++.h> using namespace std; int main() { int n,x; while(cin>>n
) { float sum1=0,sum2=0; for(int i=0;i<n;i++) { cin>>x; if(x>=60)// Pass judgment sum1++;
if(x>=85)// Excellent judgment sum2++; } int a=float(sum1/n*100+0.5);// Produces a rounding effect int b=float(
sum2/n*100+0.5); cout<<a<<"%"<<endl; cout<<b<<"%"<<endl; }return 0; }
G. Palindrome date
meaning of the title :
Enter a date , The date is 8 Digit number , You need to output the next qualified palindrome date and one, which is the palindrome date ABABBABA Palindrome date in form .
thinking :
Add the entered date automatically , And judge whether it is a leap year and whether the date is the correct date . After judgment, split the date into 8 Digit number , Store in array , If
a[0]==a[7]&&a[1]==a[6]&&a[2]==a[5]&&a[3]==a[4
] Meet palindrome , If the palindrome is satisfied **a[0]==a[2]&&a[1]==a[3]&&a[0]!=a[1]** satisfy ABABBABA Formal palindrome . See the code for details .
code
#include<bits/stdc++.h> using namespace std; int d[13]={0,31,28,31,30,31,30,31,
31,30,31,30,31}; bool runyears(int year)// Judge whether it is a leap year { return year%400==0||(year%4
==0&&year%100!=0); } bool date(int year,int month,int day)// Judge whether the date is correct { if(month>
12) { return false ; } if(runyears(year)&&month==2) { return day<=29; }return
day<=d[month]; } int main() { bool flag=false; int n; cin>>n; for(int i=n+1;i<
89991231;i++) { int a[8]; for(int j=7,m=i;j>=0;j--,m/=10) { a[j]=m%10;// Split date }
int year=a[0]*1000+a[1]*100+a[2]*10+a[3]; int month=a[4]*10+a[5]; int day=a[6]*
10+a[7]; if(!date(year,month,day))// If the date is incorrect , Skip {continue; } // Determine whether it is palindrome if(a[0]==
a[7]&&a[1]==a[6]&&a[2]==a[5]&&a[3]==a[4]) { if(flag==false) { cout<<i<<endl;
flag=true; } // Judge whether it is ABABBABA Palindrome form if(a[0]==a[2]&&a[1]==a[3]&&a[0]!=a[1]) { cout
<<i<<endl; break; } } }return 0; }
H. Substring score sum
meaning of the title :
Enter a string , Continuously split into different substrings in order , And calculate the number of different letters in each substring , Sum output
thinking :
Use container set duplicate removal , use substr Function intercepts substrings in order . Please refer to the code for specific operation .
code
#include<bits/stdc++.h> using namespace std; int solve(string s)// The container automatically deletes duplicate characters
{ set<char>st; for(int i=0;i<s.size();i++) { st.insert(s[i]); }return st.size();
// Character length after de duplication } int main() { int sum=0; string s; cin>>s; for(int i=0;i<s.size();i
++) { for(int j=i;j<s.size();j++) { string ss=s.substr(i,j-i+1);// Character interception sum+=
solve(ss); } }cout<<sum<<endl; }
** If you think the blogger's writing is good, please praise it , Bixin !**
Technology
Daily Recommendation