6-4 encrypted code (10 minutes) the number of days 6-4 and enter the year, the output of the corresponding year, month, day (15 minutes) 6-4 binary search (15 minutes) 6-24 sorting method used to select the function (25 points )

6-4 encrypted code (10 minutes)

In order to prevent information from being stolen easily others, need to be converted into plaintext code by encryption cipher text. Requested to prepare and call the function encrypt (), as required to modify the string contents. The conversion rule is as follows: z converted into lowercase letters a, the other letters of the alphabet converted into an ASCII code of the sequence of letters, such as o converted into p.

Function interface definition:

void encrypt ( char *s);

Where s is the first string address.

Referee test program Example:

#include <stdio.h>
# include <string.h>
# define MAXLINE 80
void encrypt ( char *s);
int main (void)
{
   char line [MAXLINE];

   gets(line);
   encrypt (line);
   printf ("%s\n", line);
	 return 0;
}

/* 请在这里填写答案 */

Sample input:
adfz
sample output:
Bega

The answer Code:

void encrypt ( char *s)
{
	int n = strlen(s);
	int i;
	for(i = 0;i < n;i++)
	{
		if(s[i] == 'z')
		{
			s[i] = 'a';
		}
		else
		{
			s[i] = s[i] + 1;
		}
	}
}

6-4 Enter the year and the number of days, the output of the corresponding year, month, day (15 points)

Function interface definition:

void month_day ( year, yeardy, *pmonth, *pday);

Which year was in, yearday is the number of days, pMonth and pday is calculated on the month and day.

Referee test program Example:

#include <stdio.h>
void month_day ( int year, int yearday, int * pmonth, int * pday);

int main (void)
{
   int day, month, year, yearday; /*  定义代表日、月、年和天数的变量*/
   scanf ("%d%d", &year, &yearday );		
   month_day (year, yearday, &month, &day );/* 调用计算月、日函数  */ 
   printf ("%d %d %d\n", year, month, day );	
   return 0;	
}

/* 请在这里填写答案 */

Sample input:
200061
Output Sample:
200031

The answer Code:

void month_day ( int year, int yearday, int * pmonth, int * pday)
{
	if((year%4 == 0 && year%100 != 0) || year%400 == 0)
	{
		if(yearday <= 31)
		{
			*pmonth = 1;
			*pday = yearday;
		}
		else if(yearday > 31 && yearday <= 60)
		{
			*pmonth = 2;
			*pday = yearday - 31;
		}
		else if(yearday > 60 && yearday <= 91)
		{
			*pmonth = 3;
			*pday = yearday - 60;
		}
		else if(yearday > 91 && yearday <= 121)
		{
			*pmonth = 4;
			*pday = yearday - 91;
		}
		else if(yearday > 121 && yearday <= 152)
		{
			*pmonth = 5;
			*pday = yearday - 121;
		}
		else if(yearday > 152 && yearday <= 182)
		{
			*pmonth = 6;
			*pday = yearday - 152;
		}
		else if(yearday > 182 && yearday <= 213)
		{
			*pmonth = 7;
			*pday = yearday - 182;
		}
		else if(yearday > 213 && yearday <= 244)
		{
			*pmonth = 8;
			*pday = yearday - 213;
		}
		else if(yearday > 244 && yearday <= 274)
		{
			*pmonth = 9;
			*pday = yearday - 244;
		}
		else if(yearday > 274 && yearday <= 305)
		{
			*pmonth = 10;
			*pday = yearday - 274;
		}
		else if(yearday > 305 && yearday <= 335)
		{
			*pmonth = 11;
			*pday = yearday - 305;
		}
		else if(yearday > 335 && yearday <= 366)
		{
			*pmonth = 12;
			*pday = yearday - 335;
		}	
	}
	else
	{
		if(yearday <= 31)
		{
			*pmonth = 1;
			*pday = yearday;
		}
		else if(yearday > 31 && yearday <= 59)
		{
			*pmonth = 2;
			*pday = yearday - 31;
		}
		else if(yearday > 59 && yearday <= 90)
		{
			*pmonth = 3;
			*pday = yearday - 59;
		}
		else if(yearday > 90 && yearday <= 120)
		{
			*pmonth = 4;
			*pday = yearday - 90;
		}
		else if(yearday > 120 && yearday <= 151)
		{
			*pmonth = 5;
			*pday = yearday - 120;
		}
		else if(yearday > 151 && yearday <= 181)
		{
			*pmonth = 6;
			*pday = yearday - 151;
		}
		else if(yearday > 181 && yearday <= 212)
		{
			*pmonth = 7;
			*pday = yearday - 181;
		}
		else if(yearday > 212 && yearday <= 243)
		{
			*pmonth = 8;
			*pday = yearday - 212;
		}
		else if(yearday > 243 && yearday <= 273)
		{
			*pmonth = 9;
			*pday = yearday - 243;
		}
		else if(yearday > 273 && yearday <= 304)
		{
			*pmonth = 10;
			*pday = yearday - 273;
		}
		else if(yearday > 304 && yearday <= 334)
		{
			*pmonth = 11;
			*pday = yearday - 304;
		}
		else if(yearday > 334 && yearday <= 365)
		{
			*pmonth = 12;
			*pday = yearday - 334;
		}	
	}
}

