xjtu程序设计基础作业题第七次

  • 6.10组合数排列数

编写函数f用于计算阶乘(自己定义参数、返回类型)。在主函数输入正整数m、n,调用f函数计算排列数、组合数。
【输入输出要求】
输入:m n(m>n)
输出: A(m,n) C(m,n)
【输入输出样例】
输入:
4 2
输出:
12 6

#include <stdio.h>
#include <stdlib.h>
 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void f(int m,int n){
    int x=1,y=1;
    int i,j;
    for(i=0;i<n;i++)
    {
        x=x*(m-i);
    }
    for(j=0;j<n;j++)
    {
        y=y*(n-j);
    }
    y=x/y;
    printf("%d %d",x,y);
}
 
int main(int argc, char *argv[]) {
    int m,n;
    scanf("%d %d",&m,&n);
    f(m,n);
    return 0;
}
  •  6.4冒泡排序

编写函数 void BubleSort(int *p, int n),用“冒泡法”对输入的n个整数按从小到大的顺序排列。
编写主函数,输入n和n个数组元素,调用函数排序,在主函数中输出结果。
注意,输入、输出也通过单独的函数实现(函数自己定义),在排序函数中不能输出数组元素。

输入:输入n,换行后输入n个整数。(n不超过20)
输出:从小到大的排列,数字之间有空格,末尾无空格。
【输入输出样例】
输入:
5
5 10 3 12 1
输出:
1 3 5 10 12

#include <stdio.h>
#include <stdlib.h>
  
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void BubleSort(int *p, int n){
    int i,j,t;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(*(p+j)>*(p+j+1))
            {
                t=*(p+j+1);
                *(p+j+1)=*(p+j);
                *(p+j)=t;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        if(i==n-1)
        {
            printf("%d",*(p+i));
        }
        else{
            printf("%d ",*(p+i));
        }
    }
      
}
int main(int argc, char *argv[]) {
    int n,k;
    int a[100];
    scanf("%d",&n);
    for(k=0;k<n;k++)
    {
        scanf("%d",&a[k]);
    }
    int *p=a;
    BubleSort(p,n);
    return 0;
}

  •  6.13 输入、排序、查找

题目内容:
编写两个函数分别实现整型数组从小到大排序、二分法在有序数组中查找(返回下标, 找不到时结果为-1)。函数原型:
void Sort(int *p, int n);
int Search(int *p, int n, int key); // n - 数组长度, key - 待查数字
在主函数输入若干正整数(不超过100个,输入-9999表示结束)以及5个待查找数字,调用排序和查找函数,输出查找结果。

输入样例:( 第1行为数组元素,以-9999结束;第2行输入5个待查找的元素。)
2 1 3 4 5 -9999
2 3 4 -10 10
输出样例:(5行,每行是找到的元素下标,找不到时结果为-1)
1
2
3
-1
-1

#include <stdio.h>
#include <stdlib.h>
 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void Sort(int *p,int n){
    int i,j,t;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i-1;j++)
        {
            if(p[j]>p[j+1])
            {
                t=p[j+1];
                p[j+1]=p[j];
                p[j]=t; 
            }
        }
    }
     
 }
 
int Search(int *p, int n, int key){
    int left=0,right=n-1;
    int middle=(left+right)/2;
    for(;left<=right;middle=(left+right)/2)
    {
        if(p[middle]>key)
        {
            right=middle-1;
        }
        else if(p[middle]<key)
        {
            left=middle+1;
        }
        else
        {
            return middle;
        }
          
    }
    return -1;
}
  
int main(int argc, char *argv[]) {
    int a[100],*p,n;
    int key1,key2,key3,key4,key5;
    int k=0,middle;
   while(scanf("%d",&a[k]))
    {
        if(a[k]==-9999)
        {
        break;
        }
        k++;
    }
    n=k;
     
    p=a;
    Sort(p,n);
       scanf("%d",&key1);
       scanf("%d",&key2);
          scanf("%d",&key3);
          scanf("%d",&key4);
          scanf("%d",&key5);
       printf("%d\n",Search(p,n,key1));
       printf("%d\n",Search(p,n,key2));
       printf("%d\n",Search(p,n,key3));
       printf("%d\n",Search(p,n,key4)); 
       printf("%d\n",Search(p,n,key5));
    return 0;
}

  • 5.12统计各类字符个数

【题目描述】
编写函数 void Stat(char *p, int *pA, int *pa, int *pd, int *ps, int *pother), 该函数统计一行字符(p指向)中大写
字母、小写字母、数字、空格以及其他字符的个数,并通过指针pA, pa, pd, ps, pother 将这些信息返回主函数。在
主函数中输入3行文字,每行不超过80个字符。调用Stat函数三次,统计出全部文本中的大写字母、小写字母、数字、
空格以及其他字符的个数并输出。
输出:
【输入输出样例】
输入:(三行英文文字,可能有空格及数字、标点等符号)
year 2019
Month 3
DAY 20!
输出: (5个整数,空格隔开,分别表示大写、小写字母、数字、空格及其他字符个数,末尾无空格)。
4 8 7 3 1

#include <stdio.h>

void Stat(char *p, int *pA, int *pa, int *pd, int *ps, int *pother) {
    while (*p != '\0') { //遍历整行字符
        if (*p >= 'A' && *p <= 'Z') { //大写字母
            (*pA)++;
        } else if (*p >= 'a' && *p <= 'z') { //小写字母
            (*pa)++;
        } else if (*p >= '0' && *p <= '9') { //数字
            (*pd)++;
        } else if (*p == ' ') { //空格
            (*ps)++;
        } else { //其他字符
            (*pother)++;
        }
        p++; //指针指向下一个字符
    }
}

int main() {
    char text1[80], text2[80], text3[80];
    int countA = 0, counta = 0, countd = 0, counts = 0, countother = 0;

    gets(text1);
    Stat(text1, &countA, &counta, &countd, &counts, &countother); //统计字符个数

    gets(text2);
    Stat(text2, &countA, &counta, &countd, &counts, &countother);

    gets(text3);
    Stat(text3, &countA, &counta, &countd, &counts, &countother);

    printf("%d %d %d %d %d", countA, counta, countd, counts, countother); //输出统计结果
    return 0;
}
  • 6.3转置矩阵

写一个函数,将传入的n*n的二维整型数组转置,即行列互换。函数原型:
void Trans(int A[][10], int n);
在主函数输入正整数n,换行后输入n*n个数组元素(n<10)。调用函数
Trans对输入矩阵原地进行转置。然后在主函数输出转置矩阵。
【输入输出样例】
输入:
3
1 2 3
4 5 6
7 8 9
输出:(n行,每行相邻数字之间有空格,末尾无空格)
1 4 7
2 5 8
3 6 9

#include <stdio.h>
#include <stdlib.h>
 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void Trans(int A[][10], int n){
    int t,k,q;
    for(k=0;k<n;k++)
    {
        for(q=k;q<n;q++)
        {
            t=A[k][q];
            A[k][q]=A[q][k];
            A[q][k]=t;
        }
    }
}
int main(int argc, char *argv[]) {
    int n,a[10][10];
    int i,j;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    Trans(a,n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d",a[i][j]);
            if(j<n-1)
        {
            printf(" ");
        }
        else{
            printf("\n");
        }
        }
         
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值