- 1. Matrix transposition
- 2. Statistical words
- 3. Binary Sorting Tree (DFS)
- 4. IP address
- 5. Special sorting
- 6. a+b (high-precision addition)
- 7. Parity check
- Eight, the largest two numbers
- Nine, binary tree traversal (DFS)
- 10. Sorting
- 11. Conservative numbers
- 12. Maximum value of matrix
- 13. Three employees with the youngest age (structure sort)
- Fourteen, symmetric matrix
- 15. A+B (reading questions)
- 16. Print date (judgment of leap year)
- Writing method 2: while is better
- Seventeen, large number sorting
- Twenty, palindrome string
- 21. Find a location (STL container)
- Twenty-two, factorial
- Twenty-three, octal
- Twenty-four, the longest & shortest text (STL)
- 25. Mersenne primes (prime numbers)
- Twenty-six, word count statistics
- Twenty-seven, hexadecimal conversion (handy)
- Twenty-eight, sorting and deduplication
- Twenty-nine, decimal encryption
- Thirty, delete numbers (greedy)
After reading the real questions of the summer camps in recent years, I feel that it is not particularly difficult. Compared with people who have algorithm experience, it is not difficult, but it may be difficult for people like me who do not master it very well to get full marks. .
Computer test: 2 hours, 4 questions,
there will be a ranking
1. Matrix transposition
This question should be the first question. Very simple, examine the application of two-dimensional arrays.
#include<bits/stdc++.h>
using namespace std;
const int N=100;
int a[N][N];
int main()
{
int n;
cin>>n;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
cin>>a[i][j];
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{
cout<<a[j][i];
if (j==n-1)
cout<<endl;
else cout<<" ";
}
return 0;
}
2. Statistical words
This is also a very simple question, mainly how to read this line.
Writing method 1: read a whole line
getline(cin, s) can read a line.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin,s);
int ans=0,f=0;
for(int i=0;i<s.length();i++)
{
if (s[i]=='.')cout<<ans;
if (s[i]!=' ')ans+=1,f=0;
if (s[i]==' '&&f==0)
{
cout<<ans<<" ";
ans=0;
f=1;
}
}
return 0;
}
Writing method 2: read each word in turn
#include<bits/stdc++.h>
using namespace std;
int main()
{
string st;
while(cin>>st){
if(st[st.size()-1]!='.')cout<<st.size()<<' ';
else cout<<st.size()-1<<' ';
}
}
3. Binary Sorting Tree (DFS)
Recursive tree building.
At the beginning, the root node is −1. When traversing
and finding that it is larger than the root node, traverse the right subtree.
Otherwise, traverse the left subtree.
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n;
int root;
unordered_map<int,int>l,r;
int dfs(int f,int x)
{
if(f==-1)
{
root=x;
return f;
}
if (x>f)
{
if(!r.count(f))/检查这个元素是否存在
{
r[f]=x;
return f;
}
else return dfs(r[f],x);
}
if(x<f)
{
if(!l.count(f))
{
l[f]=x;
return f;
}
else return dfs(l[f],x);
}
}
int main()
{
cin>>n;
root=-1;
int x;
while(n--)
{
cin>>x;
cout<<dfs(root,x)<<endl;
}
return 0;
}
4. IP address
#include<bits/stdc++.h>
using namespace std;
bool check(int a)
{
if(0<=a&&a<=255)
return true;
return false;
}
int main()
{
int a,b,c,d;
while(~scanf("%d.%d.%d.%d",&a,&b,&c,&d))
{
if(check(a)&&check(b)&&check(c)&&check(d))
printf("Yes!\n");
else printf("No!\n");
}
return 0;
}
5. Special sorting
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int a[N];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
cout<<a[n-1]<<endl;
if(n==1) cout<<"-1";
else for (int i=0;i<n-1;i++)
cout<<a[i]<<" ";
return 0;
}
6. a+b (high-precision addition)
#include<bits/stdc++.h>
using namespace std;
int a[1010],b[1010],c[1010];
int main()
{
string s1,s2;
while(cin>>s1>>s2)
{
memset(a,0,sizeof a);
memset(b,0,sizeof b);
memset(c,0,sizeof c);
for(int i=0;i<s1.length();i++)a[i]=s1[s1.length()-i-1]-'0';
for(int i=0;i<s2.length();i++)b[i]=s2[s2.length()-i-1]-'0';
int l=max(s1.length(),s2.length());
int f=0;
for (int i=0;i<l;i++)
{
c[i]=a[i]+b[i];
if(f==1)c[i]+=1,f=0;
if(c[i]>=10)c[i]-=10,f=1;
}
if(f==1)cout<<"1";
for(int i=l-1;i>=0;i--)cout<<c[i];
cout<<endl;
}
return 0;
}
7. Parity check
I feel like this is a simulation question!
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s,ans="";
cin>>s;
int f;
for(int i=0;i<s.length();i++)
{
int num=s[i];
f=0,ans="";
for(int j=6;j>=0;j--)
{
if ((1<<j)<=num)
{
ans+="1";
f++;
num-=1<<j;
}
else ans+="0";
}
if (f&1)cout<<0<<ans<<endl;
else cout<<1<<ans<<endl;
}
return 0;
}
Eight, the largest two numbers
#include<bits/stdc++.h>
using namespace std;
int a[8][8];
int b[8][8];
int main()
{
for(int i=0;i<4;i++)
for(int j=0;j<5;j++)
cin>>a[i][j];
int ans1,ans2;
for(int i=0;i<5;i++)
{
ans1=a[0][i],ans2=a[1][i];
for (int j=2;j<4;j++)
{
if(a[j][i]>min(ans1,ans2))
{
if(ans1>ans2)ans2=a[j][i];
else ans1=ans2,ans2=a[j][i];
}
}
b[0][i]=ans1,b[1][i]=ans2;
}
for(int i=0;i<2;i++)
{
for (int j=0;j<5;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Nine, binary tree traversal (DFS)
This examines thinking and the use of strings.
#include<bits/stdc++.h>
using namespace std;
void dfs(string pre,string in)
{
if(pre.empty())return;
char root=pre[0];
int k=in.find(root);//查找这个根节点在哪个位置
dfs(pre.substr(1,k),in.substr(0,k));
dfs(pre.substr(k+1),in.substr(k+1));
cout<<root;
}
int main()
{
string pre,in;
while(cin>>pre>>in)
{
dfs(pre,in);
cout<<endl;
}
return 0;
}
10. Sorting
#include <bits/stdc++.h>
using namespace std;
struct st{
string name;
int age;
int grade;
bool operator <(const st &x)const{
//将学生数据按成绩从低到高排序,如果成绩相同则按姓名字符的字典序排序,如果姓名的字典序也相同则按照学生的年龄从小到大排序
if(grade==x.grade&&name!=x.name) return name<x.name;
if(grade==x.grade&&name==x.name) return age<x.age;
return grade<x.grade;
}
};
st stu[1010];
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>stu[i].name>>stu[i].age>>stu[i].grade;
}
sort(stu,stu+t);
for(int i=0;i<t;i++)
{
cout<<stu[i].name<<" "<<stu[i].age<<" "<<stu[i].grade<<endl;
}
return 0;
}
11. Conservative numbers
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
if ((n*n)>=10&&n==(n*n)%10)
printf("Yes!\n");
else if((n*n)>=100&&n==(n*n)%100)
printf("Yes!\n");
else printf("No!\n");
}
return 0;
}
12. Maximum value of matrix
#include<bits/stdc++.h>
using namespace std;
const int N=105;
int a[N][N];
int main()
{
int m,n,res,ans,sum;
while(~scanf("%d%d",&m,&n))
{
for (int i=0; i<m; i++)
{
res=-1,ans=-1,sum=0;
for(int j =0; j<n; j++)
{
scanf("%d",&a[i][j]);
sum+=a[i][j];
if(a[i][j]>ans)
{
ans=a[i][j];
res=j;
}
}
a[i][res]=sum;
}
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}
13. Three employees with the youngest age (structure sort)
#include<bits/stdc++.h>
using namespace std;
const int N=35;
struct node {
int num;
string name;
int age;
}p[N];
bool cmp(node x,node y)
{
if(x.age!=y.age)
return x.age<y.age;
else if(x.num!=y.num)
return x.num<y.num;
else return x.name<y.name;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>p[i].num>>p[i].name>>p[i].age;
}
sort(p,p+n,cmp);
for(int i=0;i<min(n,3);i++)
cout<<p[i].num<<" "<<p[i].name<<" "<<p[i].age<<endl;
return 0;
}
Fourteen, symmetric matrix
#include<bits/stdc++.h>
using namespace std;
const int N=110;
int a[N][N];
int main()
{
int n;
while(~scanf("%d",&n))
{
int f=1;
for(int i =0;i<n;i++)
for (int j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(a[i][j]!=a[j][i])
{
f=0;
break;
}
if(f==1)
printf("Yes!\n");
else printf("No!\n");
}
return 0;
}
15. A+B (reading questions)
#include<bits/stdc++.h>
using namespace std;
int main()
{
string A,B;
while(cin>>A>>B)
{
int a=0,b=0,f=1;
for(int i=0; i<A.length(); i++)
{
if(A[i]=='-')
{
f=-1;
continue;
}
if(A[i]==',')
continue;
a=a*10+A[i]-'0';
}
a=f*a;
f=1;
for(int i=0; i<B.length(); i++)
{
if(B[i]=='-')
{
f=-1;
continue;
}
if(B[i]==',')
continue;
b=b*10+B[i]-'0';
}
b=f*b;
long long ans;
ans=a+b;
cout<<ans<<endl;
}
return 0;
}
16. Print date (judgment of leap year)
One way of writing: for
#include<bits/stdc++.h>
using namespace std;
int month[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int y,d;
while(cin>>y>>d)
{
int m=1,day=0;
if(y%100!=0&&y%4==0||y%400==0)
month[2]=29;
else month[2]=28;
for(int i=0;i<d;i++)
{
day++;
if(month[m]<day)
{
m++;
day=1;
}
}
printf("%04d-%02d-%02d\n",y,m,day);
}
return 0;
}
Writing method 2: while is better
#include<bits/stdc++.h>
using namespace std;
int month[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int y,d;
while(cin>>y>>d)
{
int m=1,day=0;
if(y%100!=0&&y%4==0||y%400==0)
month[2]=29;
else month[2]=28;
for(int i=0;i<d;i++)
{
day++;
if(month[m]<day)
{
m++;
day=1;
}
}
printf("%04d-%02d-%02d\n",y,m,day);
}
return 0;
}
Seventeen, large number sorting
#include<bits/stdc++.h>
using namespace std;
const int N=110;
string a[N];
bool cmp(string x,string y)
{
if(x.length()!=y.length())
return x.length()<y.length();
return x<y;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n,cmp);
for(int i=0;i<n;i++)cout<<a[i]<<endl;
return 0;
}
Twenty, palindrome string
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(cin>>s)
{
int i=0,j=s.length()-1,f=1;
while(i<j)
{
if (s[i]==s[j])i++,j--;
else {
f=0;
break;
}
}
if(f==0)cout<<"No!"<<endl;
else cout<<"Yes!"<<endl;
}
return 0;
}
21. Find a location (STL container)
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int len=s.length();
map<char,vector<int>>a;
map<char,int>b;
for(int i=0;i<len;i++)a[s[i]].push_back(i);
for(int i=0;i<len;i++)
{
int len=a[s[i]].size();
if(len>1&&!b[s[i]])
{
for(int j=0;j<len;j++)
{
if(j==0)printf("%c:%d",s[i],a[s[i]][j]);
else printf(",%c:%d",s[i],a[s[i]][j]);
}
b[s[i]]=1;
cout<<endl;
}
}
return 0;
}
Twenty-two, factorial
#include<bits/stdc++.h>
using namespace std;
long long p[15];
int main()
{
int n;
cin>>n;
p[1]=1;
for(int i=2;i<=10;i++)
p[i]=p[i-1]*i;
int y1=0,y2=0;
int t=1;
while(t<=n&&t&1)
{
y1+=p[t];
t+=2;
}
t=2;
while(t<=n&&(t&1)==0)
{
y2+=p[t];
t+=2;
}
cout<<y1<<" "<<y2;
return 0;
}
Twenty-three, octal
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,ans=0,res=1;
while(cin>>n)
{
while(n)
{
int x=n%8;
ans=ans+x*res;
res*=10;
n/=8;
}
cout<<ans<<endl;
ans=0;
res=1;
}
return 0;
}
Twenty-four, the longest & shortest text (STL)
#include<bits/stdc++.h>
using namespace std;
vector<int>len;
int main()
{
unordered_map<int,vector<string>>a;
string s;
while(cin>>s)
{
a[s.size()].push_back(s);
len.push_back(s.size());
}
sort(len.begin(),len.end());
int hh=len[0],tt=len[len.size()-1];
for(auto c:a[hh])
cout<<c<<endl;
for(auto c:a[tt])
cout<<c<<endl;
return 0;
}
25. Mersenne primes (prime numbers)
#include<bits/stdc++.h>
using namespace std;
bool is_prime(long long x)//判断是否为素数
{
if(x<2)return false;
for(long long i=2;i*i<=x;i++)
if(x%i==0)return false;
return true;
}
int main()
{
long long n;
cin>>n;
long long x=2,c=1,cnt=1;
while(c<=n)
{
if(is_prime(c))printf("M(%lld)=%lld\n",cnt,c);
cnt++;
x<<=1;
c=x-1;
}
return 0;
}
Twenty-six, word count statistics
#include<bits/stdc++.h>
using namespace std;
int a[30];
int main()
{
string s;
getline(cin,s);
int ans1=0,ans2=1,f=0,_max=0;
for(int i=0;i<s.size();i++)
{
if(s[i]==' '){
f=1;continue;}
if(f==1)ans2+=1,f=0;
ans1+=1;
a[tolower(s[i])-'a']+=1;
_max=max(_max, a[tolower(s[i])-'a']);
}
cout<<ans1<<endl<<ans2<<endl;
for(int i=0;i<26;i++)
{
if(a[i]==_max)
cout<<(char)('a'+i)<<" ";
}
cout<<endl<<_max;
return 0;
}
Twenty-seven, hexadecimal conversion (handy)
#include<bits/stdc++.h>
using namespace std;
int h[35];
int main()
{
string s;
cin>>s;
int l=s.size(),ans=0,t,x;
t=pow(12,l-1);
for(int i=0;i<l;i++)
{
if(s[i]>='a')x=s[i]-'a'+10;
else x=s[i]-'0';
cout<<x<<" ";
ans+=t*x;
t/=12;
}
cout<<endl<<ans<<endl;
for(int i=0;i<=31;i++)//转换为二进制
{
h[i]=ans&1;
ans>>=1;
}
for(int i=31,j=0;i>=0;i--)
{
cout<<h[i];
j++;
if(j==8)
{
cout<<" ";
j=0;
}
}
return 0;
}
Twenty-eight, sorting and deduplication
#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
int n,x;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end());
for(int i=0;i<a.size();i++)
cout<<a[i]<<" ";
cout<<endl;
for(int i=0;i<a.size();i++)
{
if(a[i]!=a[i+1])
cout<<a[i]<<" ";
}
return 0;
}
Twenty-nine, decimal encryption
This question examines bit operations, and I am a little rusty, so I can do this question to get acquainted.
#include<bits/stdc++.h>
using namespace std;
int a[]={
0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1};
int b[]={
1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0};
int c[]={
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0};
int main()
{
int n,x=0,e=0,p=0;
cin>>n;
for(int i=0;i<31;i++)
{
if((n&(1<<i))==0)continue;
if(a[i]==1){
x|=(1<<i);}
if(b[i]==1){
e|=(1<<i);}
if(c[i]==1){
p|=(1<<i);}
}
long long ans=x+(e<<8)+(p>>24);
cout<<ans;
return 0;
}
//010011010010
//000011010000
//010000000010
//000000000000
Thirty, delete numbers (greedy)
For the deletion problem, a greedy algorithm can be used to solve it. The greedy algorithm is to choose the current optimal solution every time, so as to obtain the global optimal solution. For this question, you can use the following greedy strategy:
scan the numbers from left to right, find the first number that is larger than the number on your right, and delete the number;
if no such number is found, it means that all numbers are increasing , delete the last number directly;
repeat the above process until k numbers are deleted.
Note the removal of the leading 0
#include <iostream>
#include <string>
using namespace std;
int main()
{
string n; // 原始数字n
int k; // 需要删除的数字个数
while (cin >> n >> k)
{
string res = ""; // 存储最终结果
int len = n.size(); // 数字n的长度
// 假如需要删除的数字个数大于等于数字n的长度,直接输出0
if (k >= len)
{
cout << 0 << endl;
continue;
}
int cnt = 0; // 记录已经删除的数字个数
for (int i = 0; i < len; i++)
{
while (cnt < k && !res.empty() && res.back() > n[i])
{
// 如果需要删除的数字尚未全部删除,且新来的数字比当前的倒数第一个数字小
// 就把最后一个数字删除,并将已删除数字个数加1
res.pop_back();
cnt++;
}
res.push_back(n[i]); // 将新的数字添加到结果字符串中
}
// 如果需要删除的数字还没删除完,再从结果字符串末尾删除
while (cnt < k)
{
res.pop_back();
cnt++;
}
// 去掉前导零,并输出结果
int i = 0;
while (i < res.size() && res[i] == '0') i++;
if (i == res.size()) cout << 0 << endl;
else cout << res.substr(i) << endl;
}
return 0;
}