编程语言
首页 > 编程语言> > Python123第八章答案

Python123第八章答案

作者:互联网

Python123第八章答案(自写,仅供参考) By:肇院伟烨

前言

1、代码自己写的,不是标准答案,仅供参考。

2、建议看懂后自己另外写而不是复制粘贴。

3、点击目录可以转跳

文章目录

第八章

第1题

#include <stdio.h>
#include<string.h>
#define MAX_LEN 100
#define LEN 5
char str[LEN][MAX_LEN];
char* order[LEN] = {0};
int size=0 , end_number;
void insert(int i)
{
	if (i == 0)
	{
		order[i] = str[i];
		size++;
	}
	else
	{
		end_number = size - 1;
		while (end_number >= 0 && strcmp(order[end_number], str[i]) > 0)
		{
			order[end_number + 1] = order[end_number];
			end_number--;
		}
		order[end_number + 1] = str[i];
		size++;
	}
}
int main()
{
	int i, b;
	for (i = 0; i <5; i++)
	{
		scanf("%s", str[i]);
		insert(i);
	}
	printf("After sorted:\n");
	for (i = 0; i <5; i++)
	{
		printf(order[i]);
		if (i != 4)
		{
			putchar('\n');
		}
	}

	return 0;
}

第2题

#include <stdio.h>
#include<stdlib.h>
int main()
{
	float* p;
	int i, n;
	scanf("%d", &n);
	p = (float*)malloc(n * sizeof(float));
	if (p == NULL)
		exit(1);
		
	float sum = 0, average;
	int max = 0, min = 0;
	for (i = 0; i < n; i++)
	{
		scanf("%f", &p[i]);
		sum += p[i];
		if (p[i] > p[max])
			max = i;
		if (p[i] < p[min])
			min = i;
	}
	average = sum / n;
	printf("average = %.2f\nmax = %.2f\nmin = %.2f\n", average, p[max], p[min]);
	free(p);
	p = NULL;
	return 0;
}

第3题

#include <stdio.h>
void sum_diff(float op1, float op2, float* psum, float* pdiff);

int main()
{
    float a, b, sum, diff;

    scanf("%f %f", &a, &b);
    sum_diff(a, b, &sum, &diff);
    printf("The sum is %.2f\nThe diff is %.2f\n", sum, diff);

    return 0;
}
void sum_diff(float op1, float op2, float* psum, float* pdiff)
{
    *psum = op1 + op2;
    *pdiff = op1 - op2;
}

第4题

#include <stdio.h>
#include <string.h>
int main()
{
	int i,n;
    char str[100],a,b,c;
    gets(str);
    a=str[0];
    b=str[1];
    c=str[2];
    for(i=0;str[i+3]!='\0';i++)
    {
        str[i]=str[i+3];
    }
    str[i]=a;
    str[i+1]=b;
    str[i+2]=c;
    str[i+3]='\0';
    puts(str);
	return 0;
}

第5题

#include <stdio.h>

void splitfloat( double x, int *intpart, double *fracpart );

int main()
{
    double x, fracpart;
    int intpart;

    scanf("%lf", &x);
    splitfloat(x, &intpart, &fracpart);
    printf("The integer part is %d\n", intpart);
    printf("The fractional part is %g\n", fracpart);

    return 0;
}

void splitfloat( double x, int *intpart, double *fracpart )
{
    *intpart=x;
    *fracpart=x-*intpart;
}

第6题

#include<stdio.h>
#define MAX 100
int arr[MAX];
int search(int list[], int n, int x);
int main()
{
    int i,n,number, x;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    scanf("%d", &x);
    if (search(arr, n, x) == -1)
        printf("Not found");
    else
        printf("index = %d", search(arr, n, x));
    return 0;
}
int search(int list[], int n, int x)
{
    int i;
    for (i = 0; i < n; i++)
        if (list[i] == x)
        {
            return i;
            break;
        }
    if (i == n)
        return -1;

}

第7题

#include <stdio.h>
#define MAXN 10

int ArrayShift(int a[], int n, int m);

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

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

    ArrayShift(a, n, m);

    for (i = 0; i < n; i++) 
    {
        if (i != 0) 
            printf(" ");
        printf("%d", a[i]);
    }
    printf("\n");

    return 0;
}
int ArrayShift(int a[], int n, int m)
{
    int swap,i,j;
    for (i = 1; i <= m; i++)
    {
        swap = a[n - 1];
        for (j = n - 1; j > 0; j--)
            a[j] = a[j-1];
        a[0] = swap;
    }
    return 0;
}

第8题

#include <stdio.h>
#define MAXN 20

