C语言练习——指针相关题目


转置矩阵

按如下函数原型编程计算并输出n×n阶矩阵的转置矩阵。其中,n由用户从键盘输入。已知n值不超过10。
void Transpose(int (*a)[N], int n);
void  Swap(int *x, int *y);
void InputMatrix(int (*a)[N], int n);
void PrintMatrix(int (*a)[N], int n);
输入提示信息:"Input n:"
输入格式:"%d"
输入提示信息:"Input %d*%d matrix:\n"
输出提示信息:"The transposed matrix is:\n"
输出格式:"%d\t"

#include <stdio.h>
#define N 10
void Transpose(int (*a)[N], int n);
void Swap(int *x, int *y);
void InputMatrix(int (*a)[N], int n);
void PrintMatrix(int (*a)[N], int n);
int main()
{
    int a[N][N],n;
    printf("Input n:");
    scanf("%d",&n);
    InputMatrix(a,n);
    Transpose(a,n);
    printf("The transposed matrix is:\n");
    PrintMatrix(a,n);
    return 0;
}
void InputMatrix(int (*a)[N], int n)
{
    int i,j;
    printf("Input %d*%d matrix:\n",n,n);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",*(a+i)+j);
        }
    }
}
void Transpose(int (*a)[N], int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=i;j<n;j++)
        {
            Swap(*(a + j) + i, *(a + i) + j);
        }
    }

}
void PrintMatrix(int (*a)[N], int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d\t",*(*(a+i)+j));
        }
        printf("\n");
    }
}
void Swap(int *x, int *y)
{
    int  temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

数组指针转置矩阵

用指向一维数组的指针变量即二维数组的行指针作为函数参数,实现矩阵转置。按如下函数原型编程计算并输出m×n阶矩阵的转置矩阵。其中,m和n的值由用户从键盘输入。已知m和n的值都不超过10。
void Transpose(int (*a)[N], int (*at)[M], int m, int n);
void InputMatrix(int (*a)[N], int m, int n);
void PrintMatrix(int (*at)[M], int n, int m);
输入提示信息:"Input m, n:"
输入格式:"%d,%d"
输入提示信息:"Input %d*%d matrix:\n"
输出提示信息和格式:"The transposed matrix is:\n"
输出格式:"%d\t"


程序测试用例:
Input m, n:3,4
Input 3*4 matrix:
1 2 3 4
5 6 7 8
9 10 11 12
The transposed matrix is:
1    5    9    
2    6    10    
3    7    11    
4    8    12    



#include <stdio.h>
#include <string.h>
#define M 10
#define N 10
void Transpose(int (*a)[N], int (*at)[M], int m, int n);
void InputMatrix(int (*a)[N], int m, int n);
void PrintMatrix(int (*at)[M], int n, int m);
int main()
{
    int s[M][N],st[N][M];
    int m,n;
    printf("Input m, n:");
    scanf("%d,%d",&m,&n);
    InputMatrix(s,m,n);
    Transpose(s,st,m,n);
    PrintMatrix(st,n,m);
    return 0;
}
void InputMatrix(int (*a)[N], int m, int n)
{
    int i,j;
    printf("Input %d*%d matrix:\n",m,n);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",*(a+i)+j);
        }
    }
}
void Transpose(int (*a)[N], int (*at)[M], int m, int n)
{
    int i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            *(*(at+j)+i)=*(*(a+i)+j);
        }
    }
}
void PrintMatrix(int (*at)[M], int n, int m)
{
    int i,j;
    printf("The transposed matrix is:\n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf("%d\t",*(*(at+i)+j));
        }
        printf("\n");
    }
}
下列给定程序中,函数fun()的功能是求出数组中最小数和次最小数,并把最小数和a[0]中的数对调,次最小数和a[1]中的数对调。
注意:
(1)请将修改正确后的完整源程序拷贝粘贴到答题区内。
(2)对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。
(3)当且仅当错误全部改正,且程序运行结果调试正确,才给加5分。
#include <stdio.h>
#define N 20
void fun(int *a,int n)
{
    int i, m, t, k;
    for(i=0; i<n; i++)
    {
        m=i;
        for(k=i; k<n; k++)
           if(a[k]<a[m])
            k=m;
        t=a[k]
        a[k]=a[i];
        a[i]=t;
    }
}
int main()
{
    int b[N]={11,5,12,0,3,6,9,7,10,8},n=10,i;
    for(i=0; i<n; i++)
        printf("%d ",b[i]);
    printf("\n");
    fun(b,n);
    for(i=0; i<n; i++)
        printf("%d ", b[i]);
    printf("\n");
    return 0;
}