6-4 binary search (15 points)

The array has a shaping element 10 a, and by value in ascending order. Enter an integer x, then look for x in the array, if found, the corresponding output index, otherwise, output "Not Found". . It requires writing functions int Bsearch (int * p, int n, int x), return to find the index can not find -1.

Function interface definition:

int Bsearch(int *p, int n, int x);

Wherein p is the address of the first array, n being the number of array elements, x is a value to be found. Find return index, can not find returns -1.

Referee test program Example:

#include<stdio.h>
int Bsearch(int *p, int n, int x);     
int main(void)   
{
    int a[10] = {1,2,3,4,5,6,7,8,9,10};    
    int x, m;
    scanf("%d",&x);                
    m = Bsearch(a, 10, x);
    if(m >= 0)   
        printf("Index is %d\n",m);
    else 
        printf( "Not Found\n");
		
    return 0;
}

/* 请在这里填写答案 */

Sample input:
8
Output Sample:
Index IS. 7

The answer Code:

int Bsearch(int *p, int n, int x)
{
	int high = n-1;
	int low = 0;
	int mid = (low+high)/2;
    if(p[high] < x)
    return -1;
    if(p[low] > x)
    return -1;
	while(p[mid] != x)
	{
		if(p[mid] > x)
		{
			high = mid;
		}
		else
		{
			low = mid;
		}
		mid = (low+high)/2;
		if(high == low)
		return -1;
	}
	return mid;
}  

6-24 sorting method using the function selection (25 points)

This problem required to achieve a method with the selected simple integer array sorting function.

Function interface definition:

void sort( int a[], int n );

Wherein a is the array to be sorted, n being a number of array elements. The function of the array elements in a ascending order by using selection method, the results are still in the array a.

Referee test program Example:

#include <stdio.h>
#define MAXN 10

void sort( int a[], int n );

int main()
{
    int i, n;
    int a[MAXN];

    scanf("%d", &n);
    for( i=0; i<n; i++ )
        scanf("%d", &a[i]);

    sort(a, n);

    printf("After sorted the array is:");
    for( i = 0; i < n; i++ )
        printf(" %d", a[i]);
    printf("\n");

    return 0;
}

/* 你的代码将被嵌在这里 */

. 4
. 5. 1. 7. 6
Output Sample:
the After the sorted Array The IS:. 1. 5. 6. 7

The answer Code:

void sort( int a[], int n )
{
	int i;
	int j;
	int flag;
	for(i = 0;i < n;i++)
	{
		flag = i;
		for(j = i;j < n;j++)
		{
			if(a[j] < a[flag])
			{
				flag = j;
			}
		}
		int t = a[i];
		a[i] = a[flag];
		a[flag] = t;
	}
}
Published 13 original articles · won praise 11 · views 474

Guess you like

Origin blog.csdn.net/qq_45811966/article/details/103652690