void CountOff(int n, int m, int out[]);

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

    scanf("%d %d", &n, &m);
    CountOff(n, m, out);
    for (i = 0; i < n; i++)
        printf("%d ", out[i]);
    printf("\n");

    return 0;
}
void CountOff(int n, int m, int out[])
{
    int i = 0, j = 1, count = 0;
    for (i = 0; i < n; i++)
    {
        out[i] = 0;
    }
    i = 0;
    while (j <= n)
    {
        for (i = 1; i <= n; i++)
        {
            if (out[i - 1] == 0)
            {
                count++;
            }
            if (out[i - 1] == 0 && count == m)
            {
                out[i - 1] = j;
                j++;
                count = 0;
            }
        }
    }
}

第9题

#include <stdio.h>
#define MAXN 100

void strmcpy( char *t, int m, char *s );
void ReadString( char s[] );

int main()
{
    char t[MAXN], s[MAXN];
    int m;

    scanf("%d\n", &m);
    ReadString(t);
    strmcpy( t, m, s );
    printf("%s\n", s);

    return 0;
}
void ReadString( char s[] )
{
    gets(s);
}
void strmcpy( char *t, int m, char *s )
{
    int i,len=0,j=0;
    for(i=0;t[i]!='\0';i++)
    {
        len++;
    }
    if(m>len)
    {
        s[0]='\0';
    }
    else
    {
        for(i=m-1;i<len;i++)
        {
            s[j]=t[i];
            j++;
        }
        s[j]='\0';
    }
    
}

第10题

#include <stdio.h>
#include <string.h>
#define MAXN 20

void delchar( char *str, char c );
void ReadString( char s[] );

int main()
{
    char str[MAXN], c;

    scanf("%c\n", &c);
    ReadString(str);
    delchar(str, c);
    printf("%s\n", str);

    return 0;
}
void ReadString( char s[] )
{gets(s);}

void delchar( char *str, char c )
{
    int i,j=0;
    char str1[MAXN];
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]!=c)
        {
            str1[j]=str[i];
            j++;
        }
        
    }
    str1[j]='\0';
    strcpy(str,str1);
}

第11题

#include <stdio.h>
#include <string.h>
#define MAXN 20
typedef enum {
false, true}
bool;
bool palindrome( char *s );
int main()
{
    char s[MAXN];
    scanf("%s", s);
    if ( palindrome(s)==true )
    printf("Yes\n");
    else
    printf("No\n");
    printf("%s\n", s);
    return 0;
}
bool palindrome( char *s )
{
    int end,star=0;
    end=strlen(s)-1;
    while(1)
    {
        if(s[star]!=s[end])
        return false;
        else
        star++;
        end--;
        if(star>end)
        return true;
    }
}

第12题

#include <stdio.h>
#include<ctype.h>
#define MAXS 15
void StringCount(char* s);
void ReadString(char* s);
int main()
{
    char s[MAXS];
    ReadString(s);
    StringCount(s);
    return 0;
}
void ReadString(char* s)
{
    int i = 0;
    while (scanf("%c", s + i) && s[i] != '~')
    {
        i++;
    }
    s[i] = '~';
    s[i + 1] = '\0';
}
void StringCount(char* s)
{
    int i = 0,upper=0,lower=0,blank=0,digit=0,other=0;
    while (s[i] != '\0')
    {
        if (isupper(s[i]))
            upper++;
        else if (islower(s[i]))
            lower++;
        else if (s[i]==' ')
            blank++;
        else if (isdigit(s[i]))
            digit++;
        else
            other++;
        i++;
    }
    printf("%d %d %d %d %d", upper,lower, blank, digit, other);
}

第13题

#include <stdio.h>
#define MAXN 10

int ArrayShift(int a[], int n, int m);

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

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

    ArrayShift(a, n, m);

    for (i = 0; i < n; i++) 
    {
        if (i != 0) 
            printf(" ");
        printf("%d", a[i]);
    }
    printf("\n");

    return 0;
}
int ArrayShift(int a[], int n, int m)
{
    int swap,i,j;
    for (i = 1; i <= m; i++)
    {
        swap = a[n - 1];
        for (j = n - 1; j > 0; j--)
            a[j] = a[j-1];
        a[0] = swap;
    }
    return 0;
}

第14题

#include <stdio.h>
#include <string.h>
#define MAXN 20
int palindrome( char *s );
int main()
{
    char s[MAXN];
    scanf("%s", s);
    if ( palindrome(s)==1 )
    printf("Yes\n");
    else
    printf("No\n");
    printf("%s\n", s);
    return 0;
}
int palindrome( char *s )
{
    int end,star=0;
    end=strlen(s)-1;
    while(1)
    {
        if(s[star]!=s[end])
            return 0;
        star++;
        end--;
        if(star>end)
            return 1;
    }
}

标签:return,str,int,第八章,char,++,Python123,答案,printf
来源: https://blog.csdn.net/weixin_62861464/article/details/122115683