c语言的题,请友友们解答

img

img

img

img


刚开始学习c语言,这几个题我不会,还没有掌握,请友友们解答!

1009 Code

有可能超时,应该还有优化的空间。


#include<stdio.h>

int main(){

    int T;
    scanf("%d", &T);

    for( int t = 0; t < T ;t++ ) {
        unsigned long long x;
        unsigned int n;
        scanf("%lld %d", &x, &n);

        unsigned long long Gx = x;
        for( int i=0; i < n; i++) {
            unsigned long long tmp = Gx;
            unsigned long long fx = 1;
            while ( tmp != 0 )
            {
                fx *= tmp % 10;
                tmp /= 10;
            }
            Gx = fx;
            if( Gx < 10 ) {
                break;
            }
        }
        
        printf("%lld\n", Gx);
    }

    return 0;
}

1018 Code


#include<stdio.h>

int main(){

    int N;
    scanf("%d", &N);
    while ( N-- )
    {
        int a,b;
        scanf("%d %d", &a, &b);

        int maxL = a < b? a : b;
        unsigned long long cnt = 0;
        for( int t = 0; t < maxL ; t++ ) {
            cnt += (a - t) * (b - t);
        }
        printf("%lld\n", cnt);
    }
    return 0;
    
}

1019 code


```c

#include<stdio.h>
#include<math.h>

int main(){

    int T;
    scanf("%d", &T);
    while ( T-- )
    {

        int Ax,Ay,Bx,By,Cx,Cy;
        scanf("%d %d %d %d %d %d", &Ax,&Ay,&Bx,&By,&Cx,&Cy);

        double area = 0;

        if( Ax == Bx || Ay == By ) {
            area = abs( (Ax-Cx)*(Ay-Cy) );
        } else{
            float cosAg =  ( (Bx-Ax)*(Cx-Ax) + (By-Ay)*(Cy-Ay) ) / ( sqrt( (Cx-Ax)*(Cx-Ax) + (Cy-Ay)*(Cy-Ay) ) * sqrt( (Bx-Ax)*(Bx-Ax) + (By-Ay)*(By-Ay) ) );
            area = abs( ( (Cx-Ax)*(Cx-Ax) + (Cy-Ay)*(Cy-Ay) ) * sqrt( 1 - cosAg* cosAg ) * cosAg );
        }

        printf("%.1f\n", area);
    }
    return 0;
    
}

```


    int a,b,d;
    cin>>a>>b>>d;
    int c;
    c=a+b;
    int z[1000],i=0;
    do
    {
        z[i++]=c%d;
        c=c/d;
    }
    while(c!=0);
    for(int j=i-1;j>=0;j--)
    {
        cout<<z[j];
    }
    cout<<endl;

回答:完成了第一题,代码如下

# include <stdio.h>
# include <stdlib.h>
# include <cmath>
# include <string.h>

void initArr(char** b, char** d, char** res, int n)
{
    for (int i = 0; i < n; i++)
    {
        b[i] = (char*)malloc(sizeof(int) * 18);
        d[i] = (char*)malloc(sizeof(int) * 18);
        res[i] = (char*)malloc(sizeof(int) * 18);
    }
}

void init(int* a, char** b, int* c, char** d, int* e, int n)
{
    for (int i = 0; i < n; i++)
    {
        scanf("%d %s %d %s %d", &a[i], b[i], &c[i], d[i], &e[i]);
    }
}

void print(int* a, char** b, int* c, char** d, int* e, int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("%d %s %d %s %d\n", a[i], b[i], c[i], d[i], e[i]);
    }
}

void printRes(char** res, int n)
{
    for (int i = 0; i < n; i++)
    {
        int length = strlen(res[i]);
        for (int j = 0; j < length; j++)
        {
            res[i][j] = res[i][j] - 'a' + 'A';
        }
    }
    for (int i = 0; i < n; i++)
    {
        printf("%s\n", res[i]);
    }
}

long transformToTen(int a, char* b)
{
    long result = 0;
    int index = 0;
    while (b[index] != '\0')
    {
        int temp = b[index] - '0';
        if (b[index] >= 'A' && b[index] <= 'Z')
        {
            temp = b[index] - 'A' + 10;
        }
        result += pow(a, index) * temp;
        index++;
    }
    return result;
}

void reverse(char* b)
{
    char* temp = (char*)malloc(sizeof(int) * 18);
    int index = strlen(b);
    for (int i = 0; i < index; i++)
    {
        temp[i] = b[index - i - 1];
    }
    temp[index] = '\0';
    strcpy(b, temp);
}

void transform(int* a, char** b, int* c, char** d, int* e, int n, char** res)
{
    for (int i = 0; i < n; i++)
    {
        reverse(b[i]);
        reverse(d[i]);
    }
    for (int i = 0; i < n; i++)
    {
        long returnSum = transformToTen(a[i], b[i]) + transformToTen(c[i], d[i]);
        itoa(returnSum, res[i], e[i]);
    }
}

int main()
{
    int n;
    scanf("%d", &n);

    int* a = (int*)malloc(sizeof(int) * n);
    char** b = (char**)malloc(sizeof(char*) * n);
    int* c = (int*)malloc(sizeof(int) * n);
    char** d = (char**)malloc(sizeof(char) * n);
    int* e = (int*)malloc(sizeof(int) * n);
    char** res = (char**)malloc(sizeof(char) * n);

    initArr(b, d, res, n);

    init(a, b, c, d, e, n);

    transform(a, b, c, d, e, n, res);
    printRes(res, n);
}

/*
3
35 N9B 26 30K 32
27 1AP 33 NQ1 35
17 D5D1 35 P9NP6P 36

1
16 13 16 15 35
*/

img


1002 
int toInt(char *s, int n)
{
    int r = 0;
    int t=1;
    while(*s)
    {
        r+=t*(*s-'0');
        t*=n;
        s++;
    }
    return r;
}

第二题答案:计算函数值

#include <stdio.h>
#include <stdlib.h>
// 计算F(x) 
int F(int x) {
    int ans = 1;
    while (x) {
        int mod = x % 10;
        ans *= mod;
        x /= 10;
    }
    return ans;
}
// 计算G(x,n) 
int G(int x, int n) {
    if (n == 0) {
        return x;
    }
    int ans = F(x);
    // 迭代计算
    for (int i = 2; i <= n; i++) {
        ans = F(ans);
    }
    return ans;
}
int main() {
    int T = 0;
    scanf("%d", &T);
    for (int i = 0; i < T; i++) {
        int x = 0, n = 0;
        scanf("%d %d", &x, &n);
        // 计算 G(x,n) 
        int ans = G(x, n);
        printf("ans==>%d\n", ans);
    }
    return 0;
}