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

查找元素

找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>
#include <cstring>
#include <cmath>
using namespace std;

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

日期处理

日期差值

// ConsoleApplication1.cpp: 定义控制台-应用程序的入口点。
//
#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

void change(int* p, int* q)
{
	if (*p >= *q)
	{
		int temp;
		temp = *p;
		*p = *q;
		*q = temp;
	}
}


int Isleap(int year)
{
	if ((year % 400 == 0)|| (year % 4 == 0 && year % 100 != 0))
	{
		return 1;
	}
	else
		return 0;
}

int main()
{
	int time1, time2, sum = 1;
	int tempyear;
	while (scanf("%d%d", &time1, &time2) != EOF)
	{
		int *p = &time1, *q = &time2;
		change(p, q);
		int year1, year2, month1, month2, day1, day2;
		year1 = floor(time1 / 10000);
		year2 = floor(time2 / 10000);
		month1 = time1 % 10000 / 100;
		month2 = time2 % 10000 / 100;
		day1 = time1 % 100;
		day2 = time2 % 100;
		//int monthday[2][13] =
		//{ 0,0,31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31 };//上一行为平年,下一行为闰年
		int monthday[2][13] =
		{ 0,31,27,31,30,31,30,31,31,30,31,30,31,0,31,28,31,30,31,30,31,31,30,31,31,30 };
		while (year1 < year2-1)
		{
			if (Isleap(year1) == 1)
			{
				sum += 366;
				year1++;
			}
			else
			{
				year1++;
				sum += 365;
			}
		}
		while (month1 <= 12)
		{
			sum += monthday[Isleap(year1)][month1];
			month1++;
			if (month1 == 13)
			{
				month1 = 1;
				year1++;
				break;
			}
		}
		while (year1<year2||month1<month2||day1 < day2)
		{
			sum++;
			day1++;
			if (day1 == monthday[Isleap(year1)][month1])
			{
				day1 = 1;
				month1++;
			}
			if (month1 == 13)
			{
				month1 = 1;
				year1++;
			}
		}
		printf("%d", sum);
	}
	return 0;
}

!!!!注意!以上代码至少是时间超限的(还有可能错误)。累了==
讨论版有个方法是吧所有的单位设置为日期,是个好想法
试试看:

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

void change(int* p, int* q)
{
	if (*p >= *q)
	{
		int temp;
		temp = *p;
		*p = *q;
		*q = temp;
	}
}


int Isleap(int year)
{
	if ((year % 400 == 0)|| (year % 4 == 0 && year % 100 != 0))
	{
		return 1;
	}
	else
		return 0;
}

int main()
{
	int time1, time2, sum = 1;
	int tempyear;
	while (scanf("%d%d", &time1, &time2) != EOF)
	{
		int *p = &time1, *q = &time2;
		change(p, q);
		int year1, year2, month1, month2, day1, day2;
		year1 = floor(time1 / 10000);
		year2 = floor(time2 / 10000);
		month1 = time1 % 10000 / 100;
		month2 = time2 % 10000 / 100;
		day1 = time1 % 100;
		day2 = time2 % 100;
		//int monthday[2][13] =
		//{ 0,0,31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31 };//上一行为平年,下一行为闰年
		int monthday[2][13] =
		{ 0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29	,31,30,31,30,31,31,30,31,31,30 };
		int turn1 = 0, turn2 = 0;
		for (int month = 1; month < month1; month++)
		{
			turn1 += monthday[Isleap(year1)][month];
		}
		int k = 1;
		for (int day = 1; day <= day1; day++)
		{
			turn1++;
		}
		k = 2;
		for (int year = year1; year < year2; year++)
		{
			if (Isleap(year) == 1)
				turn2 += 366;
			else
				turn2 += 365;
		}
		k = 3;
		for (int month = 1; month < month2; month++)
		{
			turn2 += monthday[Isleap(year2)][month];
		}
		k = 4;
		for (int day = 1; day <= day2; day++)
		{
			turn2++;
		}
		k = 5;
		sum = turn2 - turn1 + 1;
		printf("%d\n", sum);
	}
	return 0;
}

我踏马终于做对了

Day of Week

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
输入
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
输出
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

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

int Isleap(int year)
{
	if ((year % 400 == 0)|| (year % 4 == 0 && year % 100 != 0))
	{
		return 1;
	}
	else
		return 0;
}

