目录
1073 Scientific Notation(16/20)
1001 A+B Format
注意每三位数字出现一个逗号是从尾端开始向前计数。
#include<bits/stdc++.h>
using namespace std;
void getSum(int a, int b) {
string str = to_string(a + b);
for (int i = 0; i < str.size(); i++) {
cout<<str[i];
if (str[i] == '-') continue;
if (i != str.size() - 1 && (i + 1) % 3 == str.size() % 3) cout<<",";
}
return ;
}
int main() {
int a, b;
cin>>a>>b;
getSum(a, b);
return 0;
}
1005 Spell It Right
注意题目的输入和输出都是字符串
#include<bits/stdc++.h>
using namespace std;
vector<string> vec({"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"});
void getSum(string n) {
int sum = 0;
for (int i = 0; i < n.size(); i++) sum += (n[i] - '0');
string str = to_string(sum);
for (int i = 0; i < str.size(); ++i) {
cout<<vec[str[i] - '0'];
if (i != str.size() - 1) cout<<" ";
}
}
int main() {
string num;
cin>>num;
getSum(num);
return 0;
}
1035 Password
注意从测试用例找到突破口,这道题每行包括两个字符串,每一个都要cin
#include<bits/stdc++.h>
using namespace std;
vector<string> ans;
// 若发生修改则存储字符串
void checkString(string xuhao, string n) {
bool flag = false;
for (int i = 0; i < n.size(); ++i) {
if (n[i] == '1') {n[i] = '@'; flag = true;}
else if (n[i] == '0') {n[i] = '%'; flag = true;}
else if (n[i] == 'l') {n[i] = 'L'; flag = true;}
else if (n[i] == 'O') {n[i] = 'o'; flag = true;}
else continue;
}
if (flag) ans.push_back(xuhao + " " + n);
}
int main() {
int N;
string str, xuhao; // 这里一共输入了两个字符串,注意从测试用例中找到突破口
cin>>N;
for (int i = 0; i < N; ++i) {
cin>>xuhao>>str;
checkString(xuhao, str);
}
if (ans.size() == 0) {
if (N == 1) cout<<"There is 1 account and no account is modified"<<endl;
else cout<<"There are "<<N<<" accounts and no account is modified"<<endl;
}
else {
cout<<ans.size()<<endl;
for (auto it : ans) cout<<it<<endl;
}
return 0;
}
1061 Dating(18/20)
没完全跑通。。差两分,今天搞不动了,回头再说
#include<bits/stdc++.h>
using namespace std;
vector<int> tranTime(string str1, string str2, string str3, string str4) {
vector<int> res;
bool flag = false;
int n1 = min(str1.size(), str2.size()), n2 = min(str3.size(), str4.size());
for (int i = 0; i < n1; ++i) {
// cout<<flag<<endl;
// 记录周几
if (flag == false && str1[i] >= 'A' && str1[i] <= 'G' && str1[i] == str2[i]) {
res.emplace_back(str1[i] - 'A');
flag = true; // 开始判断小时
continue;
}
// 记录小时
if (flag && ((str2[i] >= 'A' && str2[i] <= 'N') || (str1[i] >= '0' && str1[i] <= '9')) && str1[i] == str2[i]) {
if (str2[i] >= 'A' && str2[i] <= 'N') res.emplace_back(str2[i] - 'A' + 10);
else res.emplace_back(str2[i] - '0');
}
}
for (int i = 0; i < n2; ++i) {
// 记录分钟
if (str3[i] == str4[i] && isalpha(str3[i])) {
res.emplace_back(i);
break; // Assuming that there's only one matching minute character
}
}
return res;
}
vector<string> date({"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"});
int main() {
string str1, str2, str3, str4;
cin>>str1>>str2>>str3>>str4;
vector<int> nums = tranTime(str1, str2, str3, str4);
// 打印日期
cout<<date[nums[0]]<<" ";
printf("%02d:%02d", nums[1], nums[2]);
return 0;
}
1073 Scientific Notation(16/20)
没完全跑通
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
if (s[0] == '-') cout<<"-";
int len = s.size();
// 截取E之后的三位ss
string ss = s.substr(len - 3);
string sss = ss.substr(1);
// weishu代表小数点要移动的位数
int weishu = stoi(sss);
// 截取E之前的不包括正负号的部分
string str = s.substr(1, len - 5);
// 小数点向后移动
if (ss[0] == '+') {
// 该情况下不用输出小数点
if (weishu >= str.size() - 2) {
for (char c : str) {
if (c == '.') continue;
cout<<c;
}
for (int i = 0; i < weishu - str.size() + 2; ++i) cout<<"0";
}
// 该情况下还是要输出小数点
else {
for (int i = 0; i < weishu + 2; ++i) {
if (str[i] == '.') continue;
cout<<str[i];
}
cout<<".";
for (int i = weishu + 2; i < str.size(); ++i) cout<<str[i];
}
}
else {
cout<<"0.";
for (int i = 1; i < weishu; ++i) cout<<"0";
for (char c : str) {
if (c == '.') continue;
cout<<c;
}
}
return 0;
}
1077 Kuchiguse(17/20)
学会了输入一整行带有空格字符串的办法get.cin() + getline(cin, str)
#include<bits/stdc++.h>
using namespace std;
void getCom(string &s1, string &s2) {
int len = min(s1.size(), s2.size());
int i;
for (i = 0; i < len; i++) {
if (s1[i] != s2[i]) break;
}
s1 = s1.substr(0, i);
return ;
}
int main() {
int n;
// cin>>n;
scanf("%d\n", &n);
string s, str;
// 输入一整行带空格字符串的方法
// cin.get();
getline(cin, s);
reverse(s.begin(), s.end());
n--;
while (n--) {
cin.get();
getline(cin, str);
reverse(str.begin(), str.end());
getCom(s, str);
}
if (s == "") cout<<"nai"<<endl;
else {
reverse(s.begin(), s.end());
cout<<s<<endl;
}
return 0;
}