problemdetail.do?&method=showdetail&id=5131

本文介绍了一种用于生成校验码的算法,包括算术极数法、几何极数法和质数法三种方法,旨在确保数据输入的准确性。文章详细解释了不同校验码的计算过程,并提供了一个具体的实现案例。

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

toj5131:校验码

描述
校验码(jiào yàn mǎ)通常是一组数字的最后一位,由前面的数字通过某种运算得出,用以检验该组数字的正确性。常见的校验码有中华人民共和国居民身份证的最后一位、ISBN号码的最后一位、组织机构代码的最后一位、数据传输的正确性验证码等。
代码作为数据在向计算机或其它设备进行输入时,容易产生输入错误,为了减少输入错误,编码专家发明了各种校验检错方法,并依据这些方法设置了校验码。
算术极数法(Arithmetic method)
数字12345 每位对应极数5 4 3 2 1 所以校验码:15+24+33+42+51=35%11=2;
几何极数法(Geometric method)
数字12345 对应极数 16 8 4 2 1所以校验码:1
16+28+34+42+51=57%11=2;
质数法(Prime number method)
数字12345 对应极数11 7 5 3 2 所以校验码:111+27+35+43+5*2=62%11=7;
输入
输入数据有T组(T<100)
对于每组数据:
第一行输入一个数字(长度不超过10000)
第二行输入一段字符串,字符串有三种:第一种‘Arithmetic’,二种‘Geometric’,第三种‘Prime’
输出
对于每组数据输出一个校验码,当校验码为10时输出‘X’;
样例输入
3
12345
Arithmetic
12345
Geometric
12345
Prime
样例输出
2
2
7