int main()
{
	int time1, turn=0;
	int year1, month1, day1;
	char monthc[15];
	while (scanf("%d %s %d", &day1,monthc,&year1) != EOF)
	{
		turn = 0;
		if (strcmp(monthc, "January") == 0)	month1 = 1;
		if (strcmp(monthc, "February") == 0)	month1 = 2;
		if (strcmp(monthc, "March") == 0)	month1 = 3;
		if (strcmp(monthc, "April") == 0)	month1 = 4;
		if (strcmp(monthc, "May") == 0)	month1 = 5;
		if (strcmp(monthc, "June") == 0)	month1 = 6;
		if (strcmp(monthc, "July") == 0)	month1 = 7;
		if (strcmp(monthc, "August") == 0)	month1 = 8;
		if (strcmp(monthc, "September") == 0)	month1 = 9;
		if (strcmp(monthc, "October") == 0)	month1 = 10;
		if (strcmp(monthc, "November") == 0)	month1 = 11;
		if (strcmp(monthc, "December") == 0)	month1 = 12;
		int monthday[2][13] =
		{ 0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,30,31 };
		for (int year = 1000; year < year1; year++)
		{
			if (Isleap(year) == 1)
				turn += 366;
			else
				turn += 365;
		}
		for (int month = 1; month < month1; month++)
		{
			turn += monthday[Isleap(year1)][month];
		}
		turn += day1;
		turn = (turn-1) % 7;
		switch (turn)
		{
		case 0:
		{
			printf("Wednesday\n");
			break;
		}
		case 1:
		{
			printf("Thursday\n");
			break;
		}
		case 2:
		{
			printf("Friday\n");
			break;
		}
		case 3:
		{
			printf("Saturday\n");
			break;
		}
		case 4:
		{
			printf("Sunday\n");
			break;
		}
		case 5:
		{
			printf("Monday\n");
			break;
		}
		case 6:
		{
			printf("Tuesday\n");
			break;
		}
		}
	}
	return 0;
}

拓展:有一个公式叫基姆拉尔森计算公式,可以通过日期判断是星期几。

打印日期

题目描述
给出年分m和一年中的第n天,算出第n天是几月几号。
输入
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。
输出
可能有多组测试数据,对于每组数据,按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

	#include <cstdio>
	#include <cstring>
	#include <cmath>

	using namespace std;

	int Isleap(int year)
	{
		if ((year % 400 == 0)|| (year % 4 == 0 && year % 100 != 0))
		{
			return 1;
		}
		else
			return 0;
	}

	int main()
	{
		int time1, turn ,sum;
		int year1, month1, day1;
		while (scanf("%d %d", &year1, &turn) != EOF)
		{
			sum = 0;
			int monthday[2][13] =
			{ 0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,30,31 };
			for (int month = 1; month <= 12; month++)
			{
				sum += monthday[Isleap(year1)][month];
				int k = 1;
				if (sum > turn)
				{
					sum -= monthday[Isleap(year1)][month];
					month1 = month;
					day1 = turn - sum;
					printf("%04d-%02d-%02d\n", year1, month1, day1);
					break;
				}
				if (sum == turn)
				{
					day1 = monthday[Isleap(year1)][month];
					month1 = month--;
					printf("%04d-%02d-%02d\n", year1, month1, day1);
					break;
				}
			}

		}
		return 0;
	}

日期类

题目描述
编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作。
输入
输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日。测试数据不会有闰年。
输出
输出m行。按xxxx-xx-xx的格式输出,表示输入日期的后一天的日期。

#include <cstdio>
	#include <cstring>
	#include <cmath>

	using namespace std;

	int Isleap(int year)
	{
		if ((year % 400 == 0)|| (year % 4 == 0 && year % 100 != 0))
		{
			return 1;
		}
		else
			return 0;
	}

	int main()
	{
		int year1, month1, day1;
		int monthday[2][13] =
		{ 0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,30,31 };
		int m;
		while (scanf("%d", &m) != EOF)
		{
			while (m--)
			{
				scanf("%d %d %d", &year1, &month1, &day1);
				day1++;
				if (day1 == monthday[Isleap(year1)][month1]+1)
				{
					day1 = 1;
					month1++;
				}
				if (month1 == 12)
				{
					month1 = 1;
					year1++;
				}
				printf("%04d-%02d-%02d\n", year1, month1, day1);
			}
		}
		return 0;
	}

日期累加

