preface
- Environment: Dev-C++
- Universal header file: #include < bits / STDC + + h>
- The level is not high, simply record the winter vacation life.
Basic exercises
BASIC-01 A+B problem
Problem solving ideas
AC code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a + b;
return 0;
}
BASIC-02 sequence summation
Problem solving ideas
- Using the formula of equal difference sequence to sum
- Pay attention to the data range
AC code
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n;
cin>>n;
cout<<(1+n)*n/2;
return 0;
}
BASIC-03 area of circle
Problem solving ideas
- Use area formula of circle
- PI needs to be defined. acos(-1.0) is used here
- Note the output format
AC code
#include<bits/stdc++.h>
#define PI acos(-1.0)
using namespace std;
int main()
{
int r;
cin>>r;
double s=r*r*PI;
printf("%.7lf",s);
return 0;
}
BASIC-04 Fibonacci sequence
Problem solving ideas
- There is no need to obtain the result and take the mold directly
- The direct recursion time will exceed the limit
AC code
#include<bits/stdc++.h>
using namespace std;
long long a[1000050];
int main( )
{
int n;
cin>>n;
a[1]=a[2]=1;
for(int i=3;i<=n;i++)
{
a[i]=(a[i-1]+a[i-2])%10007;
}
cout<<a[n];
}
BASIC-1 leap year judgment
Problem solving ideas
- Understand the characteristics of leap years
- Simple judgment statement
AC code
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int y;
cin>>y;
if((y%4==0&&y%100!=0)||y%400==0)
cout<<"yes";
else
cout<<"no";
return 0;
}
BASIC-2 01 string
Problem solving ideas
- My method is 0-31, a total of 32 numbers. Try to express them in binary.
- The output of mindless violence can pass (the dog shakes his head)
AC code
#include<bits/stdc++.h>
using namespace std;
int main( )
{
for(int i=0;i<=31;i++)
{
int j=i;
cout<<j/16; //The first indicates how many 16
j%=16;
cout<<j/8; //How many 8 are there after taking the mold of 16
j%=8;
cout<<j/4; //How many 4 are there after taking the mold of 8
j%=4;
cout<<j/2; //How many 2 are there after taking the mold of 4
j%=2;
cout<<j; //There are several 1s (i.e. itself) for 2 molds
cout<<endl;
}
return 0;
}
BASIC-3 alphabetic graphics
Problem solving ideas
- First assign all diagonals to A, and then assign them to the left and right
AC code
#include<bits/stdc++.h>
using namespace std;
char a[27][27];
int main( )
{
int n,m;
cin>>n>>m;
for(int i=0;i<26;i++) //Enter the complete table
{
a[i][i]='A';
for(int j=0;j<26-i;j++)
a[i][i+j]='A'+j;
for(int k=0;k<=i;k++)
a[i][i-k]='A'+k;
}
for(int i=0;i<n;i++) //Output n rows and m columns
{
for(int j=0;j<m;j++)
{
cout<<a[i][j];
}
cout<<endl;
}
return 0;
}
BASIC-4 sequence features
Problem solving ideas
- First save the number into the array
- Simple maximum and minimum value comparison, re assignment and sum summation. Note that the initial value of maxm should be set to be less than - 10000
AC code
#include<bits/stdc++.h>
using namespace std;
int a[10050];
int main( )
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int maxm=-10010;
int minm=10010;
int sum=0;
for(int i=0;i<n;i++)
{
if(maxm<=a[i])
maxm=a[i];
if(minm>=a[i])
minm=a[i];
sum+=a[i];
}
cout<<maxm<<endl<<minm<<endl<<sum;
return 0;
}
BASIC-5 find integer
Problem solving ideas
- Simple traversal, and then judge the output. Note that the subscript is plus 1
AC code
#include<bits/stdc++.h>
using namespace std;
int a[1050];
int main( )
{
int n,m;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cin>>m;
for(int i=0;i<n;i++)
{
if(a[i]==m)
{
cout<<i+1; //The subscript plus one represents the number, because the subscript starts from 0
return 0;
}
}
cout<<-1;
return 0;
}
BASIC-6 Yang Hui triangle
Problem solving ideas
- Simple Yang Hui triangle, method see notes
AC code
#include<bits/stdc++.h>
using namespace std;
int a[34][34];
int main( )
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
a[i][i]=1; //Left diagonal all assigned to 1
a[i][0]=1; //The first column is all assigned as 1
}
for(int i=2;i<n;i++)
{
for(int j=1;j<i+1;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];//Traverse the assignment from a[2][1], and each is equal to the sum of the two numbers on the shoulder
}
for(int i=0;i<n;i++)
{
for(int j=0;j<i+1;j++) //The output method here is a little fastidious. I don't know if multiple spaces after each line will be wrong, but it must be right
{
if(j==0)
cout<<a[i][j];
else
cout<<' '<<a[i][j];
}
cout<<endl;
}
return 0;
}
BASIC-7 special numbers
Problem solving ideas
- Determine whether it is equal to the original number by stripping each number and adding it to the third power
AC code
#include<bits/stdc++.h>
using namespace std;
int main( )
{
for(int i=100;i<=999;i++)
{
int n,a,sum=0;
n=i;
while(n)
{
a=n%10; //The first cycle a is a digit number, the second cycle is a ten digit number, and the third cycle is a hundred digit number
n/=10;
sum+=pow(a,3);
}
if(sum==i)
cout<<i<<endl;
}
return 0;
}
BASIC-8 palindromes
Problem solving ideas
- Similarly, through the stripping method, peel off each bit and multiply it by 10 to add it for judgment
AC code
#include<bits/stdc++.h>
using namespace std;
int main( )
{
for(int i=1000;i<10000;i++)
{
int n,a,sum=0;
n=i;
while(n)
{
a=n%10;
n/=10;
sum*=10;
sum+=a;
}
if(i==sum)
cout<<i<<endl;
}
return 0;
}
BASIC-9 special palindromes
Problem solving ideas
- Similar to the above question, but you also need to add and sum the stripped numbers. You need two judgments to be true at the same time
AC code
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int m;
cin>>m;
for(int i=10000;i<1000000;i++)
{
int n,a,sum1=0,sum2=0;
n=i;
while(n)
{
a=n%10; //Split bit
n/=10; //Divide yourself by 10
sum1*=10;
sum1+=a; //sum1 find the flipped number
sum2+=a; //sum2 find the sum of the numbers on you
}
if(sum1==i && sum2==m)
cout<<i<<endl;
}
return 0;
}
BASIC-10 decimal to hexadecimal
Problem solving ideas
- Master the principle of binary conversion
AC code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int k, n;
int a[100];
cin>>n;
k = 0;
while (n >=16)//This loop stores each decimal bit in the array in reverse order
{
a[k] = n % 16;
k++;
n = n / 16;
}
a[k] = n % 16;
for (k; k>=0; k--)//Convert to hexadecimal and then output in reverse order
{
if (a[k] < 10)
printf("%d", a[k]);
else
printf("%c", 'A' + a[k] - 10);
}
return 0;
}
BASIC-11 hex to decimal
Problem solving ideas
- Taking FFFF as an example, the first sum is 15, the second sum is 1516 + 15, the third sum is (1516 + 15) 16 + 15, and the fourth sum is ((1516 + 15) 16 + 15) 16 + 15, that is, 15163 + 15162 + 15161 + 15160
#include<bits/stdc++.h>
int main()
{
int i, l;
long long sum;
char a[100];
sum = 0;
gets(a);
for (i=0; a[i];i++)
{
if (a[i] <='9')
a[i] = a[i] - '0';
else if(a[i]<='F')
a[i] = a[i] - 'A' + 10;
else
a[i] = a[i] - 'a' + 10;
}
l = strlen(a);
for (i = 0; a[i]; i++)
sum = sum + a[i] * pow(16, l - i - 1);
printf("%lld", sum);
return 0;
}
BASIC-12 hex to octal
Problem solving ideas
- First convert hexadecimal to binary, and then convert to octal
- One hexadecimal bit corresponds to four binary bits and is directly converted into a string
- When binary is converted to octal, three bits are converted to octal one bit, so supplement 0 in advance
- Then output the octal string from the first place that is not '0'
#include<bits/stdc++.h>
using namespace std;
string _16_2_(string str)
{
string a="";
for(int i=0;i<str.size();i++)
{
switch(str[i]) //Convert hexadecimal to four digit binary
{
case '0': a+= "0000"; break;
case '1': a+= "0001"; break;
case '2': a+= "0010"; break;
case '3': a+= "0011"; break;
case '4': a+= "0100"; break;
case '5': a+= "0101"; break;
case '6': a+= "0110"; break;
case '7': a+= "0111"; break;
case '8': a+= "1000"; break;
case '9': a+= "1001"; break;
case 'A': a+= "1010"; break;
case 'B': a+= "1011"; break;
case 'C': a+= "1100"; break;
case 'D': a+= "1101"; break;
case 'E': a+= "1110"; break;
case 'F': a+= "1111"; break;
default: break;
}
}
return a;
}
string _2_8(string str) //Then convert binary to octal, pay attention to complement 0, and complement binary to an integer multiple of 3
{
string a;
switch(str.size()%3)
{
case 0:break;
case 1:str="00"+str;break;
case 2:str="0"+str;break;
default:break;
}
for(int i=0;i<str.size();i+=3)
{
int m=(str[i]-'0')*4+(str[i+1]-'0')*2+(str[i+2]-'0');
a+=(m+'0');
}
return a;
}
int main( )
{
int n;
cin>>n;
string s;
while(n--)
{
cin>>s;
string a=_16_2_(s); //Convert to binary first
string b=_2_8(a); //And then octal
int k=b.size();
int p=0;
while(b[p]=='0') p++; //When the title requires output, it does not need to be preceded by 0, so it starts when it is not 0
for(int i=p;i<k;i++) cout<<b[i];
if(n>=1) cout<<endl; //This line is essential. It is wrong to output less line breaks
}
return 0;
}
So far, the non VIP part of the basic exercises of the Blue Bridge Cup has been completed