您的位置:首页 > 理论基础 > 数据结构算法

《算法竞赛入门经典》 第二章 循环结构程序设计 习题

2016-06-27 22:09 441 查看
注:这里为了便于测试,直接使用stdin,stdout进行输出,没有进行文件读写操作。

习题2-1 位数(digit)

输入一个不超过10^9的正整数,输出它的位数。例如12735的位数是5。请不要使用任何数学函数,只用四则运算和循环语句实现。

#include <iostream>

using namespace std;

int main() {

int n;

while (cin >> n) {

int cnt = 0;

while (n) {

n /= 10;

cnt ++;

}

cout << cnt << endl;

}

return 0;

}

习题2-2 水仙花数(daffodil)

输入100~999中的所有水仙花数。若3位数ABC满足ABC=A^3+B^3+C^3,则称其为水仙花数。例如153=1^3+5^3+3^3,所以153是水仙花数。

#include <iostream>

using namespace std;

int main() {

for (int i = 100; i < 1000; i ++) {

int a = i / 100;

int b = i / 10 % 10;

int c = i % 10;

if (i == a * a * a + b * b * b + c * c * c)

cout << i << endl;

}

return 0;

}

习题2-3 韩信点兵(hanxin)

相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排、五人一排、七人一排地变化队形,而他每次只掠一眼队伍的排尾就知道总人数了。输入3个非负整数a,b,c,表示每种队形排位的人数(a<3,b<5,c<7),输出总人数的最小值(或报告无解)。已知总人数不小于10,不超过100。

样例输入:2 1 6

样例输出:41

样例输入:2 1 3

样例输出:No answer

#include <iostream>

using namespace std;

int main() {

int a , b, c, x;

while (cin >> a >> b >> c) {

for (x = 10; x <=100; x ++)

if (x % 3 == a && x % 5 == b && x % 7 == c) {

cout << x << endl;

break;

}

if (x > 100)

cout << "No Andwer" << endl;

}

return 0;

}

习题2-4 倒三角形(triangle)

输入正整数n<=20,输出一个n层的倒三角形。例如n=5时输出如下:

#########

#######

#####

###

#

#include <iostream>

#include <cstdio>

using namespace std;

int main() {

int n;

while (cin >> n) {

for (int i = 0; i < n; i ++) {

for (int j = 0; j < i; j ++)

putchar(' ');

for (int j = 1; j < 2*(n-i); j ++)

putchar('#');

putchar('\n');

}

}

return 0;

}

习题2-5 统计(stat)

输入一个正整数n,然后读取n个正整数a1,a2,...,an,最后再读一个正整数m。统计a1,a2,...,an中了有多少个整数的值小于m。

#include <iostream>

#include <cstdio>

using namespace std;

int main() {

int n, m, a[1000], c;

while (cin >> n) {

for (int i = 0; i < n; i ++) cin >> a[i];

cin >> m;

c = 0;

for (int i = 0; i < n; i ++)

if (a[i] < m)

c ++;

cout << c << endl;

}

return 0;

}

习题2-6 调和级数(harmony)

输入正整数n,输出H(n)=1+1/2+1/3+...+1/n的值,保留3位小数。例如n=3时答案为1.833。

#include <iostream>

#include <cstdio>

using namespace std;

int main() {

int n;

double x;

while (cin >> n) {

x = 0;

for (int i = 1; i <= n; i ++)

x += 1.0 / (double) i;

printf("%.3lf\n", x);

}

return 0;

}

习题2-7 近似计算(approximation)

计算pi/4=1-1/3+1/5-1/7+...,直到最后一项小于10^-6。

#include <iostream>

#include <cstdio>

using namespace std;

int main() {

double pi4 = 0, pi, flag = 1.;

for (int i = 0; i*2+1 < 1000000; i ++) {

pi4 += flag / (double) (i*2+1);

flag = - flag;

}

cout << pi4 << endl;

pi = pi4 * 4;

cout << pi << endl;

return 0;

}

习题2-8 子序列的和(subsequence)

输入两个正整数n<m<10^6,输出1/(n^2)+1/((n+1)^2)+...+1/(m^2),保留5位小数。例如n=2,m=4时答案是0.42361;n=65536,m=655360时答案是0.00001。注意:本题有陷阱。

#include <iostream>

#include <cstdio>

#include <cmath>

using namespace std;

int main() {

int n, m;

while (cin >> n >> m) {

double ans = 0;

if (n > m) swap(n, m);

for (int i = n; i <= m; i ++) {

ans += 1.0 / pow(1. * i, 2.0);

}

printf("%.5lf\n", ans);

}

return 0;

}

习题2-9 分数化小数(decimal)

输入正整数a,b,c,输出a/b的小数形式,精确到小数点后c位。a,b<=10^6,c<=100。例如a=1,b=6,c=4时应输出0.1667。

#include <iostream>

#include <cstdio>

#include <cmath>

using namespace std;

int main() {

int a, b, c;

while (cin >> a >> b >> c) {

cout << a / b;

if (c > 1)

cout << ".";

a %= b;

while (c --) {

a *= 10;

if (c)

cout << a / b;

else {

if (a % b * 10 / b >= 5)

cout << a / b + 1;

else

cout << a / b;

}

a %= b;

}

cout << endl;

}

return 0;

}

习题2-10 排列(permutation)

用1,2,3,...,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3。输出所有解。提示:不必太动脑筋。

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 3; i ++)

for (int j = 1; j <= 3; j ++) {

if (i == j) continue;

for (int k = 1; k <= 3; k ++) {

if (k == i || k == j) continue;

int x = i * 100 + j * 10 + k;

cout << x << " " << 2 * x << " " << 3 * x << endl;

}

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: