判断是否为素数
int is_prime(int n){
if(n<=1) return 0; #判断n<=1的情况
int m=floor(sqrt(n) + 0.5); #对n开根号之后四舍五入
for(int i=2;i<=m;i++)
if(n % i == 0) return 0;
return 1;
}
字符串格式转换sprintf函数
sprintf(存储的字符串,”输出格式控制符”,要保存的对应格式数据);
c++需要指定头文件的输入输出流和命名空间后,才能使用cin等函数
#include<iostream>
using namespace std;
声明静态常量可以用
const
引用&,参数按传引用的方式传递,可以直接修改变量,代替c语言中的指针变量
#include <iostream>
using namespace std;
void swap2(int &a,int &b){
int t = a;
a = b;
b = t;
}
int main(){
int a=3,b=4;
swap2(a,b);
cout<<a<<b;
return 0;
}
字符串string:用getline函数读取一行数据,然后用这一行创建一个“字符串流”--ss,之后用cin读取ss的每一个被空格分隔的字符串。
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
int main(){
string line;
while(getline(cin,line)){
int sum = 0,x;
stringstream ss(line);
while(ss >> x) sum += x;
cout << sum;
}
return 0;
}
结构体struct:可以包含成员变量和成员函数,可以替代复杂的class
- 结构体Point中定义了一个Point函数,没有返回值,称为构造函数。构造函数在声明变量时调用,即Point a,b(1,2)调用了Point(1,2);
- :x(x),y(y)表示把成员变量x初始化成参数x,成员变量y初始化成参数y;x和y的默认值是0。
#include <iostream>
using namespace std;
struct Point{
int x,y;
Point(int x=0,int y=0):x(x),y(y){}
};
Point operator +(const Point &A,Point &B){
return Point(A.x+B.x,A.y+B.y);
}
ostream& operator <<(ostream &out,const Point &p){
cout<<"("<<p.x<<","<<p.y<<")";
return out;
}
int main(){
Point a,b(1,2);
a.x = 3;
cout<<a+b<<"\n";
return 0;
}
STL:cpp标准模板的使用
-
检索
lower_bound
功能:查找非递减序列内第一个大于或等于元素x的位置
返回值:如果找到元素x,返回该x的位置;否则返回数组a的长度n(可能会越界);
用法:int p = lower_bound(a,a+n,x) - a; -
排序
sort
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 10000;
int main(){
int n,q,x,a[maxn],kase = 0;
while(scanf("%d%d",&n,&q) == 2 && n){
printf("Case# %d:\n",++kase);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n) //按升序顺序对a数组从0-n范围排序
while(q--){
scanf("%d",&x);
int p = lower_bound(a,a+n,x) - a; //在已排序数组a中查找x
if(a[p] == x) printf("%d found at %d\n",x,p+1);
else printf("%d not found\n",x);
}
}
return 0;
}
- 不定长数组:vector
创建一维不定长数组:vector<T> a;可以创建T类型的向量数组a,T可以是内置类型或结构体;
创建二维数组,其中一维定长另一维不定长:vector<T> a[maxn];
添加元素:a.push_back(b);
删除元素:a.pop_back();
数组大小:a.size();
改变数组大小:a.resize(n+1);
判定是否为空:a.empty();
清空:a.clear();
- 集合:set
输入一个文本,找出所有不同的单词(连续的单词序列),按字典从小到大输出。单词不区分大小写。 样例输入: Adventures in Disneyland Two blondes were going to Disneyland when they came to a fork in the road. The sign read:"Disneyland Left."So they went home. 样例输出: a adventures blondes came disneyland fork going home in left read road sign so the they to two went were when
#include <cstdio> #include <iostream> #include <cstring> #include <set> #include <sstream> using namespace std; set<string> dict; //string集合 int main(){ string s,buf; while(cin>>s){ for(int i=0;i<s.length();i++){ if(isalpha(s[i])) s[i] = tolower(s[i]); else s[i] = ' '; } stringstream ss(s); while(ss >> buf) dict.insert(buf); } for(set<string>::iterator it = dict.begin();it!=dict.end();++it) cout<< *it << "\n"; return 0; }
- 映射:map
反片语 (Uva 156) 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排得到输入文本中的另外一个单词。输出时保留大小写,按字典序排列(所有大写字母在小写字母前面)。 输入样例: ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Rides dealer NotE derail LaCeS drIed noel dire Disk mace Rob dries # 输出样例: Disk NotE derail drIed eye ladder soon
#include <iostream> #include <string> #include <cctype> #include <map> #include <vector> #include <algorithm> using namespace std; map<string,int> cnt; vector<int> words; //将单词标准化 string repr(const string& s){ string ans = s; for(int i=0;i<ans.length();i++) ans[i] = tolower(ans[i]); sort(ans.begin(),ans.end()); return ans; } int main(){ int n=0; string s; while(cin>>s){ if(s[0] == '#') break; words.push_back(s); string r = repr(s); if(!cnt.count(r)) cnt[r] = 0; cnt[r]++; } vector<string> ans; for(int i=0;i<words.size();i++) if(cnt[repr(words[i])] == 1) ans.push_back(words[i]); sort(ans.begin(),ans.end()); for(int i=0;i<ans.size();i++) cout<<ans[i]<<"\n"; return 0; }