算法竞赛入门经典习题

第一章:程序设计入门

总结
1、%.2f 表示保留两位小数

2、const double Pi=acos(-1.0) //尽量用const声明常量

3、三整数排序:

	If(a>b) {交换} if(a>c){交换} if(b>c){交换}`

第二章:循环结构设计

总结

1、重复次数可变、递增:for(初始化 ; 条件 ; 调整)
循环次数不确定、也不是递增:while
循环终止判断是在计算之后:do while

2、溢出问题:int32_t uint32_t (约21亿) 若溢出用 long long
检查方法:输出中间变量

3、管道输入:echo 内容 | 程序名

4、计时函数:

#include <time.h>
(double)clock() / CLOCKS_PER_SEC 可以写出当前程序运行的秒数

5、遇到没有输入结束标志:

 While(scanf(“%d”,&x)==1) //scanf返回输入的变量个数
 想要结束:windows: enter Ctrl+Z enter
		  Linux:Ctrl+D

6、不想删除某些信息的写法

#define Local 
#ifdef Local 
	 freopen("txt.in","r",stdin);
	 freopen("txt.out","w",stdout);
 #endif  //注释用`

7、遇到最大值最小值问题:可以先规定一个最大值INF 最小值就是 -INF。

8、嵌套的两层代码块,内层会隐蔽外层。

9、 iomanip,在C++程序里面经常见到下面的头文件#include ,io代表输入输出,manip是manipulator(操纵器)的缩写(在c++上只能通过输入缩写才有效)。

在这里插入图片描述

  1. 水仙花数
#include <iostream>
using namespace std;
int main()
{
	int a, b, c;
	int sum = 0;
	for (int i = 100; i <= 999; ++i)
	{
		a = i % 10;
		b = (i / 10) % 10;
		c = i / 100;
		sum = a * a * a + b * b * b + c * c * c;
		if (i == sum)
			cout << i<<" ";
	}
}
  1. 韩信点兵
/*没能写出直接输入多行的情况,只能实现一行一行输入*/
#include <iostream>
using namespace std;

int main()
{
	//freopen("1.txt","r",stdin);
	int a, b, c;
	int flag = 0;
	int n = 0;
	while (scanf("%d %d %d", &a, &b, &c)==3)
	{
		n++;
		for (int i = 10; i < 100; ++i)
		{
			if (i % 3 == a && i % 5 == b && i % 7 == c)
			{
				flag++;
				cout << "case " << n << ":" << i;
				break;
			}
		}
		if (flag == 0)
			cout << "No answer" << endl;
		cin.get();
	}
	return 0;
}
  1. 倒三角形
/*改进版打印沙漏*/
#include <iostream>
using namespace std;

int main()
{
	int N;
	cin >> N;
	for (int i = N; i >= 1; --i)
	{
		for (int j = N - i; j > 0; --j)
			cout << " ";
		for (int k = 1; k <= 2 * (i - 1) + 1; ++k)
			cout << "*";
		cout << endl;
	}
	for (int i = 2; i <= N; ++i)
	{
		for (int j = N - i; j > 0; --j)
			cout << " ";
		for (int k = 1; k <= 2 * (i - 1) + 1; ++k)
			cout << "*";
		cout << endl;
	}
}
  1. 子序列的和
#include <iostream>
using namespace std;

int main()
{
	//freopen("1.txt","r",stdin);
	long long n, m; //首先第一个注意的是平方溢出问题,所以要用longlong
	float sum = 0.0;
	int flag = 0;
	while (scanf("%lld %lld", &n, &m)==2 && n!=0 && m!=0)
	{
		flag++;
		for (float i = n; i <= m; ++i)  //其次,只有浮点数的计算才是浮点数,1是整数,所以i一定要是浮点数
		{
			sum += 1 / (i * i);
		}
		cout << "Case" << flag << ":";
		printf("%.5f\n", sum);
		sum = 0; //最后,sum一定要更新,否则会在原来的sum上进行累加出错
	}
}



  1. 分数化小数
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//freopen("1.txt","r",stdin);
	double a, b;
	double c;
	double res;
	while (scanf("%lf %lf %lf", &a, &b, &c) && a != 0, b != 0, c != 0)
	{
		res = a / b;
		cout<< setiosflags(ios::fixed) << setprecision(c)<<res;//C++中格式化方式
	}
}
  1. 排列
#include <iostream>
using namespace std;

int main()
{
	int a,b, c;
	for (a = 123; a < 329; ++a) //这里要限定一下最小的那个数字的范围
	{
		b = 2 * a;
		c = 3 * a;
		int d[10]{};
		int flag = 0;
		d[a % 10]++;
		d[a / 10 % 10]++;
		d[a / 100]++;

		d[b % 10]++;
		d[b / 10 % 10]++;
		d[b / 100]++;

		d[c % 10]++;
		d[c / 10 % 10]++;
		d[c / 100]++;

		for (int i = 1; i <= 9; ++i)
		{
			if (d[i] == 1)
				flag++;
		}
		if (flag == 9)
			cout << a << " " << b << " " << c<<endl;
	}
}

第三章:数组和字符串

  1. 蛇形填数
/*好像是一道面试题,我感觉还挺重要的*/
#include <iostream>
#include <iomanip>
using namespace std;

int a[50][50]{};

int main()
{
	int N;
	int x, y;
	int res = 1;
	cin >> N;
	a[x=0][y=N - 1] = 1;
	while (res < N * N)
	{
		while (x + 1 < N && !a[x + 1][y]) a[++x][y] = ++res;
		while (y - 1 >= 0 && !a[x][y-1]) a[x][--y] = ++res;
		while (x - 1 >= 0 && !a[x-1][y]) a[--x][y] = ++res;
		while (y + 1 < N && !a[x][y+1]) a[x][++y] = ++res;
	}
	for (x = 0; x < N; x++)
	{
		for (y = 0; y < N; y++)
		{
			printf("%3d",a[x][y]);
		}
		printf("\n");
	}
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值