(WUST_OJ) 1012: 译码

Description

要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。因此,"China"应译为"Glmre"。请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。

Input

China

Output

加密后的China

Sample Input

 

China

 

Sample Output

Glmre

 

HINT

 

so easy


#include<stdio.h>
int main()
{
	char c1='C',c2='h',c3='i',c4='n',c5='a';
	printf("%c%c%c%c%c",c1+4,c2+4,c3+4,c4+4,c5+4);
	return 0;
 } 

 

### C++ 时间类重载的实现与示例代码 在C++中,可以通过重载运算符来实现自定义类的行为。对于时间类(例如表示小时、分钟和秒的时间对象),可以重载常见的运算符(如`+`、`-`、`<<`等)以支持对时间对象的操作[^2]。以下是一个适用于WUST OJ的时间类重载的示例代码。 ```cpp #include <iostream> using namespace std; class Time { private: int hours; int minutes; int seconds; public: // 构造函数 Time(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {} // 重载加法运算符 Time operator+(const Time& t) const { int totalSecs = this->toSeconds() + t.toSeconds(); return Time::fromSeconds(totalSecs); } // 重载减法运算符 Time operator-(const Time& t) const { int totalSecs = this->toSeconds() - t.toSeconds(); if (totalSecs < 0) totalSecs = 0; // 防止负值 return Time::fromSeconds(totalSecs); } // 重载输出流运算符 friend ostream& operator<<(ostream& os, const Time& t); // 辅助方法:将时间转换为总秒数 int toSeconds() const { return hours * 3600 + minutes * 60 + seconds; } // 辅助方法:从总秒数创建时间对象 static Time fromSeconds(int totalSecs) { int h = totalSecs / 3600; int m = (totalSecs % 3600) / 60; int s = totalSecs % 60; return Time(h, m, s); } }; // 实现输出流运算符重载 ostream& operator<<(ostream& os, const Time& t) { os << t.hours << "小时 " << t.minutes << "分钟 " << t.seconds << "秒"; return os; } int main() { Time t1(1, 30, 45); // 1小时30分45秒 Time t2(2, 15, 20); // 2小时15分20秒 Time sum = t1 + t2; cout << "t1 + t2 = " << sum << endl; Time diff = t1 - t2; cout << "t1 - t2 = " << diff << endl; return 0; } ``` #### 代码说明 1. **构造函数**:初始化时间对象的小时、分钟和秒。 2. **运算符重载**: - `operator+`:通过将两个时间对象转换为总秒数相加,然后重新转换为时间格式。 - `operator-`:类似加法,但需确保结果非负。 - `operator<<`:重载输出流运算符,以便可以直接使用`cout`打印时间对象[^3]。 3. **辅助方法**: - `toSeconds`:将时间对象转换为总秒数。 - `fromSeconds`:将总秒数转换回时间对象。 #### 注意事项 - 在实现时间类时,需注意溢出问题(如秒数超过60或分钟数超过60的情况),通常通过将所有时间单位统一转换为秒来简化计算[^4]。 - 对于WUST OJ等在线评测系统,确保代码符合其输入输出格式要求,并避免不必要的头文件引入。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值