python10 11章答案

第十章:

第一题:

#include<stdio.h>
double sum(double n)
{
    if (n <= 0)
        return 0;
    if (n == 1)
        return 1;
    else
        return sum(n-1)+n;
}
int main()
{
    double n;

    scanf("%lf", &n);
    printf("%.0lf\n", sum(n));

    return 0;
}

第二题:

#include<stdio.h>
#include<math.h>
int search(int n)
{
    int i, count = 0;
    for (i=101;i<=n;i++)
        if (i == (int)pow((int)sqrt(i), 2))
            if (i / 100 == i % 10 || i / 100 == i / 10 % 10 || i / 10 % 10 == i % 10)
                count++;

    return count;
}
int main()
{
    int number;

    scanf("%d", &number);
    printf("count=%d\n", search(number));

    return 0;
}

 第三题:

#include<stdio.h>
double fact(int n)
{
    if (n == 1 || n <= 0)
        return 1;
    else
        return n * fact(n - 1);
}
double factsum(int n)
{
    double sum=0;
    int i;
    for (i=1;i<=n;i++)
    {
        sum += fact(i);
    }
    return sum;
}
int main()
{
    int n;

    scanf("%d", &n);
    printf("fact(%d) = %.0f\n", n, fact(n));
    printf("sum = %.0f\n", factsum(n));

    return 0;
}

 第四题:

#include<stdio.h>
double calc_pow(double x, int n)
{
    if (x == 0)
        return 0;
    if (n == 0)
        return 1;
    if (n == 1)
        return x;
    else
        return x * calc_pow(x,n-1);
}
int main()
{
    double x;
    int n;

    scanf("%lf %d", &x, &n);
    printf("%.0f\n", calc_pow(x, n));

    return 0;
}

第五题:

#include<stdio.h>
#include<math.h>
double fn(double x, int n)
{
    if (n == 1)
        return x;
    else
        return pow(-1,n+1)*pow(x,n)+fn(x,n-1);
}
int main()
{
    double x;
    int n;

    scanf("%lf %d", &x, &n);
    printf("%.2f\n", fn(x, n));

    return 0;
}

第六题:

#include<stdio.h>
int Ack(int m, int n)
{
    if (m == 0)
        return n + 1;
    else if (n == 0 && m > 0)
        return Ack(m - 1, 1);
    else if (m > 0 && n > 0)
        return Ack(m - 1, Ack(m, n - 1));
}
int main()
{
    int m, n;

    scanf("%d %d", &m, &n);
    printf("%d\n", Ack(m, n));

    return 0;
}

第七题:

#include<stdio.h>
int f(int n)
{
    if (n == 1 || n == 2)
        return 1;
    else
        return f(n - 1) + f(n - 2);
}
int main()
{
    int n;

    scanf("%d", &n);
    printf("%d\n", f(n));

    return 0;
}

第八题:

#include<stdio.h>
void dectobin(int n)
{
    if (n == 0)
        return;
    int a;
    a = n % 2;
    dectobin(n/2);
    printf("%d", a);
}
int main()
{
    int n;

    scanf("%d", &n);
    if (n > 0)
        dectobin(n);
    else
        printf("0");

    return 0;
}

第九题:

#include<stdio.h>
void printdigits(int n)
{
    if (n != 0)
    {
        int x = n;
        printdigits(x /= 10);
        printf("%d\n", n % 10);
    }
}
int main()
{
    int n;

    scanf("%d", &n);
    if (n == 0)
    {
        printf("0");
        return 0;
    }
    if (n < 0)
        n = -n;
    
    printdigits(n);

    return 0;
}

                                                                十一章答案 :

第一题:

#include <stdio.h>
char* getmonth(int n)
{
    if (n >= 1 && n <= 12)
    {
        char* str[] = { "January","February","March","April","May","June","July","August","September","October","November","December" };
        return str[n - 1];
    }
    else
        return NULL;
}
int main()
{
    int n;
    char* s;

    scanf("%d", &n);
    s = getmonth(n);
    if (s == NULL) printf("wrong input!\n");
    else printf("%s\n", s);

    return 0;
}

第二题:

#include <stdio.h>
#include <string.h>
#define MAXS 80
int getindex(char* s)
{
    char* str[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int i;

    for(i=0;i<7;i++)
        if (strcmp(str[i], s) == 0)
            return i;
    return -1;
}
int main()
{
    int n;
    char s[MAXS];

    scanf("%s", s);
    n = getindex(s);
    if (n == -1) printf("wrong input!\n");
    else printf("%d\n", n);

    return 0;
}

第三题:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 10
#define MAXS 20
int max_len(char* s[], int n)
{
    int i,max=0;
    for (i=0;i<n;i++)
        if (strlen(s[i]) > max)
            max = strlen(s[i]);
    return max;
}
int main()
{
    int i, n;
    char* string[MAXN] = { NULL };

    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        string[i] = (char*)malloc(sizeof(char) * MAXS);
        scanf("%s", string[i]);
    }
    printf("%d\n", max_len(string, n));

    return 0;
}

