Problem A: 编写函数:输出时间 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 322 Solved: 230 [Submit][Status] Description 编写一个带默认值的函数showTime(),用于输出指定的时间,其中参数hour、minute和second分别是小时、分钟 、秒,输出格式为:hh:mm:ss。也就是说在输出时,小时、分钟和秒都要输出2位,如果不足2位,则在前面补0。 Input 输入三个整数,分别表示小时、分钟和秒。 Output 按照hh:mm:ss格式输出的三个时间(在输出时,小时、分钟和秒都要输出2位,如果不足2位,则在前面补0。): 第一行是使用默认分钟和秒的时间; 第二行是使用默认秒的时间; 第三行是输入值的时间。 Sample Input 12 1 1 Sample Output 12:00:00 12:01:00 12:01:01 HINT Append Code
时间: 2023-06-03 22:02:12 浏览: 112
抱歉,我可以回答这个问题。以下是代码实现:
#include <iostream>
#include <iomanip> // 用于按照2位输出
using namespace std;
void showTime(int hour, int minute=0, int second=0) { // 默认值分别为0
// 按照hh:mm:ss格式输出
cout << setfill('0') << setw(2) << hour << ":"
<< setfill('0') << setw(2) << minute << ":"
<< setfill('0') << setw(2) << second << endl;
}
int main() {
int hour, minute, second;
cin >> hour >> minute >> second;
showTime(hour); // 只输入小时,其他用默认值
showTime(hour, minute); // 输入小时和分钟,秒用默认值
showTime(hour, minute, second); // 全部输入
return 0;
}
相关问题
Problem B: 编写函数:输出数据类型 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 482 Solved: 239 [Submit][Status] Description 根据append.cc中的代码,编写一个能输出数据类型的函数printData()。 Input 输入3行,第一行是一个字母,第二行是一个整数,第三行是一个实数。 Output 输出有3行,分别于上述三行输入一一对应,具体格式见题目描述和样例。 Sample Input C 3 3.4 Sample Output C is a character. 3 is an integer. 3.4 is a dobule. HINT Append Code append.cc,
以下是符合要求的程序:
#include <iostream>
using namespace std;
void printData(char c, int i, double d) {
cout << c << " is a character." << endl;
cout << i << " is an integer." << endl;
cout << d << " is a double." << endl;
}
int main() {
char c;
int i;
double d;
cin >> c >> i >> d;
printData(c, i, d);
return 0;
}
Problem H: 求个最大值 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 2838 Solved: 2340 [Submit][Status] Description 定义MaxValue类,用于求一系列非零整数的最大值。其中: 1. 数据成员elements用于存储所有输入的非零整数。 2. void append(int)用于向elements中添加一个新数据。 3. int getMax()用于求出elements中的最大值。 Input 输入若干个整数,以输入0表示输入结束。 Output 所有输入的非零整数中的最大值。 Sample Input 321 496 553 338 837 463 158 154 929 537 0 Sample Output 929 HINT 使用vector更为容易实现。
以下是该问题的C++解决方案,使用vector实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
class MaxValue {
public:
void append(int num) {
elements.push_back(num);
}
int getMax() {
int max_val = elements[0];
for(int i = 1; i < elements.size(); i++) {
if(elements[i] > max_val) {
max_val = elements[i];
}
}
return max_val;
}
private:
vector<int> elements;
};
int main() {
MaxValue maxval;
int num;
cin >> num;
while(num != 0) {
maxval.append(num);
cin >> num;
}
cout << maxval.getMax() << endl;
return 0;
}
```
该程序中,我们创建了一个名为MaxValue的类,其中包含了一个vector类型的数据成员elements,用于存储输入的非零整数。类中的append方法用于向elements中添加新数据,getMax方法用于求出elements中的最大值。在主函数中,我们创建了一个MaxValue类型的对象maxval,并通过while循环不断输入非零整数并添加到maxval对象的elements中。最终输出elements中的最大值即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)