323
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include<cmath>
using namespace std;
int main() {
int num1, num2,h,m,s;
cin >> num1 >> num2;
int diff = num2 - num1;
int data = round(diff / 100.0);
h = data / 3600;
m = data % 3600 / 60;
s = data % 3600 % 60;
printf("%02d:%02d:%02d", h, m,s);
}
求数列前n项和
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int num;
char sym;
cin >> num >> sym;
int n = 0;
while (num > 2 * pow(n+1, 2) + 1) {//手动计算
n++;
}
int count = n;
for (int i = 0; i < 2 * n +1; i++) {
string output(2*abs(count)+1, sym);
string space(n-abs(count), ' ');
output = space + output;
cout << output<<endl;
count--;
}
cout << num- 2 * pow(n + 1, 2) + 1<<endl;
}
字符串比较更快捷
#include<iostream>
#include<string>
using namespace std;
int main() {
int N;
cin >> N;
string max[2], min[2];
max[1] = "0000/00/00";
min[1] = "9999/99/99";
int count=0;
for (int i = 0; i < N; i++) {
string name, birth;
cin >> name >> birth;
if ("2014/09/06" >= birth && birth >= "1814/09/06") {
if (birth > max[1]) {
max[0] = name;
max[1] = birth;
}
if (birth < min[1]) {
min[0] = name;
min[1] = birth;
}
count++;
}
}
cout << count << " " << min[0] << " " << max[0];
}
字符串相减
#include<iostream>
#include<string>
using namespace std;
int main() {
string a;
string b;
getline(cin, a);
getline(cin, b);
for (int i = 0; i < b.size(); i++) {
for (size_t j = 0; j < a.size();) {
if (a[j] == b[i]) {
a.erase(j, 1);
}
else {
j++;
}
}
}
string result;
for (size_t i = 0; i < a.size(); i++) {
a[i] = toupper(a[i]);
if (result.find(a[i]) == string::npos) {
result.push_back(a[i]);
}
}
cout << result;
}