[刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO
都是《算法竞赛入门经典(第二版)》的题目,标题上没写(第二版)
题目:算法竞赛入门经典 3-7/UVa1368:DNA Consensus String
代码:
//UVa1368 - DNA Consensus String
#include<iostream>
using namespace std;
#define MAX_M 52
#define MAX_N 1005
char DNA[MAX_M][MAX_N];
int main()
{
int T, m, n, err, num[4];//num用于存放ACGT个数
const int &mm = m, &nn = n;//防止自己代码书写错误
char consensus[MAX_N], ACGT[] = "ACGT";//consensus为计算结果
cin >> T;
while (T--) {
err = 0;
cin >> m >> n;
cin.getline(DNA[51], 3);//吸收'\n'
for (int i = 0;i < mm;++i)
cin.getline(DNA[i], MAX_N);
for (int i = 0;i < nn;++i) {
num[0] = num[1] = num[2] = num[3] = 0;//初始化或重置num
for (int j = 0;j < mm;++j)
switch (DNA[j][i]) {
case 'A': ++num[0];break;
case 'C': ++num[1];break;
case 'G': ++num[2];break;
case 'T': ++num[3];break;
default:cerr << "Error:1\n";exit(0);
}
int max = 0;
for (int j = 1;j < 4;++j)
if (num[j]>num[max])max = j;
consensus[i] = ACGT[max];
err += mm - num[max];
}
consensus[nn] = '\0';
cout << consensus << '\n' << err << '\n';
}
return 0;
}
分析:找出每个DNA序列的第i个的碱基,找到出现最多的项,即为Consensus String的第i个碱基
在算法竞赛入门经典里看到使用常量数组的方法,可以有效减少switch与if的使用,感觉很好用(虽然记得学校教材上也讲到过,但是当时没那么印象深刻)。于是这次在ACGT[]=”ACGT”这里用到了,真心好用呀。
题目:算法竞赛入门经典 3-8/UVa202:Repeating Decimals
代码:
//UVa202 - Repeating Decimals
#include<iostream>
using namespace std;
#define MAX 502
unsigned Decimal[MAX];//存放各个小数
unsigned Remainder[MAX];//存放各位小数对应的余数
int main()
{
unsigned numerator, denominator;//分子、分母
const unsigned &de = denominator;
int temp, digit;//由于分母可以正可以负,先输入temp至int再转为unsigned,digit记录位数(或者说是位数-1)
while (cin >> numerator) {
cin >> temp;
digit = 0;
cout << numerator << '/' << temp << " = ";
if (temp < 0)temp = -temp, cout << '-';
denominator = (unsigned)temp;
cout << numerator / de << '.';
Decimal[0] = (numerator % de * 10) / de;
Remainder[0] = (numerator % de * 10) % de;
while (digit < MAX) {
unsigned di, t = Remainder[digit++] * 10;
Decimal[digit] = t / de;
Remainder[digit] = t % de;
for (di = 0;di < digit;++di)
if (Decimal[di] == Decimal[digit] && Remainder[di] == Remainder[digit])
break;
if (di < digit) {
for (int i = 0;i < di;++i)
cout << Decimal[i];
cout << '(';
if (digit - di <= 50)
for (int i = di;i < digit;++i)
cout << Decimal[i];
else {
for (int i = 0;i < 50;++i)
cout << Decimal[di+i];
cout << "...";
}
cout << ")\n\t" << digit - di << " = number of digits in repeating cycle\n\n";
break;
}
}
if (digit == MAX) { cerr << "Error:1\n";exit(0); }
}
return 0;
}
分析:算出下一位小数b并与之前每一位小数比较,如果找到一位小数a,a的小数值与对应的余数均与b的小数与余数相等,则这两个数a—-b之间的就是循环部分。想不出好办法,不是所有小数都是像1/3一样从第一位开始洗脑循环。
题目:算法竞赛入门经典 3-9/UVa10340:All in All
代码:
//UVa10340 - All in All
#include<iostream>
#define M 101000
char s[M], t[M], *ps, *pt;//s短t长
int main()
{
while (std::cin>>s>>t) {
//为什么scanf按了crtl^z还是不停止,还得再按一下回车才能结束程序。。。
//于是使用scanf导致提交总是超时,因为程序还在等着输入
//而cin只要按一下crtl^z再回车,马上就结束了。不懂。。
//好吧,于是虽然试图尽量少用cin cout,但是c的东西还是不大会用,不熟练
ps = s, pt = t;
while (*ps != '\0') {
while (*pt != *ps&&*pt != '\0')++pt;
if (*pt == '\0') break;
++ps,++pt;
}
printf(*ps == '\0' ? "Yes\n" : "No\n");
}
return 0;
}
分析:不难(上面用cin下面用printf是不是有点违和。。)
[刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340的更多相关文章
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A
题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...
- [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation
题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...
随机推荐
- React虚拟DOM具体实现——利用节点json描述还原dom结构
前两天,帮朋友解决一个问题: ajax请求得到的数据,是一个对象数组,每个对象中,具有三个属性,parentId,id,name,然后根据这个数据生成对应的结构. 刚好最近在看React,并且了解到其 ...
- TypeScript入门-接口
▓▓▓▓▓▓ 大致介绍 在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约. ▓▓▓▓▓▓ 接口 例子: function printLabel(labelledO ...
- mvalidator手机端校验
官网地址:https://github.com/efri-yang/mobileValidate#%E5%8F%82%E6%95%B0 使用方法: html如下: <li class=" ...
- 车大棒浅谈jQuery源码(二)
前言 本来只是一个自己学习jQuery笔记的简单分享,没想到获得这么多人赏识.我自己也是傻呵呵的一脸迷茫,感觉到受宠若惊. 不过还是有人向批判我的文章说,这是基本知识点,完全跟jQuery源码沾不上边 ...
- Redis + keepalived 高可用群集搭建
本次实验环境介绍: 操作系统: Centos 7.3 IP : 192.168.10.10 Centos 7.3 IP : 192.168.10.20 VIP 地址 : 192.168.1 ...
- JAVA优化建议
前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用, ...
- Where T:Class,new()的使用
当我们使用泛型的时候,有时候就会提示我们T必须是引用类型而无法进行下去,其实我们学泛型的时候也应该了解到这个T的使用场合,他可以是值类型也可以是引用类型,但是我们某些场合就只能使用引用类型比如EF中的 ...
- 老李分享:大数据框架Hadoop和Spark的异同 1
老李分享:大数据框架Hadoop和Spark的异同 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨 ...
- 老李推荐:第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态
老李推荐:第14章5节<MonkeyRunner源码剖析> HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态 poptest是国内唯一 ...
- SVG如何做圆形图片
SVG如何做圆形图片 2016年5月31日17:30:48 提到圆形图片,大家首先想到的一定是border-radius,但在SVG中这些方法很难起效,下面方法适合SVG中制作任意规则与不规则的图形. ...