金山WPS笔试题

本文介绍了一个实现奇偶校验的C语言函数,并提供了一段C++代码用于统计给定字符串中所有长度为3的子串出现次数,通过使用map进行统计并根据子串出现次数及字典序进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值