0
Follow
1
View

c/java language two problems to solve, need code

demon909 注册会员
2023-02-26 20:17

This answer quotes ChatGPT

Question 1

In this program, we first define an f function to compute the value of f(a,b) for a given parameter. In this function, C is returned if a=1, D if a=2, otherwise f(a-1, f(a-1,b)) is recursively computed.

In the Main function, we read in the input parameters and calculate the value of f(a,b). Finally, we just need to print out the units digits of f(a,b).

using System;

class Program {
    static int C, D;

    static int f(int a, int b) {
        if (a == 1) {
            return C;
        } else if (a == 2) {
            return D;
        } else {
            return f(a - 1, f(a - 1, b));
        }
    }

    static void Main(string[] args) {
        int a = int.Parse(Console.ReadLine());
        int b = int.Parse(Console.ReadLine());
        C = int.Parse(Console.ReadLine());
        D = int.Parse(Console.ReadLine());

        int result = f(a, b) % 10;
        Console.WriteLine(result);
    }
}


、、、、、、、、、、、、、、、、、、

Question 2

In this program, we first define an f function to compute the value of f(a,b) for a given parameter. In this function, C is returned if a=1, D if a=2, otherwise f(a-1, f(a-1,b)) is recursively computed.

In the main function, we read in the input parameters and calculate the value of f(a,b). Finally, we just need to print out the units digits of f(a,b).

Note that in Java, we need to use the Scanner class to read in the input. First we need to create a Scanner object and set its constructor to System.in, indicating that we are reading from standard input. We can then use the nextInt method to read the input integer. After reading all the input parameters, we can calculate the value of f(a,b) and output the result.

import java.util.Scanner;

public class Main {
    private static int C, D;

    /**
     * 计算 f(a, b) 的值
     * @param a 参数 a
     * @param b 参数 b
     * @return f(a, b) 的值
     */
    private static int f(int a, int b) {
        if (a == 1) {
            return C;
        } else if (a == 2) {
            return D;
        } else {
            return f(a - 1, f(a - 1, b));
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        C = scanner.nextInt();
        D = scanner.nextInt();

        int result = f(a, b) % 10;
        System.out.println(result);
    }
}


ctl198423 注册会员
2023-02-26 20:17
Hello, I am asked to answer the little assistant, your question has been a little partner to help you answer, thank you for your support and attention to have asked to answer!
PS: Q&A VIP annual card [limited time plus: free IT technical books], for details > > > https://vip.csdn.net/askvip?utm_source=1146287632
cuihongxiangvic 注册会员
2023-02-26 20:17

The following answers are quoted from GPT-3 model, please use them wisely:

,谢谢!

C语言:

题目1:给定一个无序数组,找出最大的k个数

思路:本题可以利用堆排序的思想来求解。首先我们需要建立一个大小为k的最大堆,将前k个元素放进去,再建立一个循环,将数组中剩余的元素依次与堆顶元素比较,如果大于堆顶元素,就把堆顶元素替换掉,再调整堆结构,重复这个过程,最后就得出最大的K个数。

代码如下:

#include 
#define MAXSIZE 20 
  
void heapAdjust(int arr[], int i, int nLength) 
{ 
    int nChild; 
    int nTemp; 
    for(; 2*i+1//子结点的位置=2*(父结点位置)+1 
        nChild=2*i+1; 
        //得到子结点中较大的结点 
        if(nChild1 && arr[nChild+1]>arr[nChild]) 
            ++nChild; 
        //如果较大的子结点大于父结点那么把较大的子结点往上移动,替换它的父结点 
        if(arr[i][nChild]) 
        { 
            nTemp=arr[i]; 
            arr[i]=arr[nChild]; 
            arr[nChild]=nTemp; 
        } 
        else 
        // 如果发现父结点较大,则不做调整 
            break; 
    } 
} 
  
void HeapSortDesc(int arr[], int nLength) 
{ 
    int i; 
    // 调整序列的前半部分元素,调整完之后第一个元素是序列的最大的元素 
    for(i=nLength/2-1; i>=0; --i) 
        heapAdjust(arr,i,nLength); 
    // 从最后一个元素开始对序列进行调整,不断的缩小调整的范围直到第一个元素 
    for(i=nLength-1; i>0; --i) 
    { 
        // 把第一个元素和当前的最后一个元素交换, 
        // 保证当

Please accept my answer if it answers your question

dfzxdmy110 注册会员
2023-02-26 20:17

The first problem is a simple recursive problem. Note that the value returned by the recursive function should be returned directly to its units. Instead of returning the value of f(a,b) and then printing the ones digit.

#include
 
int func(int a,int b,int c,int d){
    int y;
    if(a==1){
        y=c*b+d;
    }
    if(a>=2){
        y=func((a-1),func((a-1),b,c,d),c,d);
    }
    return y%10;
}
int main() 
{ 
    int a,b,c,d;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    printf("%d",func(a,b,c,d));
 
    return 0; 
}

kdfashioninc 注册会员
2023-02-26 20:17

Either c or java is acceptable, either

cwpwds 注册会员
2023-02-26 20:17

Second problem, walk to n, calculate the inverse of each number, and then test whether it is a multiple of the inverse of this number, if so, increase the number by 1, output after the end of the walk.


#include
int main() 
{ 
    int n;
    scanf("%d",&n);
    int q=0 ;
    for(int i=1;i<=n;i++){
        int a=i;
        int b = 0;
        int c = 0;
        while(a!=0){
            b=a%10;
            a=a/10;
            c=c*10+b;
        }
        if(i%c==0){
            q++;
        }
    }
    printf("%d",q);
    return 0; 
}

About the Author

Question Info

Publish Time
2023-02-26 20:17
Update Time
2023-02-26 20:17