算法竞赛入门经典-第一章源代码

// 程序1-7 三位数反转 (2)
#include <stdio.h>
int main(void)
{
	int n, m;
	scanf("%d", &n);
	m = (n%10)*100 + (n/10%10)*10 + (n/100);
	printf("%03d\n", m);	// 处理个位数为零的特殊情况
	return 0;
}

// 程序1-11 鸡兔同笼
#include <stdio.h>
int main(void)
{
	int a, b, n, m;
	scanf("%d%d", &n, &m);
	a = (4*n-m)/2;	 // 解方程直接算出结果
	b = n-a;
	if(m%2==1 || a<0 || b<0)	// 排除错误结果
		printf("No answer\n");
	else
		printf("%d %d\n", a, b);
	return 0;
}

// 程序1-13 三整数排序(2)
#include <stdio.h>
int main(void)
{
	int a, b, c;
	scanf("%d%d%d", &a, &b, &c);
	if (a<=b && b<=c) printf("%d %d %d\n", a, b, c);
	else if (a<=c && c<=b) printf("%d %d %d\n", a, c, b);
	else if (b<=a && a<=c) printf("%d %d %d\n", b, a, c);
	else if (b<=c && c<=a) printf("%d %d %d\n", b, c, a);
	else if (c<=a && a<=b) printf("%d %d %d\n", c, a, b);
	else if (c<=b && b<=a) printf("%d %d %d\n", c, b, a);
	return 0;
}

// 程序1-14 三整数排序(3)
#include <stdio.h>
int main(void)
{
	int a, b, c, t;
	scanf("%d%d%d", &a, &b, &c);
	if(a>b) {t=a; a=b; b=t;}	// 判断的顺序很关键
	if(a>c) {t=a; a=c; c=t;}
	if(b>c) {t=b; b=c; c=t;}
	printf("%d %d %d\n", a, b, c);
	return 0;
}

// 程序1-15 三整数排序(4)
#include <stdio.h>
int main(void)
{
	int a, b, c, x, y, z;
	scanf("%d%d%d", &a, &b, &c);
	x=a; if(b<x) x=b; if(c<x) x=c;
	z=a; if(b>z) z=b; if(c>z) z=c;
	y=a+b+c-x-z;	 // 利用“问题求解”这一目标,并没有真正排序
	printf("%d %d %d\n", x, y, z);
	return 0;
}

// 习题1-1 平均数(average)
#include <stdio.h>
int main(void)
{
    int a, b, c;
    double aver;
    scanf("%d%d%d", &a, &b, &c);
    aver = (a+b+c) / 3.0;
    printf("%.3f\n", aver);
    return 0;
}

// 习题1-2 温度(temperature)
#include <stdio.h>
int main(void)
{
	float f, c;
	scanf("%f", &f);
	c = 5*(f-32)/9;
	printf("%.3f\n", c);
	return 0;
}

// 习题1-3 连续和(sum)
#include <stdio.h>
int main(void)
{
	int n, sum;
	scanf("%d", &n);
	sum = (1+n)*n/2;
	printf("%d\n", sum);
	return 0;
}

// 习题1-4 正弦和余弦(sincos)
#include <stdio.h>
#include <math.h>
int main(void)
{
	int n;
	double x;
	const double PI = 3.14;	 // 尽量用const关键字声明常数
	scanf("%d", &n);
	x = n*(PI/180.0);
	printf("%f\n", sin(x));
	printf("%f\n", cos(x));
	return 0;
}

// 习题1-5 距离(distance)
#include <stdio.h>
#include <math.h>
int main(void)
{
	float x1, y1, x2, y2, dis;
	scanf("%f%f%f%f", &x1, &y1, &x2, &y2);
	dis = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
	printf("%f\n", dis);
	return 0;
}

// 习题1-6 偶数(odd)
#include <stdio.h>
int main(void)
{
	int n;
	scanf("%d", &n);
	if(n%2 == 0)
		printf("yes\n");
	else
		printf("no\n");
	return 0;
}

// 习题1-7 打折(discount)
#include <stdio.h>
int main(void)
{
	int n;
	float money;
	scanf("%d", &n);
	money = 95*n;
	if(money >= 300)
		money *= 0.85;
	printf("%.2f\n", money);
	return 0;
}

// 习题1-8 绝对值(abs)
#include <stdio.h>
int main(void)
{
	float x;
	scanf("%f", &x);
	if(x>0)
		printf("%.2f\n", x);
	else
		printf("%.2f\n", -x);
	return 0;
}

// 习题1-9 三角形(triangle)
#include <stdio.h>
int main(void)
{
	int a, b, c;
	scanf("%d%d%d", &a, &b, &c);
	if(a+b>c && a+c>b && b+c>a)
	{
		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");
	}
	else
    	printf("not a triangle\n");
	return 0;
}

// 习题1-10 年份(year)
#include <stdio.h>
int main(void)
{
	int n;
	scanf("%d", &n);
	if((n%4==0&&n%100!=0) || n%400==0)
		printf("yes\n");
	else
		printf("no\n");
	return 0;
}

转载于:https://www.cnblogs.com/yanweicode/p/4356297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值