#include<stdio.h>
#include<string.h>
int fun(unsigned int v){
unsigned int t=v;
while(t!=0)
{
printf("%d",t%2);
t/=2;
}
printf(" ");
v^=v>>16;
v^=v>>8;
v^=v>>4;
v&=0xf;
printf("%d\n",(0x6996>>v)&1);
return (0x6996>>v)&1; //1代表校验字符
}
int main()
{
int val=0;
int t = 1<<4;
int i;
for(i=0;i<t;i++)
val+=fun(i);
printf("\n%d\n",val);
}
fun函数是奇偶校验
另一道模板嵌套的题目
给定一个字符串输出<3的子串的和个数,按个数降序排序,个数相同按字典序排序
使用map自动按字典序排序,然后手动按个数排序,很综合的题目。
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <string.h>
using namespace std;
int cmp(pair<string, int> a,pair<string,int> b){
return a.second>b.second;
}
void show(pair<string,int> a)
{
cout<<a.first<<" "<<a.second<<endl;
}
vector<pair<string,int> > calc3gram(string &s)
{
map<string, int> mp;
//..把数据插入到mp
//char temp[4];
for(int i =0;i<s.length();i++)
{
for(int j=0;j<3&&(i+j)<s.length();j++)
{
cout<<s.substr(i,j+1)<<endl;
//strncpy(temp, s.c_str()+i, j+1);
//temp[j] = '\n';
//cout<<temp<<endl;
mp[s.substr(i,j+1)]++;
}
}
vector<pair<string, int> > vt(mp.begin(),mp.end());
sort(vt.begin(),vt.end(),cmp);
for_each(vt.begin(),vt.end(),show);
return vt;
}
int main(int argc, const char * argv[]) {
//insert code here...
//int a=5,b,c,d
string str("abbcd");
calc3gram(str);
return 0;
}