【数据结构与算法】学习笔记-《算法笔记》-7

这篇博客探讨了数据结构与算法的学习,重点介绍了查找元素的相关问题,包括统计同成绩学生人数、查找学生信息和查找算法的应用。此外,还提到了与奥巴马一起编程的思路,以及涉及日期处理、进制转换的编程挑战。
摘要由CSDN通过智能技术生成

查找元素

找x

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n;
	int a[200];
	while (scanf("%d", &n) != EOF)
	{
		int flag = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
		}
		int x;
		scanf("%d", &x);
		for (int i = 0; i < n; i++)
		{
			if (x == a[i])
			{
				printf("%d\n", i);
				flag = 1;
			}
		}
		if (!flag)
			printf("-1\n");
	}
	return 0;
}

用的是遍历的方法。
统计同成绩学生人数

题目描述
读入N名学生的成绩,将获得某一给定分数的学生人数输出。
输入
测试输入包含若干测试用例,每个测试用例的格式为
第1行:N
第2行:N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数
当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。
输出
对每个测试用例,将获得给定分数的学生人数输出。

查找学生信息

题目描述
输入N个学生的信息,然后进行查询。
输入
输入的第一行为N,即学生的个数(N<=1000)
接下来的N行包括N个学生的信息,信息格式如下:
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:
02
03
01
04
输出
输出M行,每行包括一个对应于查询的学生的信息。
如果没有对应的学生信息,则输出“No Answer!”

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n,m;
	struct stu
	{
		char num[8];
		char name[100];
		char sex[20];
		int age;
	} student[1000]; 
	//char search[10000][3];
	char search[8];
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%s %s %s %d", student[i].num, student[i].name,student[i].sex,&student[i].age);
		}
		scanf("%d", &m);
		for (int i = 0; i < m; i++)
		{
			scanf("%s", &search);
			for (int j = 0; j < n; j++)
			{
				if (strcmp(student[j].num, search)==0)
				{
					printf("%s %s %s %d\n", student[j].num, student[j].name, student[j].sex, student[j].age);
					break;
				}
				if(j==n-1)	printf("No Answer!\n");
			}
		}
	}
	return 0;
}

查找

题目描述
输入数组长度 n
输入数组 a[1…n]
输入查找个数m
输入查找数字b[1…m]
输出 YES or NO 查找有则YES 否则NO 。
输入
输入有多组数据。
每组输入n,然后输入n个整数,再输入m,然后再输入m个整数(1<=m<=n<=100)。
输出
如果在n个数组中输出YES否则输出NO。

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n, m;
	int a[100], b[100];
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
		}
		scanf("%d", &m);
		for (int j = 0; j < m; j++)
		{
			scanf("%d", &b[j]);
			for (int i = 0; i < n; i++)
			{
				if (a[i] == b[j])
				{
					printf("YES\n");
					break;
				}
				if (i == n - 1)
					printf("NO\n");
			}
		}

	}
	return 0;
}

跟奥巴马一起编程

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n;
	char c;
	scanf("%d %c", &n, &c);
	int m = floor(n / 2);
	for (int i = 0; i < m; i++)
	{
		if (i == 0 || i == m - 1)
		{
			for (int j = 0; j < n; j++)
			{
				printf("%c", c);
				if (j == n - 1)	printf("\n");
			}
		}
		else
		{
			for (int j = 0; j < n; j++)
			{
				if (j == 0)
					printf("%c", c);
				if(j == n - 1)
					printf("%c\n", c);
				if(j!=0&&j!=n-1)
					printf(" ");
			}
		}
	}
	return 0;
}

其中if语句用for循环语句代替可能更好

输出梯形

题目描述
输入一个高度h,输出一个高为h,上底边为h的梯形。
输入
一个整数h(1<=h<=1000)。
输出
h所对应的梯形。

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int h;
	while (scanf("%d", &h) != EOF)
	{
		int n = 3 * h - 2;
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < (n-h-2*(i-0)); j++)
			{
				printf(" ");
			}
			for (int j = (n - h - 2 * (i - 0))+1; j <= n; j++)
			{
				printf("*");
				if (j == n)	printf("\n");
			}
		}
	}
	return 0;
}

Hello World for U

Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, “helloworld” can be printed as:
h d
e l
l r
lowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible – that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	char a[80] = {' '};
	scanf("%s", a);
	int len;
	len = strlen(a);
	int side, mid;
	side = floor((len + 2) / 3);
	mid = len - 2 * side;
	for (int i = 0; i < side-1; i++)
	{
		printf("%c", a[i]);
		for (int j = 0; j < mid; j++)
		{
			printf(" ");
		}
		printf("%c\n", a[len - i - 1]);
	}
	for (int j = 0; j < mid+2; j++)
	{
		printf("%c", a[side - 1+j]);
	}
	printf("\n");
	return 0;
}

** 等腰梯形**

题目描述
请输入高度h,输入一个高为h,上底边长为h 的等腰梯形(例如h=4,图形如下)。
输入
输入第一行表示样例数m,接下来m行每行一个整数h,h不超过10。
输出
对应于m个case输出要求的等腰梯形。

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int n;
	int h;
	scanf("%d", &n);
	while (n--)
	{
		scanf("%d", &h);
		for (int i = 0; i < h; i++)
		{
			for (int j = 0; j < h - i - 1; j++)
			{
				printf(" ");
			}
			for (int j = h - i - 1; j < 2*h+i-1; j++)
			{
				printf("*");
			}
			for (int j = 2*h+i-1; j < 3*h-2; j++)
			{
				printf(" ");
			}
			printf("\n");
		}
	}
	return 0;
}

沙漏图形 tri2str [1 * +]

问题:输入n,输出正倒n层星号三角形。首行顶格,星号间有一空格,效果见样例 
输入样例: 
3 
输出样例:
* * *
 * * 
  *
 * * 
* * *
数据规模 1<= n <=50 

#include <cstdio&g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值