//
#include<stdio.h>
#include<string.h>
int d[110000];
int main()
{
	int a[10005],b[10005],c[10005];
	b[1]=1;a[1]=1;
	int i,j;
	for(i=2;i<=10000;i++)
	{
		a[i]=i%11;
		b[i]=b[i-1]*2%11;
	}
	memset(d,0,sizeof(d));
	int k=1;
	for(i=2;;i++)
	{
		if(d[i])
		continue;
		
		if(k>10000)
		break;
		c[k++]=i%11;
		for(j=i*2;j<=110000;j+=i)
		d[j]=1;
	}
	int t;
	char s[10005];
	char ss[1000];
	scanf("%d",&t); 
	while(t--)
	{
		int sum=0;
		scanf("%s%s",s,ss);
		int l=strlen(s);//这里要注意,如果循环中直接用strlen的话会超时
		if(strcmp(ss,"Arithmetic")==0)
		{
			for(i=0;i<l;i++)
			{
				sum+=(s[i]-'0')*a[l-i];
				sum%=11;
			}
		}
		else if(strcmp(ss,"Geometric")==0)
		{
			for(i=0;i<l;i++)
			{
				sum+=(s[i]-'0')*b[l-i];
				sum%=11;
			}
		}
		else
		{
			for(i=0;i<l;i++)
			{
				sum+=(s[i]-'0')*c[l-i];
				sum%=11;
			}
		}
		if(sum==10)
		printf("X\n");
		else
		printf("%d\n",sum);
	}
}
import { BusinessError } from '@kit.BasicServicesKit'; import { http } from '@kit.NetworkKit'; import { xml, util } from '@kit.ArkTS'; // 定义文章数据接口 interface Article { id: string; title: string; source: string; time: string; category: string; isCollected: boolean; image?: string; content?: string; } @Entry @Component struct ITHomeApp { @State currentTab: 'home' | 'favorite' = 'home'; @State showDetail: boolean = false; @State currentArticle: Article | null = null; @State articles: Article[] = []; @State isLoading: boolean = true; private currentItem: Article | null = null; private inItem: boolean = false; // 解析日期格式 private formatDate(dateString: string): string { try { const date = new Date(dateString); return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`; } catch (e) { return dateString; } } // 从描述中提取图片 private extractImage(description: string): string | undefined { const imgRegex = /<img[^>]+src="([^">]+)"/; const match = description.match(imgRegex); return match ? match[1] : undefined; } // 从描述中提取纯文本内容 private extractContent(description: string): string { return description.replace(/<[^>]+>/g, '').replace(/\s+/g, ' ').trim(); } aboutToAppear() { this.fetchRSSData(); } // 获取RSS数据 fetchRSSData() { this.isLoading = true; const httpRequest = http.createHttp(); httpRequest.request( "https://www.ithome.com/rss/", { method: http.RequestMethod.GET }, (err: BusinessError, data: http.HttpResponse) => { if (err) { console.error(`Request failed, code: ${err.code}, message: ${err.message}`); this.isLoading = false; return; } if (data.responseCode !== http.ResponseCode.OK) { console.error(`Response error, code: ${data.responseCode}`); this.isLoading = false; return; } const textEncoder = new util.TextEncoder(); const buffer = textEncoder.encode(data.result as string); this.parseRSSData(buffer.buffer); } ); } // 标签值回调处理 private handleTagValue(name: string, value: string) { switch (name) { case 'item': if (this.inItem && this.currentItem) { this.articles.push(this.currentItem); } this.currentItem = { id: '', title: '', source: 'IT之家', time: '', category: '', isCollected: false, content: '' }; this.inItem = true; break; case 'title': if (this.inItem && this.currentItem) { this.currentItem.title = value; } break; case 'link': if (this.inItem && this.currentItem) { this.currentItem.id = value; } break; case 'pubDate': if (this.inItem && this.currentItem) { this.currentItem.time = this.formatDate(value); } break; case 'category': if (this.inItem && this.currentItem) { this.currentItem.category = value; } break; case 'description': if (this.inItem && this.currentItem) { this.currentItem.image = this.extractImage(value); this.currentItem.content = this.extractContent(value); } break; case 'source': if (this.inItem && this.currentItem && value) { this.currentItem.source = value.replace('来源:', ''); } break; } } // 解析RSS数据 parseRSSData(buffer: ArrayBuffer) { try { this.articles = []; this.currentItem = null; this.inItem = false; const parser = new xml.XmlPullParser(buffer, 'UTF-8'); // 修复1: 使用显式类型定义回调函数 const callback: (name: string, value: string) => boolean = (name: string, value: string) => { this.handleTagValue(name, value); return true; // 必须返回布尔值 }; // 修复2: 创建符合ParseOptions接口的对象 const options: xml.ParseOptions = { supportDoctype: true, ignoreNamespace: true, tagValueCallbackFunction: callback }; parser.parse(options); if (this.inItem && this.currentItem) { this.articles.push(this.currentItem); } this.isLoading = false; } catch (e) { console.error('RSS解析错误: ' + JSON.stringify(e)); this.isLoading = false; } } // 切换标签页 private changeTab(tab: 'home' | 'favorite') { this.currentTab = tab; } // 显示文章详情 private showArticleDetail(article: Article) { this.currentArticle = article; this.showDetail = true; } // 关闭详情页 private closeDetail() { this.showDetail = false; } // 切换收藏状态 private toggleCollect(article: Article) { const index = this.articles.findIndex(a => a.id === article.id); if (index !== -1) { this.articles[index].isCollected = !this.articles[index].isCollected; this.articles = [...this.articles]; } } build() { Stack() { Column() { this.buildHeader() this.buildTabBar() this.buildContent() } .width('100%') .height('100%') .backgroundColor('#F5F5F5') .visibility(this.showDetail ? Visibility.None : Visibility.Visible) if (this.showDetail && this.currentArticle) { this.buildArticleDetail(this.currentArticle) } if (this.isLoading) { Column() { Progress() .width(60) .height(60) } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) .backgroundColor('rgba(0,0,0,0.2)') } } } @Builder buildHeader() { Row() { Text('IT之家') .fontSize(24) .fontWeight(FontWeight.Bold) .fontColor('#FF6700') Blank() Image($r('app.media.ic_public_search')) .width(24) .height(24) .margin({ right: 16 }) .onClick(() => { this.fetchRSSData(); }) Image($r('app.media.ic_public_user')) .width(24) .height(24) } .padding({ left: 16, right: 16, top: 12, bottom: 12 }) .width('100%') .backgroundColor('#FFFFFF') .justifyContent(FlexAlign.SpaceBetween) } @Builder buildTabBar() { Row() { Column() { Text('首页') .fontSize(18) .fontColor(this.currentTab === 'home' ? '#FF6700' : '#666666') .fontWeight(this.currentTab === 'home' ? FontWeight.Bold : FontWeight.Normal) if (this.currentTab === 'home') { Divider() .width(24) .height(3) .color('#FF6700') } } .height(56) .justifyContent(FlexAlign.Center) .onClick(() => this.changeTab('home')) .layoutWeight(1) Column() { Text('收藏') .fontSize(18) .fontColor(this.currentTab === 'favorite' ? '#FF6700' : '#666666') .fontWeight(this.currentTab === 'favorite' ? FontWeight.Bold : FontWeight.Normal) if (this.currentTab === 'favorite') { Divider() .width(24) .height(3) .color('#FF6700') } } .height(56) .justifyContent(FlexAlign.Center) .onClick(() => this.changeTab('favorite')) .layoutWeight(1) } .width('100%') .borderWidth({ bottom: 1 }) .borderColor({ bottom: '#EEEEEE' }) .backgroundColor('#FFFFFF') } @Builder buildContent() { Column() { if (this.articles.length === 0 && !this.isLoading) { Column() { Image($r('app.media.ic_public_refresh')) .width(60) .height(60) .margin({ bottom: 20 }) Text('暂无数据,点击刷新') .fontSize(16) .fontColor('#666666') } .width('100%') .height('100%') .justifyContent(FlexAlign.Center) .onClick(() => this.fetchRSSData()) } else if (this.currentTab === 'home') { List({ space: 8 }) { ForEach(this.articles, (article: Article) => { ListItem() { this.buildArticleItem(article) } .onClick(() => { this.showArticleDetail(article); }) }, (article: Article) => article.id) } .width('100%') .height('100%') .layoutWeight(1) } else { List({ space: 8 }) { ForEach(this.articles.filter(article => article.isCollected), (article: Article) => { ListItem() { this.buildArticleItem(article) } .onClick(() => { this.showArticleDetail(article); }) }, (article: Article) => article.id) } .width('100%') .height('100%') .layoutWeight(1) } } .layoutWeight(1) } @Builder buildArticleItem(article: Article) { Column() { Row() { if (article.image) { Image(article.image) .width(120) .height(80) .objectFit(ImageFit.Cover) .borderRadius(4) .margin({ right: 12 }) } else { Image($r('app.media.ic_public_picture')) .width(120) .height(80) .objectFit(ImageFit.Cover) .borderRadius(4) .margin({ right: 12 }) .backgroundColor('#EAEAEA') } Column() { Text(article.title) .fontSize(16) .fontColor('#333333') .fontWeight(FontWeight.Medium) .maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis }) .margin({ bottom: 8 }) Row() { Text(article.source) .fontSize(12) .fontColor('#999999') Text(article.time) .fontSize(12) .fontColor('#999999') .margin({ left: 12 }) Blank() // 修复3: 添加事件参数并返回true阻止冒泡 Image(article.isCollected ? $r('app.media.ic_public_favorite') : $r('app.media.ic_public_favorite_filled')) .width(20) .height(20) .onClick(() => { this.toggleCollect(article); return true; // 阻止事件冒泡 }) } .width('100%') .alignItems(VerticalAlign.Center) } .layoutWeight(1) } .padding(12) .width('100%') .backgroundColor('#FFFFFF') .borderRadius(8) Divider() .color('#EEEEEE') .margin({ left: 12, right: 12 }) } } @Builder buildArticleDetail(article: Article) { Column() { Row() { Image($r('app.media.ic_public_back')) .width(24) .height(24) .onClick(() => this.closeDetail()) Text('文章详情') .fontSize(18) .fontWeight(FontWeight.Bold) .margin({ left: 12 }) Blank() Image(article.isCollected ? $r('app.media.ic_public_favorite_filled') : $r('app.media.ic_public_favorite')) .width(24) .height(24) .onClick(() => this.toggleCollect(article)) } .padding({ left: 16, right: 16, top: 12, bottom: 12 }) .width('100%') .backgroundColor('#FFFFFF') Scroll() { Column() { Text(article.title) .fontSize(22) .fontWeight(FontWeight.Bold) .margin({ top: 16, bottom: 12, left: 16, right: 16 }) Row() { Text(article.source) .fontSize(14) .fontColor('#666666') Text(article.time) .fontSize(14) .fontColor('#666666') .margin({ left: 12 }) } .margin({ bottom: 16, left: 16, right: 16 }) if (article.image) { Image(article.image) .width('100%') .height(200) .objectFit(ImageFit.Cover) .margin({ bottom: 16 }) } Text(article.content || '') .fontSize(16) .lineHeight(24) .margin({ left: 16, right: 16, bottom: 24 }) Text('相关推荐') .fontSize(18) .fontWeight(FontWeight.Bold) .margin({ left: 16, bottom: 12, top: 16 }) ForEach( this.articles.filter(a => a.id !== article.id && a.category === article.category).slice(0, 3), (item: Article) => { Column() { Row() { if (item.image) { Image(item.image) .width(80) .height(60) .objectFit(ImageFit.Cover) .borderRadius(4) .margin({ right: 12 }) } else { Image($r('app.media.ic_public_picture')) .width(80) .height(60) .objectFit(ImageFit.Cover) .borderRadius(4) .margin({ right: 12 }) .backgroundColor('#EAEAEA') } Column() { Text(item.title) .fontSize(14) .fontWeight(FontWeight.Medium) .maxLines(2) .textOverflow({ overflow: TextOverflow.Ellipsis }) .margin({ bottom: 4 }) Row() { Text(item.source) .fontSize(12) .fontColor('#999999') Text(item.time) .fontSize(12) .fontColor('#999999') .margin({ left: 8 }) } } .layoutWeight(1) } .padding(12) .width('100%') Divider() .color('#EEEEEE') .margin({ left: 12, right: 12 }) } .onClick(() => this.showArticleDetail(item)) }, (item: Article) => item.id ) } } .layoutWeight(1) .backgroundColor('#FFFFFF') } .width('100%') .height('100%') .backgroundColor('#FFFFFF') } }为什么会出现Structural typing is not supported (arkts-no-structural-typing) <ArkTSCheck>,<html>Type '{ supportDoctype: true; ignoreNamespace: boolean; tagValueCallbackFunction: (name: string, value: string) => boolean; }' is not assignable to type 'ParseOptions'.<br/>Object literal may only specify known properties, but 'ignoreNamespace' does not exist in type 'ParseOptions'. Did you mean to write 'ignoreNameSpace'? <ArkTSCheck>,Expected 1 arguments, but got 0. <ArkTSCheck>三个问题
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值