《算法竞赛入门经典(第2版)》代码 Chapter 1

算法竞赛QQ群:210838572,一起进步吧!

例题代码

1-1 圆柱体的表面积

// 1-1
#include <stdio.h>
#include <math.h>
int main()
{
	const double pi = acos(-1.0); // ¦Ð 
	double r, h, s1, s2, s;
	scanf("%lf%lf", &r, &h); // C99
	s1 = pi*r*r;
	s2 = 2*pi*r*h;
	s = s1*2 + s2;
	printf("Ares = %.3f\n", s);
	return 0;
}


1-2 三位数反转

// 1-2
#include <stdio.h>
int main()
{
	int num;
	scanf("%d", &num);
	printf("%d%d%d\n", num%10, num/10%10, num/100);
	return 0;
}


1-3 交换变量

// 1-3
#include <stdio.h>
int main()
{
	int a, b, t;
	scanf("%d%d", &a, &b);
	t = a;
	a = b;
	b = t;
	printf("%d %d\n", a, b);
	return 0;
}


1-4 鸡兔同笼

// 1-4
#include <stdio.h>
int main()
{
	int x, y, m, n;
	scanf("%d%d", &n, &m);
	y = (m-2*n)/2;
	x = n-y;
	if(m % 2 == 1 || x < 0 || y < 0) // ¼¦ÍþùΪÕûÊý£¬²¢ÇÒ¶¼´óÓÚ0 
	{
		printf("No Answer\n");
	}
	else
	{
		printf("%d %d\n", x, y);
	}
	return 0;
}


1-5 三整数排序

// 1-5
#include <stdio.h>
void swap(int &x, int &y);
int main()
{
	int a, b, c;
	scanf("%d%d%d", &a, &b, &c);
	if(a > b){swap(a, b);}
	if(a > c){swap(a, c);}
	if(b > c){swap(b, c);}
	printf("%d %d %d\n", a, b, c); 
	return 0; 
}
void swap(int &x, int &y)
{
	int t;
	t = x;
	x = y;
	y = t;
	return;
}


习题代码

t1-1 平均数(average)

// t1-1
#include <stdio.h>
int main()
{
	double x, y, z, res;
	scanf("%lf%lf%lf", &x, &y, &z);
	res = (x+y+z)/3;
	printf("%.3f\n", res);
	return 0;
}


t1-2 温度(temperature)

// t1-2
#include <stdio.h>
int main()
{
	double f, c;
	scanf("%lf", &f);
	c = 5*(f-32)/9;
	printf("%.3f\n", c);
	return 0;
}


t1-3 连续和(sum)

// t1-3
#include <stdio.h>
int main()
{
	double n, res;
	scanf("%lf", &n);
	res = n*(n+1)/2;
	printf("%.3f", res);
	return 0;
}


t1-4 正弦和余弦(sin和cos)

// t1-4
#include <stdio.h>
#include <math.h>
int main()
{
	const double pi = acos(-1.0);
	double n, s, c;
	scanf("%lf", &n);
	n = n/180*pi;
	s = sin(n);
	c = cos(n);
	printf("%f %f", s, c);
	return 0;
}


t1-5 打折(discount)

// t1-5
#include <stdio.h>
int main()
{
	const double PRICE = 95;
	const double sale = 0.85;
	double n, cost;
	scanf("%lf", &n);
	cost = PRICE*n;
	if(cost >= 300)
	{
		printf("%.2f\n", cost*sale);
	}
	else
	{
		printf("%.2f\n", cost);
	}
	return 0;
}


t1-6 三角形(triangle)

// t1-6
#include <stdio.h>
int main()
{
	int a, b, c;
	scanf("%d%d%d", &a, &b, &c);
	if(!(a+b > c)) {printf("not a triangle\n"); return 0;}
	if(!(a+c > b)) {printf("not a triangle\n"); return 0;}
	if(!(b+c > a)) {printf("not a triangle\n"); return 0;}
	if(a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a)
	{
		printf("yes\n");
	}
	else
	{
		printf("no\n");
	}
	return 0;
}


t1-7 年份(year)

// t1-7
#include <stdio.h>
int main()
{
	int year;
	scanf("%d", &year);
	if(year%4 == 0 && year%100 != 0)
	{
		printf("yes\n");
	}
	else
	{
		printf("no\n"); 
	} 
	return 0;
}


  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
算法竞赛入门经典——训练指南》代码仓库 例题代码 限于篇幅,书上并没有给出所有例题的代码,这里给出了所有例题的代码,并且改进了书上的一些代码。 第一章 32题 38份代码 第二章 28题 30份代码 第三章 22题 23份代码 第四章 19题 21份代码 第五章 34题 39份代码 第六章 24题 26份代码 共159题 177份代码 为了最大限度保证代码风格的一致性,所有例题代码均由刘汝佳用C++语言编写。 所有代码均通过了UVa/La的测试,但不能保证程序是正确的(比如数据可能不够强),有疑问请致信rujia.liu@gmail.com,或在googlecode中提出: http://code.google.com/p/aoapc-book/ [最新更新] 2013-04-23 增加字符串中例题10(UVa11992 Fast Matrix Operations)的另一个本的程序,执行效率较低,但更具一般性,可读性也更好 2013-04-22 增加字符串部分“简易搜索引擎”代码,可提交到UVa10679 2013-04-13 修正Treap中优先级比较的bug(原来的代码实际上是在比较指针的大小!),加入纯名次树代码 2013-03-31 修正UVa1549标程的bug,即buf数组不够大。 增加线段树部分“动态范围最小值”的完整代码 2013-03-23 修正UVa10054标程的bug,即没有判断是否每个点的度数均为偶数。UVa数据已经更新 LA3401修正了代码和文字不一致的问题 UVa11270增加了答案缓存 2013-03-21 增加线段树部分中两个经典问题的完整代码:快速序列操作I和快速序列操作II 2013-02-28 补全所有159道例题的代码
算法竞赛入门经典--训练指南,代码仓库,有四个本的代码仓库。 《算法竞赛入门经典——训练指南》代码仓库 例题代码 限于篇幅,书上并没有给出所有例题的代码,这里给出了所有例题的代码,并且改进了书上的一些代码。 第一章 32题 38份代码 第二章 28题 30份代码 第三章 22题 23份代码 第四章 19题 21份代码 第五章 34题 39份代码 第六章 24题 26份代码 共159题 177份代码 为了最大限度保证代码风格的一致性,所有例题代码均由刘汝佳用C++语言编写。 所有代码均通过了UVa/La的测试,但不能保证程序是正确的(比如数据可能不够强),有疑问请致信rujia.liu@gmail.com,或在googlecode中提出: http://code.google.com/p/aoapc-book/ [最新更新] 2013-04-23 增加字符串中例题10(UVa11992 Fast Matrix Operations)的另一个本的程序,执行效率较低,但更具一般性,可读性也更好 2013-04-22 增加字符串部分“简易搜索引擎”代码,可提交到UVa10679 2013-04-13 修正Treap中优先级比较的bug(原来的代码实际上是在比较指针的大小!),加入纯名次树代码 2013-03-31 修正UVa1549标程的bug,即buf数组不够大。 增加线段树部分“动态范围最小值”的完整代码 2013-03-23 修正UVa10054标程的bug,即没有判断是否每个点的度数均为偶数。UVa数据已经更新 LA3401修正了代码和文字不一致的问题 UVa11270增加了答案缓存 2013-03-21 增加线段树部分中两个经典问题的完整代码:快速序列操作I和快速序列操作II 2013-02-28 补全所有159道例题的代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值