</pre>背景</h2><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">在古代中国,《三字经》、《百家姓》、《千字文》被合称为三、百、千,都是非常重要的启蒙教育课本,广为流传。而其中问世最早的《千字文》更凭借其优美的文字、华丽的辞藻成为中华传统文化的一个重要组成部分,得到了人们的普遍重视和喜爱。</p><p style="margin-top:0px; margin-bottom:10px; margin-left:40px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">《千字文》的作者,是梁武帝时代官拜散骑员外郎的周兴嗣。历来在正史上的记载,就这样一笔带过,但据私家笔记的野史记载,内容不是这样简单了。周兴嗣同梁武帝本来便是文字之交的朋友,在萧齐时代,还在朝廷上有过同僚之谊。到了梁武帝当了皇帝,那就变成君臣的关系。由朋友变君臣,说是关系不错,其实,伴君如伴虎,反是最糟糕的事,周兴嗣有一次不小心得罪了梁武帝,梁武帝一怒之下,想杀他或很严厉地处分他,到底还是于心不忍,只好下令把先关起来再说。但梁武帝又说了一句话,你不是文才很好吗?你能在一夜之间,把一千个不同的字,写一篇好文章,就赦你无罪。因此,周兴嗣就在一夜之间,挖空心思,写了这篇《千字文》。文章写好了,可是在一夜之间,头发、眉毛、胡子也都白了!<strong>大家要注意,用一千个不同的中文字,一夜之间,写出有关宇宙、物理、人情、世故的文间,等于写了一篇非常精简的“中国文化纲领要点”,虽然,只写到南北时期的梁朝为止,实在也太难了。</strong>梁武帝本人,才华文学都自命不凡,看了周兴嗣一夜之间之间所写的《千字文》,也不能不佩服。周兴嗣因此得到宽恕,而且还特加赏赐。</p><p style="margin-top:0px; margin-bottom:10px; margin-left:40px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px; text-align:right">——摘自南怀瑾《原本大学微言》</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">现代人,一生中有机会通读千字文的机会是非常少的,很多理工科的大学生,完全没有听说过这篇神奇的文章,自然也会抱着“怀疑一切”的态度问,真的有这么神吗,真的一个重复的字都没有吗?耳听为虚,眼见为实,我们就来检验一下千字文中是不是真的没有重复的字。</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">不过在动手之前,先让我们花上几分钟的时间,读读《千字文》吧:<a target=_blank target="_blank" href="http://online.bit.edu.cn/moodle/mod/page/view.php?id=52893" style="color:rgb(67,142,185); text-decoration:none">简体中文版</a>、<a target=_blank target="_blank" href="http://online.bit.edu.cn/moodle/mod/page/view.php?id=52894" style="color:rgb(67,142,185); text-decoration:none">正体中文版</a>。</p><h2 style="margin:10px 0px; font-family:open_sansbold,sans-serif; line-height:40px; color:rgb(85,85,85); font-size:28px; font-weight:normal!important">任务</h2><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">请编写一个程序,从输入中读取一篇中文文章,并统计出该文章中 ASCII 字符以外的重复出现的每一个字重复出现的次数。</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">文章使用 UTF-8 编码,可能会出现任何可以用 UTF-8 编码表示的字符(不限于中文)。</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px"><span style="color:rgb(255,0,0)">文章中所有的字符在 UCS-2 能够表示的范围内,即字符的 Unicode 值用两个字节就可以表示。</span></p><h2 style="margin:10px 0px; font-family:open_sansbold,sans-serif; line-height:40px; color:rgb(85,85,85); font-size:28px; font-weight:normal!important">输入</h2><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">一篇文章,总字数不限、每行字符数不限。每个字重复出现的次数不超过 60000 次。</p><h2 style="margin:10px 0px; font-family:open_sansbold,sans-serif; line-height:40px; color:rgb(85,85,85); font-size:28px; font-weight:normal!important">输出</h2><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">按照 Unicode 编码从小到大的顺序,输出文章中 ASCII <span style="color:blue">(0~127)</span> 字符以外的每一个重复出现过的字重复出现的次数。每行包含三项内容,首先是重复的字符(以 UTF-8 编码输出),然后是该字符的 Unicode 编码值(十六进制输出,字母均使用小写,长度不足4位数的用0补齐),最后输出该字符的重复次数。</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">如果文章中没有出现重复的字,则输出“No repeat!”。</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">由于网页中的样例输入输出很奇怪,所以就不在这里贴了……</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px"></p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">题解:</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px">首先推荐一下大神的博客:<a target=_blank target="_blank" href="http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html">点击打开链接</a> 只用看utf-8的部分就可以了.</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px"><span style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)">Unicode符号范围 | UTF-8编码方式</span><br style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)" /><span style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)">(十六进制) | (二进制)</span><br style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)" /><span style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)">--------------------+---------------------------------------------</span><br style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)" /><span style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)">0000 0000-0000 007F | 0xxxxxxx</span><br style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)" /><span style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)">0000 0080-0000 07FF | 110xxxxx 10xxxxxx</span><br style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)" /><span style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)">0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx</span><br style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)" /><span style="color:rgb(17,17,17); font-family:Consolas,Monaco,'Andale Mono',monospace; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,242,240)">0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx</span></p><p style="margin-top:0px; margin-bottom:10px">所以文字在输入时有四种形式,而具体是哪种需要判断。如果输入的第一个值小于128,则是第一种;大于128小于192则是第二种,大于192小于224是第三种,剩下一个就是第四种。使用位运算可以更加直观地判断:(首先读入a)(a>>4)==15是第四种,(a>>4)==14是第三种,(a>>5)==6是第二种,剩下的是第一种。而第一种又不需要计数。</p><p style="margin-top:0px; margin-bottom:10px">知道了文字编码格式后,就看一下这些数字具体是怎么储存的。</p><p style="margin-top:0px; margin-bottom:10px"><span style="color:rgb(17,17,17); font-family:Georgia,serif; font-size:16px; line-height:28px; word-spacing:2px; background-color:rgb(245,245,213)">已知"严"的unicode是4E25(100111000100101),根据上表,可以发现4E25处在第三行的范围内(0000 0800-0000 FFFF),因此"严"的UTF-8编码需要三个字节,即格式是"1110xxxx 10xxxxxx 10xxxxxx"。然后,从"严"的最后一个二进制位开始,依次从后向前填入格式中的x,多出的位补0。</span></p><p style="margin-top:0px; margin-bottom:10px">也就是说,读入a,b,c之后,还要算出来xxxx是多少,然后再乘上它们的位。第一个xxxx是(a-2^7-2^6-2^5),第二个是(b-2^7),第三个是(c-2^7),最后知道这个数的unicode编码是(a-2^7-2^6-2^5)*2^6*2^6+(b-2^7)*2^6+(c-2^7),最后再储存到数组里。输出的时候再算回来,输出编号,unicode号和出现的次数即可。</p><p style="margin-top:0px; margin-bottom:10px">这个题的难点在于理解编码的形式。</p><p style="margin-top:0px; margin-bottom:10px">AC代码:</p><p style="margin-top:0px; margin-bottom:10px; color:rgb(68,68,68); font-family:open_sansregular,Arial,Helvetica,sans-serif; font-size:13px; line-height:25px"></p><pre name="code" class="cpp">/*http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html*/
#include<stdio.h>
#include<string.h>
#include<locale.h>
#include<wchar.h>
struct node
{  int a;  unsigned char s[5];
}vis[(1<<16)+5];//记录英文字母的方法是长度26的数组,记录汉字就用65536的数组  int main()
{
//  freopen("F:in.txt", "r", stdin);
//  freopen("F:out.txt", "w", stdout);  unsigned char ch,ch1,ch2,ch3;  int i;  for (i = 0; i < 65536; i++)  {  vis[i].a = 0;  vis[i].s[0] = '\0';  }  while (scanf("%c",&ch)!=EOF)  {  int t=ch,t0 = ch, t1 = 0, t2 = 0, t3 = 0;  if ((ch>>4)==15)  {  scanf("%c%c%c", &ch1,&ch2,&ch3);  t1 = ch1; t2 = ch2; t3 = ch3;  t = (t0 - 128 - 64 - 32-16) * 64 * 64 * 64 + (t1 - 128) * 64 * 64 + (t2 - 128) * 64 + t3 - 128;  vis[t].s[0] = ch; vis[t].s[1] = ch1; vis[t].s[2] = ch2; vis[t].s[3] = ch3; vis[t].s[4] = '\0';  }  else if ((ch>>4)==14)  {  scanf("%c%c", &ch1, &ch2);  t1 = ch1; t2 = ch2;  t = (t0 - 128 - 64 - 32) * 64 * 64 + (t1 - 128) * 64 + t2 - 128;  vis[t].s[0] = ch; vis[t].s[1] = ch1; vis[t].s[2] = ch2; vis[t].s[3] = '\0';  }  else if ((ch >> 5) == 6)  {  scanf("%c", &ch1);  t1 = ch1;  t = (t0 - 64 - 128) * 64 + t1 - 128;  vis[t].s[0] = ch; vis[t].s[1] = ch1; vis[t].s[2] = '\0';  }  else  t = ch;  vis[t].a++;  }  int flag = 1;  for (i = 1; i < 65536; i++)  {  if (vis[i].a > 1)  if (vis[i].s[0]>=128)  {  printf("%s 0x%04x %d\n", vis[i].s, i, vis[i].a);
//          wprintf(L"%c 0x%x %d\n", i, i, vis[i]);  flag = 0;  }  }  if (flag)  printf("No repeat!\n");  return 0;
}  

网教9. 一夜发白《千字文》相关推荐

  1. BITCS2016程序设计 | 9. 一夜发白 《千字文》

    9. 一夜发白<千字文> 成绩 10 开启时间 2016年09月6日 星期二 11:00 折扣 0.8 折扣时间 2016年09月8日 星期四 23:55 允许迟交 否 关闭时间 2016 ...

  2. (程序设计方法与实践)一夜发白《千字文》

    一夜发白<千字文> Background 在古代中国,<三字经>.<百家姓>.<千字文>被合称为三.百.千,都是非常重要的启蒙教育课本,广为流传.而其中 ...

  3. 【BIT2021程设】7. 一夜发白《千字文》——Unicode和UTF-8、位运算

    写在前面: 本系列博客仅作为本人十一假期过于无聊的产物,对小学期的程序设计作业进行一个总结式的回顾,如果将来有BIT的学弟学妹们在百度搜思路时翻到了这一条博客,也希望它能对你产生一点帮助(当然,依经验 ...

  4. 学it中的python怎么样_长安大专网教学历

    包进l文也就有把件打去是没,长安l文自动件-扫描,r包文件里面二:对应的x解决缺少,h为修改. 离性)的.大专隔一致子性)和是原缩写性(性(持久.务.网教务.管理容器应用服务器支C事)事事务持J,一般 ...

  5. 教你如何收拾发短信的骗子们 (让40亿人崩溃)

    转自:凤凰网论坛 » 网络技术 [推荐]教你如何收拾发短信的骗子们 (让40亿人崩溃) 一直很郁闷,总没有收到过骗子的短信,以至于总觉得受歧视 今天,终于收到了一条骗子短信,还是时下最流行的冒充银行骗 ...

  6. python发朋友圈_10分钟教你用Python发一个高逼格的朋友圈

    程序猿声 你与千万程序猿在一起 01 前言 Hello~各位小伙伴们大家好.现在大家是越来越离不开手机,离不开微信了.每天打开手机的第一或者第二件事就是赶紧打开朋友圈看看有什么好玩的东西.偶尔忍不住了 ...

  7. 云客网教你让网站排名更好的方法

    有些人在进行网站优化的时候发现,自己没有去管的网站排名很好,而自己天天维护操作的站,排名却不见涨,这是什么原因呢?今天云客网教大家解决这些问题的方法. 网站维护的内容价值极小 很多人每天的优化操作重点 ...

  8. 职称计算机证是继续教育的内容吗,豆腐网教你一分钟弄懂继续教育学时认定表及上传注意事项~...

    原标题:豆腐网教你一分钟弄懂继续教育学时认定表及上传注意事项~ 大家好,我是豆腐网王老师,专注于南京工程师职称评审政策解读,关注我,让您轻松面对职称评审,少走弯路!!! 南京中高级职称申报已有段时间, ...

  9. 为什么喝酒有的人会脸红,有的人会脸发白

    http://zhidao.baidu.com/question/89581 为什么喝酒有的人会脸红,有的人会脸发白? 让我们从脸红的原因说起吧.很多人以为是酒精导致的,其实不然,是乙醛引起的.乙醛具 ...

最新文章

  1. [翻译] NMock 简介
  2. nginx 上传文件 405_今天教大家Nginx+tomcat负载均衡群集如何配置
  3. html固定dl高度_HTML入门笔记1
  4. android弹窗不能手动关闭_vue3.0系列:Vue3自定义PC端弹窗组件V3Layer
  5. java.jsp.jdbc_Java-jsp使用JDBC访问数据库时显示乱码是怎么回事?
  6. ie 浏览器布局中的 offset
  7. 神奇的python(六)之python的串口操作(pyserial)
  8. 收藏 | 10个重要问题概览Transformer全部内容
  9. 创建你自己的AngularJS -- 第一部分 Scopes(一)
  10. Xgboost算法——Kaggle案例
  11. Unity场景打包AssetBundle并加载
  12. 问答题库(路由与交换){简答版}
  13. 什么是CPU Die?
  14. Ubuntu安装deb包
  15. 北京玉渊潭公园第二十届樱花节
  16. su自带模型库怎么打开_草图大师模型库竟然在软件内部
  17. 南柯服务器压力,南柯梦崇洋(十一)
  18. 2022年P气瓶充装考试试题及答案
  19. 关于ViewGroup和View的事件分发
  20. 如何复制网页上的任何内容(比如百度文库)

热门文章

  1. drupal安装模块
  2. 在FireFox下设为首页的解决方法
  3. boat启动器 minecraft_我的世界boat启动器
  4. 设计者最想拥有的45个美丽手绘字体
  5. ISO8601时间格式的转换
  6. php 实现抽奖功能
  7. ArrayList的remove方法
  8. Cityscapes数据集的深度完整解析
  9. openCV3中双目深度BM算法参数解析
  10. 【数据压缩】H.264文件解析和码流分析