题目描述
设计一个程序能计算一个日期加上若干天后是什么日期。
输入
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
输出
输出m行,每行按yyyy-mm-dd的个数输出。

	#include <cstdio>
	#include <cstring>
	#include <cmath>

	using namespace std;

	int Isleap(int year)
	{
		if ((year % 400 == 0)|| (year % 4 == 0 && year % 100 != 0))
		{
			return 1;
		}
		else
			return 0;
	}

	int main()
	{
		int year1, month1, day1,turn;
		int monthday[2][13] =
		{ 0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,30,31 };
		int m;
		while (scanf("%d", &m) != EOF)
		{
			while (m--)
			{
				scanf("%d %d %d %d", &year1, &month1, &day1,&turn);
				while(turn--)
				{
					day1++;
					if (day1 == monthday[Isleap(year1)][month1] + 1)
					{
						day1 = 1;
						month1++;
					}
					if (month1 == 12+1)
					{
						month1 = 1;
						year1++;
					}
					if (turn == 0)
					{
						printf("%04d-%02d-%02d\n", year1, month1, day1);
						break;
					}
				}
			}
		}
		return 0;
	}

进制转换

两种不同进制进行转换

#include <cstdio>
	#include <cstring>
	#include <cmath>

	using namespace std;

	int mtop(int m , int a)
	{
		int p = 0,product=1;
		while (a != 0)
		{
			p += (a % 10)*product;
			a /= 10;
			product *= m;
			int k = 1;
		}
		return p;
	}

	int pton(int n, int temp, int *p0)
	{
		int i = 0;
		do 
		{
			*p0 =temp % n;
			temp /= n;
			p0++;
			i++;
		} while (temp != 0);
		return i;
	}

	int main()
	{
		int a,temp,m,n,len;
		int b[50] = { 0 };
		int* p0=b;
		scanf("%d%d", &m, &n);
		scanf("%d", &a);
		temp=mtop(m,a);
		printf("%d\n", temp);
		len=pton(n,temp,p0);
		for (int i = len-1; i >= 0; i--)
		{
			printf("%d", b[i]);
		}
		printf("\n");
		return 0;
	}

D进制的A+B

输入非负十进制整数AB以及D(进制数),输出A+B的D进制结果

	#include <cstdio>
	#include <cstring>
	#include <cmath>

using namespace std;

int mtop(int m, int a)
{
	int p = 0, product = 1;
	while (a != 0)
	{
		p += (a % 10)*product;
		a /= 10;
		product *= m;
		int k = 1;
	}
	return p;
}

int pton(int n, int temp, int *p0)
{
	int i = 0;
	do
	{
		*p0 = temp % n;
		temp /= n;
		p0++;
		i++;
	} while (temp != 0);
	return i;
}

int main()
{
	int num1, num2, N,len;
	scanf("%d%d%d", &num1, &num2, &N);
	int b[50] = { 0 };
	int* p0 = b;
	int sum, turnsum;
	sum = num1 + num2;
	len = pton(N, sum, p0);
	for (int i = len - 1; i >= 0; i--)
	{
		printf("%d", b[i]);
	}
	printf("\n");
	return 0;
}

又一版 A+B

题目描述
输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。
输入
输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。
输出
输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
提示
注意输入的两个数相加后的结果可能会超过int和long的范围。

	#include <cstdio>
	#include <cstring>
	#include <cmath>

using namespace std;

int mtop(int m, int a)
{
	int p = 0, product = 1;
	while (a != 0)
	{
		p += (a % 10)*product;
		a /= 10;
		product *= m;
		int k = 1;
	}
	return p;
}

int pton(int n, long long temp, int *p0)
{
	int i = 0;
	do
	{
		*p0 = temp % n;
		temp /= n;
		p0++;
		i++;
	} while (temp != 0);
	return i;
}

int main()
{
	int N;
	while (scanf("%d", &N),N != 0)
	{
		long long num1, num2, len;
		scanf("%lld%lld",&num1, &num2 );
		int b[5000] = { 0 };
		int* p0 = b;
		long long sum, turnsum;
		sum = num1 + num2;
		len = pton(N, sum, p0);
		for (int i = len - 1; i >= 0; i--)
		{
			printf("%d", b[i]);
		}
		printf("\n");
	}
	return 0;
}

** 数制转换**

题目描述
求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。
不同进制的表示符号为(0,1,…,9,a,b,…,f)或者(0,1,…,9,A,B,…,F)。
输入
输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。a,b是十进制整数,2 =< a,b <= 16。

	#include <cstdio>
	#include <cstring>
	#include <cmath>

using namespace std;

