亦清y 2021-12-15 14:11 采纳率: 71.4%
浏览 49
已结题

大一新生在学递归时碰到的问题

到很多地方求助都没有结果,来这里碰碰运气,谢谢大佬了。#include<stdio.h>

void selection_sort(int* arr,int n)
{
int count=0,tmp;
for(int j=0;j<n-1-count;j++)
{

   if(arr[j]>arr[j+1]){
    tmp=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=tmp;
}}
count++;
if(count!=n-1)

selection_sort(arr,n);
else return;

}
int main()
{int n;
scanf("%d",&n);
int a[n]={0};

for(int i=0;i<n;i++)

    scanf("%d",&a[i]);

selection_sort(a,n);
for(int k=0;k<n;k++)
printf("%d ",a[k]);
return 0;

}#include<stdio.h>

void selection_sort(int* arr,int n)
{
int count=0,tmp;
for(int j=0;j<n-1-count;j++)
{

   if(arr[j]>arr[j+1]){
    tmp=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=tmp;
}}
count++;
if(count!=n-1)

selection_sort(arr,n);
else return;

}
int main()
{int n;
scanf("%d",&n);
int a[n]={0};

for(int i=0;i<n;i++)

    scanf("%d",&a[i]);

selection_sort(a,n);
for(int k=0;k<n;k++)
printf("%d ",a[k]);
return 0;

}

#include<stdio.h>
#include<math.h>
int quickerpower(int x,int n)
{
if(n%2==0)
return quickerpower(x,n/2)quickerpower(x,n/2);
else if(n%2!=0)
return x
quickerpower(x,n-1);
else if(n==0)
return 1;
}
int main()
{
int x=3,n=2,a;
a=quickerpower(3,2);
printf("%d %d",a,pow(x,n));
return 0;
}这两个递归都写错了啊,怎么改都不对。

img

img

img

第一题和第七题。

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2021-12-15 16:38
    关注

    修改如下,供参考:

    //编写程序,要求用户登录一串整数(把这串整数存储在数组中),
    //然后通过调研selectio_sort函数来排序这些整数。
    //在给定n个元素数组后,selection_sort函数必须做下列工作:
    //a.搜索数组找出最大的元素,然后把它移到数组的最后
    //b.递归地调用函数本身来对前n-1个数组元素进行排序。
    #include <stdio.h>
    #define N 10
    void selection_sort(int* arr, int n)
    {
        int i, t, imax = 0;
        if (n < 1) return;
        for (i = 1; i < n; ++i) {
            if (arr[imax] < arr[i])
                imax = i;
        }
        if (imax != n - 1) {
            t = arr[n - 1];
            arr[n - 1] = arr[imax];
            arr[imax] = t;
        }
        selection_sort(arr, n - 1);
    }
    int main()
    {
        int n;
        int a[N] = { 0 };
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        selection_sort(a, n);
        for (int k = 0; k < n; k++)
            printf("%d ", a[k]);
        return 0;
    }
    
    
    
    //换一种方法计算x^n,如果n是2的幂,则可以通过自乘的方法计算x^n.
    //如果n是偶数,可以用公式x^n=(x^n/2)^2;
    //如果n是奇数,则x^n=x*x^n-1.
    #include<stdio.h>
    int quickerpower(int x, int n)
    {
        if (n == 0)
            return 1;
        if (n % 2 == 0)
            return quickerpower(x, n / 2) * quickerpower(x, n / 2);
        else if (n % 2 != 0)
            return x * quickerpower(x, n - 1);
    }
    int main()
    {
        int x = -3, n = 2, a;
        a = quickerpower(x, n);
        printf("%d", a);
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月16日
  • 已采纳回答 12月15日
  • 创建了问题 12月15日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题