提前批C++AC3.2,没收到面试通知,又投了正式批
1. 求一个矩阵中的两个数,要求两个数不在一行一列,使得这两个数的乘积最大,输出最大积。
没啥好的思路,直接暴力,居然过了
#include <bits/stdc++.h> using namespace std; int main() { int m,n; cin>>m>>n; int a[m][n]; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>a[i][j]; } } int max=-1; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { int tmp=0; for(int k=0;k<m;k++) { if(k==i) { continue; } for(int h=0;h<n;h++) { if(h==j) { continue; } if(a[k][h]>tmp) { tmp=a[k][h]; } } } //cout<<tmp<<" "; if((tmp*a[i][j])>max) { max=a[i][j]*tmp; } } } cout<<max; return 0; }2. 按给定的输出序列判断排序方法,编程该排序方法,输出排序结果
按快排写的,也过了
3. 给定一个整数,判断用二进制表示时“1”的个数
#include <bits/stdc++.h> using namespace std; int OneCount(unsigned int x) { int count; for(count=0;x>0;count++) { x&=x-1; } return count; } int main() { unsigned int a; cin>>a; int result; result=OneCount(a); cout<<result; cout<<endl; return 0; }4. 左旋字符串,例如abcdefg,左旋三位,defgabc,刚开始始终20%,懵逼了20分钟,后来发现cin输入的时候,读取不了空格,换为getline解决问题
#include <bits/stdc++.h> using namespace std; class Solution { public: string Left(string str,int n) { if(n<=0) { return str; } if(n>str.length()) { n=n%str.length(); } string res=""; for(int i=n;i<str.length();i++) { res+=str[i]; } for(int i=0;i<n;++i) { res+=str[i]; } return res; } }; int main() { string str; getline(cin,str); int k; cin>>k; string res; Solution s; res=s.Left(str,k); cout<<res; return 0; }
全部评论
(2) 回帖