#include <stdio.h>
#define N 20
void fun(int *a,int n)
{
    int i, m, t, k;
    for(i=0; i<2; i++)
    {
        m=i;
        for(k=i; k<n; k++)
            if(a[k]<a[m])
                m=k;
        t=a[i];
        a[i]=a[m];
        a[m]=t;
    }
}
int main()
{
    int b[N]={11,5,12,0,3,6,9,7,10,8},n=10,i;
    for(i=0; i<n; i++)
        printf("%d ",b[i]);
    printf("\n");
    fun(b,n);
    for(i=0; i<n; i++)
        printf("%d ", b[i]);
    printf("\n");
    return 0;
}
写一个程序逆反输入的字符串。

**提示信息:"请输入一个字符串:"
**输出格式要求:"该字符串逆反后为%s!\n"


#include <stdio.h>
#include <string.h>
#define N 20
int main()
{
    char a[N];
    int i,len;
    char *p;
    char temp;
    printf("请输入一个字符串:");
    gets(a);
    len = strlen(a);
    p = a;
    for(i=0;i<len/2;i++)
    {
        temp = *(p+i);
        *(p+i) = *(p+len-i-1);
        *(p+len-i-1) = temp;
    }
    printf("该字符串逆反后为%s!\n",a);
    return 0;
}
用指针编程实现3X4的二维数组的元素读入以及求此二维数组的最大值及最大值下标 
请用以下函数实现:
void Input(int (*p)[4],int m, int n); /*数组元素读入函数*/
int FindMax(int *p[4], int m, int n, int *pRow, int *pCol); /*求最大值及下标函数*/

***输入提示信息:"Please input your data:\n"
***输入格式要求:无格式要求
***输出格式要求:"The maximum is %d, which is in row %d, colum %d\n"

样例:

Please input your data:
**输入样例:
3 5 2 7 1 6 12 11 4 10 8 9
**输出样例:
The maximum is 12, which is in row 1, colum 2



#include <stdio.h>
#define M 3
#define N 4
void Input(int (*p)[4], int m, int n); 
int FindMax(int (*p)[4], int m, int n, int *pRow, int *pCol);       
main()
{                      
    int score[M][N], maxScore, row, col;
    printf("Please input your data:\n");
    Input(score, M, N);     
    maxScore = FindMax(score, M, N, &row, &col);        
    printf("The maximum is %d, which is in row %d, colum %d\n", maxScore, row, col);
}                      
void Input(int (*p)[4], int m, int n)
{                      
    int i, j;
    for (i = 0; i < m; i++)     
    {                      
        for (j = 0; j < n; j++)     
            scanf("%d", (p[i] + j));
    }
}                      
int FindMax( int (*p)[4], int m, int n, int *pRow, int *pCol )
{                      
    int  i, j, max;
    max = *(p[0]);      
    *pRow = 0;
    *pCol = 0;
    for (i = 0; i < m; i++)     
    {                      
        for (j = 0; j < n; j++)     
        {                      
            if ( *(p[i] + j) > max )        
            {                      
                max = *(p[i] + j) ;
                *pRow = i;
                *pCol = j;
            }
        }
    }
    return max;
}
任意输入英文的星期几,通过查找星期表,输出其对应的数字,
若查到表尾,仍未找到,则输出错误提示信息。
**输入格式要求:"%s" 提示信息:"Please enter a string:\n"
**输出格式要求:"%s is %d\n" "Not found!\n"
查找表中信息:
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
程序运行示例1如下:
Please enter a string:
Monday
Monday is 1
程序运行示例2如下:
Monkey
Not found!


#include <stdio.h>
#include <string.h>
#define MAX 10

int main()
{
    int i;
    char week[MAX];
    char *week_list[MAX] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    printf("Please enter a string:\n");
    scanf("%s",week);
    for(i = 0;i < 7;i++)
    {
        if(strcmp(week,week_list[i])==0)
        {
            if(i==0)
            {
                printf("%s is %d\n",week,i+7);
                break;
            }
            else
            {
                printf("%s is %d\n",week,i);
                break;
            }
        }
        else if(i>= 6)
        {
            printf("Not found!\n");
        }
    }
    return 0;
}
程序改错。在一个3x4矩阵中找出最大数及最大数所在元素的下标。 
#include <stdio.h>
#define M 3
#define N 4
int FindMax(int (*p)[N],int m,int n, int *pRow, int *pCol) 
main()
{
    int score[M][N], i, j, maxScore, row, col;
    for ( i=0; i<M; i++ )
    {
          for ( j=0; j<N; j++ )
          {
               scanf("%d", &score[i][j]);
          }
    }
    maxScore = FindMax(*score, M, N, &row, &col);
    printf("%d %d %d\n", maxScore, row+1, col+1);
}
 
int FindMax( int (*p)[N], int m, int n, 
int *pRow, int *pCol )
{
    int  i, j, max;
    max = *(p);
    pRow = 0; 
    pCol = 0; 
    for (i=0; i<m; i++)
    {
        for (j = 0; j<n; j++)
                {
            if ( *(*(p+i)+j) > max )
                        {
                max = *(*(p+i)+j) ;
                *pRow = i;
                *pCol = j;
                        }
        }
    }
     
}




