[刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
来源:互联网 发布:宽带拨号软件下载 编辑:程序博客网 时间:2024/04/29 21:38
书上具体所有题目: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 1005char 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 502unsigned 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 101000char 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是不是有点违和。。)
0 0
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
- 算法竞赛入门经典(刘汝佳)习题3-8 UVa202
- 算法竞赛入门经典(刘汝佳)习题3-7 UVa1368
- 算法竞赛入门经典(刘汝佳)习题3-9 UVa10340
- 算法竞赛入门经典(第2版)习题3-8 循环小数 Repeating Decimals UVa202
- 算法竞赛入门经典(第二版) 习题3-8 循环小数(Repeating Decimals) UVa202 Finals1990
- 《算法竞赛入门经典2ndEdition 》习题3-8 循环小数(Repeating Decimals, Uva202)
- 《算法竞赛入门经典2ndEdition 》习题3-7 DNA序列(DNA Consensus String, Uva1368)
- 《算法竞赛入门经典2ndEdition 》习题3-9 子序列(All in All, Uva10340)
- 算法竞赛入门经典(第2版)习题3-7 DNA序列 DNA Consensus String UVa1368
- 算法竞赛入门经典(第2版)习题3-9 子序列 All in All UVa10340
- 算法竞赛入门经典(第2版)习题3-9 子序列(All in All) Uva10340
- 算法竞赛入门经典 UVa10340子序列
- 算法竞赛入门经典 第三章 uVA202 - Repeating Decimals
- 算法竞赛入门经典java版程序CH3 UVA202
- 算法竞赛入门经典 第三章 uVA1368 - DNA Consensus String
- 算法竞赛入门经典java版程序CH3 UVa1368
- 算法竞赛入门经典 第三章 uVa10340 - All in All
- 如何在Apache 上配置 WebDAV 服务器
- 求100—999之间的水仙花数
- Java Notes: Containers
- (1)ping www.baidu.com [ping: unknown host www.baidu.com]---(2)xshell 连接虚拟机问题---(3) 网卡重启失败
- 面试题(1)
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
- 【Android笔记】Android MediaPlayer的生命周期
- 基础总结篇之一:Activity生命周期
- C代码:二分法求三次方程近似根
- html css jquery怎么做开门的效果
- TOJ 2470Robot in Maze (广度搜索应用)
- Ubuntu 之路
- POJ 3169 差分约束
- 算法积累——K均值聚类(Kmeans)