int main()
{
	int a, b;
	char str[500];
	long long n;
	while (scanf("%d%s%d", &a, str, &b) != EOF)
	{
		//转换成十进制
		//  转换成int类型
		int lenth_of_a = strlen(str);
		int p = 0;
		int product = 1;
		for (int i = lenth_of_a-1; i >= 0; i--)
		{
			switch (str[i])
			{
			case '0': {p += 0 * product; break; }
			case '1': {p += 1 * product; break; }
			case '2': {p += 2 * product; break; }
			case '3': {p += 3 * product; break; }
			case '4': {p += 4 * product; break; }
			case '5': {p += 5 * product; break; }
			case '6': {p += 6 * product; break; }
			case '7': {p += 7 * product; break; }
			case '8': {p += 8 * product; break; }
			case '9': {p += 9 * product; break; }
			case 'a': {p += 10 * product; break; }
			case 'A': {p += 10 * product; break; }
			case 'b': {p += 11 * product; break; }
			case 'B': {p += 11 * product; break; }
			case 'c': {p += 12 * product; break; }
			case 'C': {p += 12 * product; break; }
			case 'd': {p += 13 * product; break; }
			case 'D': {p += 13 * product; break; }
			case 'e': {p += 14 * product; break; }
			case 'E': {p += 14 * product; break; }
			case 'f': {p += 15 * product; break; }
			case 'F': {p += 15 * product; break; }
			default:
				break;
			}
			product *= a;
			int rk = 0;
		}
		//转换成b进制
		char output[500];
		int i = 0;
		int temp;
		do {
			temp = p % b;
			switch (temp)
			{
			case 0: {output[i] = '0';break; }
			case 1: {output[i] = '1'; break; }
			case 2: {output[i] = '2'; break; }
			case 3: {output[i] = '3';break; }
			case 4: {output[i] = '4';  break; }
			case 5: {output[i] = '5'; break; }
			case 6: {output[i] = '6'; break; }
			case 7: {output[i] = '7'; break; }
			case 8: {output[i] = '8'; break; }
			case 9: {output[i] = '9'; break; }
			case 10: {output[i] = 'A'; break; }
			case 11: {output[i] = 'B'; break; }
			case 12: {output[i] = 'C'; break; }
			case 13: {output[i] = 'D'; break; }
			case 14: {output[i] = 'E'; break; }
			case 15: {output[i] = 'F'; break; }
			default:
				break;
			}
			p/= b;
			i++;
		} while (p != 0);
		for (int j = i - 1; j >= 0; j--)
		{
			printf("%c", output[j]);
		}
		printf("\n");
	}
	return 0;
}

进制转换

题目描述
将一个长度最多为30位数字的十进制非负整数转换为二进制数输出。
输入
多组数据,每行为一个长度不超过30位的十进制非负整数。
(注意是10进制数字的个数可能有30个,而非30bits的整数)
输出
每行输出对应的二进制数。

  • 遇到问题

????????????????

八进制

题目描述
输入一个整数,将其转换成八进制数输出。
输入
输入包括一个整数N(0<=N<=100000)。
输出
可能有多组测试数据,对于每组数据,
输出N的八进制表示数。

	#include <cstdio>
	#include <cstring>
	#include <cmath>

using namespace std;

int main()
{
	long long p;
	while (scanf("%lld", &p) != EOF)
	{

		char output[500];
		int i = 0;
		int temp;
		do {
			temp = p % 8;
			switch (temp)
			{
			case 0: {output[i] = '0'; break; }
			case 1: {output[i] = '1'; break; }
			case 2: {output[i] = '2'; break; }
			case 3: {output[i] = '3'; break; }
			case 4: {output[i] = '4';  break; }
			case 5: {output[i] = '5'; break; }
			case 6: {output[i] = '6'; break; }
			case 7: {output[i] = '7'; break; }
			case 8: {output[i] = '8'; break; }
			default:
				break;
			}
			p /= 8;
			i++;
		} while (p != 0);
		for (int j = i - 1; j >= 0; j--)
		{
			printf("%c", output[j]);
		}
		printf("\n");
	}
	return 0;
}

【字符串】回文串

题目描述
读入一串字符,判断是否是回文串。“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。
输入
一行字符串,长度不超过255。
输出
如果是回文串,输出“YES”,否则输出“NO”。

#include <cstdio>
#include <cstring>
#include <cmath>
const int maxn = 256;
using namespace std;

bool judge(char str[])
{
	int len = strlen(str);
	for (int i = 0; i < len / 2; i++)
	{
		if (str[i] != str[len - i - 1])	return false;
	}
	return true;
}