#include <stdio.h>
#define M 3
#define N 4
int FindMax(int (*p)[N],int m,int n, int *pRow, int *pCol);
int main()
{
    int score[M][N], i, j, maxScore, row, col;
    for ( i=0; i<M; i++ )
    {
          for ( j=0; j<N; j++ )
          {
               scanf("%d", &score[i][j]);
          }
    }
    maxScore = FindMax(score, M, N, &row, &col);
    printf("%d %d %d\n", maxScore, row+1, col+1);
    return 0;
}

int FindMax( int (*p)[N], int m, int n, int *pRow, int *pCol )
{
    int  i, j, max;
    max = *(*p);
    for (i=0; i<m; i++)
    {
        for (j = 0; j<n; j++)
        {
            if ( *(*(p+i)+j) > max )
                {
                max = *(*(p+i)+j) ;
                *pRow = i;
                *pCol = j;
                }
        }
    }
    return max;
}
求最长有效括号长度
求出一串由:‘(’和‘)’组成的字符串中最长有效括号的长度。例如:(()(),结果是4。((()))结果是6。())()()结果是4。
程序输入输出示例
Please input the array
()()()
The longest length is 6
函数原型为
int longestValidParentheses(char* s)


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 100
int longestValidParentheses(char* s);
int main()
{
    char arr[M];
    int len;
    printf("Please input the array\n");
    scanf("%s",arr);
    len = longestValidParentheses(arr);
    printf("The longest length is %d",len);
    return 0;
}
int longestValidParentheses(char* s)
{
    int tmp=0,i=0,max=0;
    int s_length = strlen(s);
    int s_stack_index = 0;
    char* s_stack = malloc(sizeof(char)*s_length);
    int* number_stack = malloc(sizeof(int)*s_length);

    while(i<s_length)
    {
        //入栈
        //s_stack为栈,数据来源于s数组
        //number_stack数组依次为入栈数据编号
        //s_stack_index用来记录栈内剩余元素
        if(s[i]=='(')
        {
            s_stack[s_stack_index]=s[i];
            number_stack[s_stack_index]=i;
            s_stack_index++;
        }
        else
        {
            if(s_stack_index>=1 && s_stack[s_stack_index-1]=='(')
                //如果栈内元素大于1个,且栈顶下一个元素为(时,由于栈顶元素为),故弹出
            {
                s_stack_index--;
                if(s_stack_index==0)
                {
                    tmp = i+1;
                }
                else
                {
                    tmp = i-number_stack[s_stack_index-1];//栈内的栈出完了的话,则出栈玩
                }
                //连续配对出栈数计数,最大值即为最长括号对
                if(tmp>max)
                {
                    max = tmp;
                }
            }
            else
            {
                s_stack[s_stack_index]=s[i];
                number_stack[s_stack_index]=i;
                s_stack_index++;
            }
        }
        i++;
    }
    free(s_stack);
    free(number_stack);
    return max;
}
大夫值班问题:医院有A、B、C、D、E、F、G七位大夫,在一个星期内(星期一至星期天)
每天要轮流值班一天。现在已知:
        A大夫比C大夫晚一天值班;
        D大夫比E大夫晚两天值班;
        B大夫比G大夫早三天值班;
        F大夫的值班日在B和C大夫的中间,且是星期四;
请编程确定每天究竟哪位大夫值班?
**输出格式要求:"Doctor %c is on duty %s.\n"
程序运行示例如下:
Doctor E is on duty MONDAY.
Doctor B is on duty TUESDAY.
Doctor D is on duty WEDNESDAY.
Doctor F is on duty THURDAY.
Doctor G is on duty FRIDAY.
Doctor C is on duty STAURDAY.
Doctor A is on duty SUNDAY.



#include<stdio.h>
#include<stdlib.h>
 
int a[8]; 
char *day[] = {"","MONDAY","TUESDAY","WEDNESDAY","THURDAY",
"FRIDAY","STAURDAY","SUNDAY"};
 
int main()
{
    int i,j,t; 
    a[4]=6;
 
    for(i=1;i<=3;i++)
    { 
        a[i]=2;
 
        if(!a[i+3])
        a[i+3]=7;
 
        else
        {
            a[i]=0;continue;
        }
 
        for(t=1;t<=3;t++)
        { 
            if(!a[t])
            a[t]=5;
            else
            continue; 
            if(!a[t+2])
            a[t+2]=4;
            else
            {
                a[t]=0;continue;
            }
            for(j=5;j<7;j++)
            { 
                if(!a[j])
                a[j]=3;
                else
                continue; 
                if(!a[j+1])
                a[j+1]=1;
                else
                {
                    a[j]=0;continue;
                }
                for(i=1;i<=7;i++)
                printf("Doctor %c is on duty %s.\n",'A'+a[i]-1,day[i]);
                exit(0);
                }
        }
    }
}

声明:ITanger|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - C语言练习——指针相关题目


Carpe Diem and Do what I like