L1-031
思路:
按题意判断即可
代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while(n--){
double h, w;
cin >> h >> w;
double ww = (h - 100) * 1.8;
if(abs(ww - w) < ww * 0.1) puts("You are wan mei!");
else if(ww - w >= ww * 0.1) puts("You are tai shou le!");
else puts("You are tai pang le!");
}
return 0;
}
L1-032
思路:
如果长度超过了需要减去前面的字符串
代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
char c;
string s;
cin >> n >> c;
getchar();
getline(cin, s);
reverse(s.begin(), s.end());
if(n <= s.length()) s = s.substr(0, n);
int k = n - s.length();
while(k--) putchar(c);
reverse(s.begin(), s.end());
cout << s;
return 0;
}
L1-033
思路:
写个计算最多不重复数字个数的函数,然后依次遍历即可
代码:
#include<bits/stdc++.h>
using namespace std;
int cal(int x) {
unordered_map<int, bool> mp;
int ans = 0;
if(x < 1000) mp[0] = true, ++ans;
while(x) {
if(mp[x % 10] == false) ++ans, mp[x % 10] = true;
x /= 10;
}
return ans;
}
int main() {
int y, n;
cin >> y >> n;
for(int i = 0; ; i++){
if(cal(y + i) == n){
printf("%d %04d", i, y + i);
break;
}
}
return 0;
}
L1-034
思路:
用pair存储编号和次数,然后自定义排序规则即可
代码:
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define fi first
#define sc second
const int maxn = 1005;
P arr[maxn];
bool cmp(P & a, P & b){
return a.sc == b.sc ? a.fi > b.fi : a.sc > b.sc;
}
int main() {
int n, k, f;
cin >> n;
for(int i = 0; i < maxn; i++){
arr[i].fi = i;
arr[i].sc = 0;
}
for(int i = 0; i < n; i++){
cin >> k;
while(k--){
cin >> f;
++arr[f].sc;
}
}
sort(arr, arr + maxn, cmp);
cout << arr[0].fi << ' ' << arr[0].sc;
return 0;
}
L1-035
思路:
统计输入进来的字符串个数即可
代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
int ans = 0;
string a, b, s;
while(cin >> s){
if(s == ".") break;
++ans;
if(ans == 2) a = s;
else if(ans == 14) b = s;
}
if(ans < 2) cout << "Momo... No one is for you ...";
else if(ans < 14) cout << a + " is the only one for you...";
else cout << a + " and " + b + " are inviting you to dinner...";
return 0;
}