TopCoder竞赛:C++, STL 用法快速入门(转)& 输入输出

********************************

下面总结了一些题目中常用的STL库的用法。


#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <iostream>
 
using namespace std;
 
//递归
int GetN(int n)
{
if (n==1) return 1;
else return GetN(n-1);
}
 
void TestSTL_main( int argc, char* argv[] )
//void main( int argc, char* argv[] )
{
/******** STL **********/
 
//string的用法
{
string s = "mmmmm";
string s2("ss22");
s2.insert(2,"kkkkk"); //把"kkkkk"插到s2的第2个位置之前(位置从0开始)
s2+=s+"44444"+'c';
const char *pc = s.c_str();//把string转成C-style的string,以/0终了
const char *ptr1 = s.data();;//把string转成字符串
if (s2[2] == 'k') s2[2]='C';
s+="jkl";
s+='m';
s.push_back('/n'); //把'/n'(换行符)放在s的最后一个位置
reverse(s.begin(), s.end()); //反转
basic_string <char>::iterator str_Iter; //遍历
str_Iter = s.begin();
}
 
//vector的用法
{
vector<int> v;
v.push_back(8); //向v中插入元素,元素的值是8
int iLen = (int)v.size();
for(int i=0;i<iLen;i++)
{
int k = v[0]; //k==8
}
}
 
//map的用法
{
map<int, int> mp;
for(int i=0;i<3;i++)
{
mp[i]=i*2; //通过[第一个元素]来访问第二个元素
}
 
int total = 100;
map<int, int>::iterator it = mp.begin();
for(;it!=mp.end();it++) //遍历mp
{
total+=it->second; //通过iterator it来访问第二个元素
}
cout<<"total="<<total<<endl;
}
 
//算法
int n = GetN(5); //递归n!=n*(n-1)*(n-2)*…*1
int aa=10,bb=15;
int maxi = max(aa,bb); //最大值
int mini = min(aa,bb); //最小值
int absi = abs(-12); //绝对值
vector<string> v;
v.push_back("hello");
v.push_back("123");
v.push_back("no");
sort(v.begin(),v.end()); //按照字母顺序,把v里面的元素排序
int savei;
sscanf(v[0].c_str(), "%d", &savei); //把字符串“123”转换成数字123
cout<<"savei="<<savei<<endl;
char buf[100];
sprintf(buf,"v[1]=%d",savei); //把内容打印进字符串
cout<<"buf="<<buf<<endl;
 
}

********************************************************

TC的输入输出跟一般oj有区别的!要记得转变过来

SRM的输入输出
SRM是不用标准或文件输入和输出的,只要写一个类的一个成员函数。也就是说,你需要编写的并不是一个完整的程序,而是一个类。
输入是成员函数的参数,输出用return,所以经常需要STL中的vector和string。
因为TC的系统并不测试标准输出,所以标准输出可以当调试用。
下面以SRM 413 Div 2的1000分题目介绍程序的写法。
题目如下(选择不同的语言,题目描述会略有不同,本文以C++为例):

Problem Statement
NOTE: This problem statement contains subscripts that may not display properly if viewed outside of the applet.

Let's consider an infinite sequence A defined as follows:
A0 = 1;
Ai = A[i/p] + A[i/q] for all i >= 1, where [x] denotes the floor function of x. (see Notes)
You will be given n, p and q. Return the n-th element of A (index is 0-based).

Definition
Class:
InfiniteSequence
Method:
calc
Parameters:
long long, int, int
Returns:
long long
Method signature:
long long calc(long long n, int p, int q)
(be sure your method is public)

Notes
- [x] denotes the floor function of x which returns the highest integer less than or equal to x. For example, [3.4] = 3, [0.6] = 0.

Constraints
- n will be between 0 and 10^12, inclusive.
- p and q will both be between 2 and 10^9, inclusive.

Examples
0)
0
2
3
Returns: 1

A[0] = 1.

1)
7
2
3
Returns: 7

A[0] = 1; A[1] = A[0] + A[0] = 2; A[2] = A[1] + A[0] = 2 + 1 = 3; A[3] = A[2] + A[1] = 3 + 2 = 5; A[7] = A[3] + A[2] = 5 + 3 = 8.

2)
10000000
3
3
Returns: 32768

3)
256
2
4
Returns: 89

4)
1
1000000
1000000
Returns: 2

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

下面是一个错误算法的程序,仅供参考格式用:

#include <string>
#include <vector>
class InfiniteSequence {
public:
long long calc(long long n, int p, int q) {
if (n == 0)
return 1;
else
return calc(n/p, p, q) + calc(n/q, p, q);
}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值