HDU 1200 字符串

不断补充:

1.strlen()用法
strlen()用于返回字符数组或字符串所占的字节数。该函数是从该字符串的第一个地址开始遍历,直到遇到结束符’\0’为止。返回的长度大小不包括’\0’。
c/c++ strlen(str)和str.length()和str.size()都可以求字符串长度。
2.reverse()用法
reverse可以用于vector、string类型的变量,使用方法:

string s;
reverse(s.begin(),s.end());  //s已经是反转后的结果

对于字符数组,也可以使用reverse翻转某一段,用法如下:

char st[MAXN];
cin>>st;
reverse(st+c*i, st+c*(i+1));  //翻转某一段
例题:HDU 1200

重点是思路,AC代码:

#include <iostream>
#include <string.h>
#include <algorithm>
#include <string>
using namespace std;
const int MAXN = 205;
//HDU	Accepted	1200	0MS	1396K	479 B	G++
char st[MAXN];
int main() {
	int c;
	while(cin>>c && c) {
		cin>>st;
		int len = strlen(st);
		int r = len / c;
		
		for(int i=0; i<r; i++) {
			if(i&1) {
				//奇数,翻转回去
				reverse(st+c*i, st+c*(i+1));
			} 
		}
//		cout<<st<<endl;
		for(int i=0; i<c; i++) {
			for(int j=0; j<r; j++) {
				cout<<st[i+j*c];
			}
		}		
		cout<<endl;
	} 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值