1021 C++

代码实现: 

#include <iostream>
using namespace std;
int main() {
	string s;
	cin>>s;
	int a[10];
	for(int i=0; i<10; i++)
		a[i]=0;
	for(int i=0; i<s.length(); i++) {
		a[s[i]-'0']++;
	}
	for(int i=0; i<10; i++) {
		if(a[i]>0)
		cout<<i<<":"<<a[i]<<endl;
	}
	return 0;
}

问题与收获:

发现有好多题都可以用到这个点啊

e.g:string一个变量s,for循环的时候可以用到s.length来定范围,还有s[i]<'0',s[i]<'A'等等语句可以用来判断检验(题目要求)。

CRC (循环冗余校验) 是一种用于检测数据传输错误的技术。它通过生成一个特定长度的值(通常称为“校验和”),该值取决于输入的数据,并将其附加到发送的消息上。接收端可以重新计算接收到消息的 CRC 并与传送过来的比较,如果两者匹配,则认为数据无误。 对于使用多项式 `0x1021` 实现的 CRC 校验,在 C++ 中可以通过如下步骤完成: ### 以下是基于字节处理的一个简单的例子: ```cpp #include <iostream> using namespace std; unsigned short crc16(const unsigned char *data_p, unsigned int length) { unsigned short crc = 0xFFFF; for(unsigned int i=0; i<length;i++) { crc ^= data_p[i]; // XOR byte into least sig. byte of crc for(int j = 8; j != 0;j--) { // Do eight times. if((crc & 0x0001)) { // If the LSB is set, crc >>= 1; // Shift right and XOR 0xA001. crc ^= 0xA001; } else { // Else LSB is not set, crc >>= 1; // So Just shift right. } } } return crc; } int main() { string input = "Hello World!"; cout << "原始字符串:" << input << endl; const unsigned char* inputData = reinterpret_cast<const unsigned char*>(input.c_str()); unsigned short result_crc = crc16(inputData , input.size()); printf("CRC16 结果(十六进制): %X\n",result_crc); return 0; } ``` 此程序实现了给定文本"Hello World!" 的 CRC-16 计算过程,所使用的多项式为 `0x1021`, 起始值设为了 `0xFFFF`. 这里需要注意的是实际应用中需要确认起始值、是否对结果进行反向操作等细节,因为不同的应用场景可能会有不同的约定.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值