int main()
{
	char str[maxn];
	//while (gets(str))
	scanf("%s", str);

		bool flag = judge(str);
		if (flag == true)
			printf("YES\n");
		else
			printf("NO\n");

	return 0;
}

单词倒序输出

#include <cstdio>
#include <cstring>
#include <cmath>
const int maxn = 3000;
using namespace std;

int main()
{
	int num = 0;
	char ans[90][90];
	/*while (scanf("%s", ans[num] )!= EOF)
	{
		num++;
	}*/
	char str[90];
	fgets(str,90,stdin);
	int len = strlen(str), r = 0, h = 0;
	for (int i = 0; i <= len; i++)
	{
		if(str[i]!='\n')
		{
			if (str[i] != ' ')
			{
				ans[r][h++] = str[i];
			}
			else
			{
				ans[r][h] = '\0';
				r++;
				h = 0;
			}
		}
	}
	for (int i = r; i >=0 ; i--)
	{
		printf("%s", ans[i]);
		if (i > 0)
			printf(" ");
	}
	return 0;
}

注意题目中的’\n’和’\0’
字符串连接

题目描述
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输入
每一行包括两个字符串,长度不超过100。
输出
可能有多组测试数据,对于每组数据,
不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来。
输出连接后的字符串。

#include <cstdio>
#include <cstring>
#include <cmath>
//const int maxn = 200;
using namespace std;

int main()
{
	char str1[200], str2[100];
	char str3[100];
	int len, len1, len2;
	while (scanf("%s%s", str1, str2) != EOF)
	{
		len1 = strlen(str1);
		len2 = strlen(str2);
		for (int i = len1; i < len1+len2; i++)
		{
			str1[i] = str2[i - len1];
			int k = 0;
		}
		str1[len1 + len2] = '\0';
		printf("%s\n", str1);
	}
}

首字母大写

题目描述
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。
在字符串中,单词之间通过空白符分隔,空白符包括:空格(’ ‘)、制表符(’\t’)、回车符(’\r’)、换行符(’\n’)。
输入
输入一行:待处理的字符串(长度小于100)。
输出
可能有多组测试数据,对于每组数据,
输出一行:转换后的字符串。

#include <cstdio>
#include <cstring>
#include <cmath>
//const int maxn = 200;
using namespace std;