第四题:

#include <stdio.h>
#include <string.h>
#define MAXS 100
char* str_cat(char* s, char* t)
{
    int i,n;
    n = strlen(s);
    for (i=n;t[i-n]!='\0';i++)
        s[i] = t[i-n];
    return s;
}
int main()
{
    char* p;
    char str1[MAXS + MAXS] = { '\0' }, str2[MAXS] = { '\0' };

    scanf("%s%s", str1, str2);
    p = str_cat(str1, str2);
    printf("%s\n%s\n", p, str1);

    return 0;
}

第五题:

#include <stdio.h>
#define MAXS 100
char* match(char* s, char ch1, char ch2)
{
    int i,flag=0;
    char* pc={" "};
    for (i=0;s[i]!='\0';i++)
    {
        if (s[i] == ch1&&flag!=1)
        {
            flag = 1;
            pc = s + i;
        }
        if (s[i] == ch2&&flag==1)
        {
            printf("%c", ch2);
            return pc;
        }
        if (flag == 1)
            printf("%c", s[i]);
    }
    return pc;
}
int main()
{
    char str[MAXS], ch_start, ch_end, * p;

    scanf("%s\n", str);
    scanf("%c %c", &ch_start, &ch_end);
    p = match(str, ch_start, ch_end);
    printf("\n%s\n", p);

    return 0;
}

第六题:

#include <stdio.h>
#define MAXS 30
void ReadString(char s[])
{
    gets(s);
}
char* search(char* s, char* t)
{
    int i,flag=0,j;
    char* pt=NULL;
    j = 0;
    for (i=0;s[i]!='\0';i++)
    {
        if (t[0] == s[i]&&flag==0)
        {
            flag = 1;
            pt = s + i;
        }
        if (flag == 1)
        {
            if (s[i] == t[j] && t[j + 1] == '\0')
                return pt;

            if (s[i] == t[j])
                j++;
            else
            {
                flag = 0;
                pt = NULL;
                j = 0;
            }
        }
    }
}
int main()
{
    char s[MAXS], t[MAXS], * pos;

    ReadString(s);
    ReadString(t);
    pos = search(s, t);
    if (pos != NULL)
        printf("%d\n", pos - s);
    else
        printf("-1\n");

    return 0;
}

第七题:

#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
    int data;
    struct ListNode* next;
};
struct ListNode* readlist()
{
    int a;
    struct ListNode* p,*head=NULL,*q=NULL;
    while (scanf("%d", &a)&&a != -1)
    {
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = a;
        p->next = NULL;

        if (head != NULL)
            q->next = p;
        else
            head = p;

        q = p;
    }
    return (head);
}
struct ListNode* getodd(struct ListNode** L)
{
    struct ListNode* p,*head=NULL,*node=NULL,*temp=NULL,*head2=NULL,*temp2=NULL;
    p = *L;
    while (p!=NULL)
    {
        node = (struct ListNode*)malloc(sizeof(struct ListNode));
        if (p->data % 2 != 0)
        {
            node->data = p->data;
            node->next = NULL;

            if (head == NULL)
                head = node;
            else
                temp->next = node;

            temp = node;
        }
        else
        {
            node->data = p->data;
            node->next = NULL;

            if (head2 == NULL)
                head2 = node;
            else
                temp2->next = node;

            temp2 = node;
        }
        p = p->next;
    }

    (*L) = head2;
    return (head);
}
void printlist(struct ListNode* L)
{
    struct ListNode* p = L;
    while (p)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}
int main()
{
    struct ListNode* L, * Odd;
    L = readlist();
    Odd = getodd(&L);
    printlist(Odd);
    printlist(L);

    return 0;
}

第八题:

#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
    int data;
    struct ListNode* next;
};
struct ListNode* readlist()
{
    struct ListNode* head=NULL,*node,*tempp=NULL;
    int n;
    while (scanf("%d", &n)&&n !=-1)
    {
        node = (struct ListNode*)malloc(sizeof(struct ListNode));
        node->data = n;
        node->next = NULL;

        if (head == NULL)
            head = node;
        else
            tempp->next = node;

        tempp = node;
    }
    return head;
}
struct ListNode* deletem(struct ListNode* L, int m)
{
    struct ListNode* p,*head=NULL,*tempp=NULL;
    head = L;
    while (L!=NULL)
    {
        if (L->data == m)//符合条件
        {
            if (head->data==m)//头节点的删除
                head = head->next;
            else//普通节点
                tempp->next = L->next;
        }
        tempp = L;
        L = L->next;
    }
    return (head);
}
void printlist(struct ListNode* L)
{
    struct ListNode* p = L;
    while (p)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}
int main()
{
    int m;
    struct ListNode* L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值