int main()
{
	char str[105];
	int len;
	while (fgets(str,110,stdin)!=NULL)
	{
		len = strlen(str);
		for (int i = 0; i < len; i++)
		{
			if (i == 0&&str[i]>=97&&str[i]<=122)
			{
				str[i] -= 32;//要不要加单引号?
			}
			if (str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n')
			{
				if (str[i + 1] >=97 && str[i + 1] <= 122)
					str[i + 1] = str[i + 1] - 32;
			}
		}
		printf("%s", str);
	}
	return 0;
}

字符串的查找删除

  • 答案错误????
#include <cstdio>
#include <cstring>
#include <cmath>
//const int maxn = 200;
using namespace std;

int judge1(char str[], char text[], int i, int len1)
{
	for (int j = 0; j < len1; j++)
	{
		char qq= str[j];
		char qqr = text[i];
		if (str[j] == text[i] || (str[j] <= 122 && str[j] >= 97 && ((str[j] - 32) == text[i])) || (str[j] <= 90 && str[j] >= 65 && ((str[j] + 32) == text[i])))
		{
			int nothing=5;
			i++;
		}
		else
			return 0;
	}
	int ee = 5;
	return 1;
}

int main()
{
	char str[500];
	char text[5000];
	scanf("%s", str);
	getchar();
	while (fgets(text, 5000, stdin) != NULL)
	{
		int len0 = strlen(text);
		int len1 = strlen(str);
		for (int i = 0; i < len0 - len1 + 1; i++)
		{
			int flag = judge1(str, text, i, len1);
			if (flag)
			{
				i = i + len1-1;
				int k = 0;
			}
			if (flag==0&&text[i] != ' ')
			{
				printf("%c", text[i]);
				int m = 1;
			}
		}
		printf("\n");
	}
	return 0;
}


** 单词替换**

题目描述
输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。
输入
多组数据。每组数据输入包括3行,
第1行是包含多个单词的字符串 s,
第2行是待替换的单词a,(长度<=100)
第3行是a将被替换的单词b。(长度<=100)
s, a, b 最前面和最后面都没有空格。
输出
每个测试数据输出只有 1 行,
将s中所有单词a替换成b之后的字符串。

  • 不知道错在哪
#include <cstdio>
#include <cstring>
#include <cmath>
//const int maxn = 200;
using namespace std;

int main()
{
	char str[500];
	char output[500][500];
	char a[110], b[110];
	while (fgets(str,500,stdin)!=NULL)
	{
		scanf("%s\n%s", a,b);//要不要\n?
		int len = strlen(str);
		int r = 0, h = 0;
		for (int i = 0; i < len; i++)
		{
			if (str[i] == ' '||str[i]=='\n')
			{
				output[r][h] = '\0';
				r++;
				h = 0;
			}
			else
			{
				output[r][h] = str[i];
				h++;
			}
		}
		for (int j = 0; j < r; j++)
		{
			if (!strcmp(output[j], a))
			{
				printf("%s ", b);
			}
			else
			{
				printf("%s", output[j]);
				if (j == r - 1)
					printf("\n");
				else printf(" ");
			}
		}
	}
	return 0;
}


字符串去特定字符

题目描述
输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果。
输入
测试数据有多组,每组输入字符串s和字符c。
输出
对于每组输入,输出去除c字符后的结果。

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

int main()
{
	char str[100];
	char c;
	while (fgets(str,100,stdin)!=NULL)
	{
		c = getchar();
		int len = strlen(str);
		for (int i = 0; i < len; i++)
		{
			if (str[i] != c)
				printf("%c",str[i]);
		}
		getchar();
		//printf("\n");
	}
	return 0;
}

这里一定要注意用getchar()来吸收换行符!
输入可能包含空格,为了使字符串不被中断,函数不可以选用scanf,而应该选用fgets(str,buf,stdin);由于fgets()函数会把\n也写入字符数组中,所以在这个程序中,打印完字符串后会跟上\n(居然在\0后面???),在下一次循环时,直接将\n误读入,结束fgets字符串的输入,导致程序结果错误。
数组逆置

题目描述
输入一个字符串,长度小于等于200,然后将数组逆置输出。
输入
测试数据有多组,每组输入一个字符串。
输出
对于每组输入,请输出逆置后的结果。

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

int main()
{
	char str[210];
	int len;
	while (fgets(str,210,stdin)!=NULL)
	{
		len = strlen(str);
		int i;
		for (i = len-1; i >= 0; --i)
		{
			putchar(str[i]);
		}
		printf("\n");
	}
	return 0;
}

** 比较字符串**

题目描述
输入两个字符串,比较两字符串的长度大小关系。
输入
输入第一行表示测试用例的个数m,接下来m行每行两个字符串A和B,字符串长度不超过50。
输出
输出m行。若两字符串长度相等则输出A is equal long to B;若A比B长,则输出A is longer than B;否则输出A is shorter than B。

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

int main()
{
	int m;
	char a[55], b[55];
	int lena, lenb;
	while (scanf("%d", &m) != EOF)
	{
		while (m--)
		{
			scanf("%s%s", a, b);
			lena = strlen(a);
			lenb = strlen(b);
			if (lena == lenb)
				printf("%s is equal long to %s\n", a, b);
			if (lena > lenb)
				printf("%s is longer than %s\n", a, b);
			if (lena < lenb)
				printf("%s is shorter than %s\n", a, b);
		}
	}
	return 0;
}

编排字符串

输入
第一行为字符串个数m,接下来m行每行一个字符床,m不超过100,每个字符床长度不超过20。
输出
输出m行,每行按照样例格式输出,注意用一个空格隔开。
样例输入
5
EricZ
David
Peter
Alan
Jane
样例输出
1=EricZ
1=David 2=EricZ
1=Peter 2=David 3=EricZ
1=Alan 2=Peter 3=David 4=EricZ
1=Jane 2=Alan 3=Peter 4=David

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

int main()
{
	int m;
	char num[110][25];
	while (scanf("%d", &m) != EOF)
	{
		for (int i = 0; i < m; i++)
		{
			scanf("%s", num[i]);
			for (int j = i; j >= 0; j--)
			{
				if (j < i - 3)	break;
				printf("%d=%s", i - j + 1, num[j]);
				if (j == 0||j==i-3)	printf("\n");
				else
				{
					printf(" ");
				}
			}
		}
	}
	return 0;
}
发布了43 篇原创文章 · 获赞 4 · 访问量 1220

猜你喜欢

转载自blog.csdn.net/weixin_42176221/article/details/99813235