哈工大C语言程序设计精髓第十周

news/2024/7/10 22:58:32 标签: html, jquery, css

由于这些代码也是我初学时写的代码,故其中的规范程度及简洁程度并不很好(此处我后来写的有可以参考一下->C语言代码规范),但是能很好的接近出初学者的水平,也更有参考价值!排版不易,喜欢就点个赞吧!如有问题,请勿吐槽,欢迎留言互相学习。

第7周编程题在线测试

1. 数字字符串转换为整型数


题目内容
从键盘输入一串字符(假设字符数少于8个),以回车表示输入结束,编程将其中的数字部分转换为整型数并以整型的形式输出。
函数原型为 int Myatoi(char str[]);
其中,形参数组str[]对应用户输入的字符串,函数返回值为转换后的整型数。
解题思路的关键是:1)判断字符串中的字符是否是数字字符;2)如何将数字字符转换为其对应的数字值;3)如何将每一个转换后的数字值加起来形成一个整型数。
程序运行结果示例1:
Input a string: 7hg09y↙
709
程序运行结果示例2:
Input a string: 9w2k7m0↙
9270
程序运行结果示例3:
Input a string: happy↙
0
输入提示信息:“Input a string:”
输入格式: “%7s”
输出格式:"%d\n"

代码实现

#include <stdio.h>
#include <stdlib.h>
int Myatoi(char str[]);
int main()
{
    char a[8];
    int b;
    printf("Input a string:");
    scanf("%s",a);
    b=Myatoi(a);
    printf("%d",b);
    return 0;
}
int Myatoi(char str[])
{
    int i,u,sum=0;
    u=strlen(str);
    for(i=0;i<u;i++)
    {
        if(str[i]>=48 && str[i]<=57)
        {
            sum*=10;
            sum+=str[i];
            sum-=48;
        }
    }
    if(sum==0)
        return 0;
    else
        return sum;
}

2. 查找子串


题目内容
用字符数组作函数参数,编程实现在从键盘输入的字符串(假设长度小于80)中查找与指定的子串,并输出该子串在字符串中首次出现的位置,如果该字符不存在,则输出"Not found!"。
函数原型:int SearchString(char s[], char d[])
函数功能:在字符数组s中查找子串d,返回d在s中首次出现的位置,若找不到,则返回-1。
程序运行结果示例1:
Input a string: How are you!↙
Input another string: are↙
Searching results:5
程序运行结果示例2:
Input a string: hello↙
Input another string: are↙
Not found!
程序运行结果示例3:
Input a string: You are a student.↙
Input another string: you↙
Not found!
输入第一个字符串的提示信息:“Input a string:”
输入第二个字符串的提示信息:“Input another string:”
输入格式: gets()
输出格式:“Searching results:%d\n”
没找到子串的输出提示信息: “Not found!\n”

代码实现

#include <stdio.h>
#include <stdlib.h>
int SearchString(char s[], char d[]);
int main()
{
    char a[80],b[80];
    int c;
    printf("Input a string:");
    gets(a);
    printf("Input another string:");
    gets(b);
    c=SearchString(a,b);
    if(c==-1)
    {
        printf( "Not found!\n");
    }
    else
    {
        printf("Searching results:%d\n",c);
    }
    return 0;
}
int SearchString(char s[], char d[])
{
    int a,b,c,e,f,q;
    b=strlen(s);
    f=strlen(d);
    for(a=0;a<b;a++)
    {
            q=1;
        for(e=0,c=a;e<f;e++,c++)
        {
            if(s[c]!=d[e])
            {
                q=0;
            }            
        }
        if(q==1)
            return a+1;
    }
    return -1;
}

3. 统计重复字符


题目内容
输入一串字符(字符数小于80),以回车表示输入结束,编程计算并输出这串字符中连续重复次数最多的字符和重复次数。如果重复次数最多的字符有两个,则输出最后出现的那一个。
已知 函数原型
//函数功能:统计字符串中连续重复次数最多的字符及其重复的次数
//函数参数:str指向待统计的字符串,指针形参tag返回重复字符最后出现的下标位置
//函数返回值:返回字符重复的次数
int CountRepeatStr(char str[], int *tag);
求解思路:设置一个计数器,遍历字符串中的所有字符,若str[i] == str[i+1],则计数器加1,同时判断计数器的值是否大于记录的最大重复次数max,若大于,则用计数器的值更新max,并记录该字符最后出现的位置i+1.若str[i] != str[i+1],则计数器重新初始化为1。遍历结束时,函数返回max的值。
程序运行结果示例1:
Input a string:
2344455555↙
5:5
程序运行结果示例2:
Input a string:
sgf222257↙
2:4
输入提示信息:“Input a string:\n”
输入格式: 用gets()输入字符串
输出格式:"%c:%d\n"

代码实现

#include <stdio.h>
#include <stdlib.h>
int CountRepeatStr(char str[], int *tag);
int main()
{
    char a[80];
    int i=0,t;
    printf("Input a string:\n");
    gets(a);
    t=CountRepeatStr(a,&i);
    printf("%c:%d\n",t,i);
    return 0;
}
int CountRepeatStr(char str[], int *tag)
{
    int i,u,max=0,m=0;
    u=strlen(str);
    for(i=0,*tag=1;i<u-1;i++)
    {
        if(str[i]==str[i+1])
        {
            *tag+=1;
            if(*tag>max)
            {
                max=*tag;
                m=str[i];
            }
            if(*tag==max)
            {
                m=str[i];
            }
        }
        else{*tag=1;}

    }
    *tag=max;
    return m;
}

4. 凯撒密码


题目内容
凯撒密码是罗马扩张时期朱利斯•凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令,其原理很简单,就是通过将字母表中的字母移动一定位置而实现加密。例如,每个字母按字母表顺序向后移3位,如a加密后变成d,b加密后变成e,……x加密后变成a,y加密后变成b,z加密后变成c。请编写一个程序,将用户从键盘输入的文本字符串(只包含a~z的字符且长度小于100)进行加密后输出。
函数原型:void Caesar(char c[]);
函数功能:计算凯撒密码
程序的运行结果示例1:
Input a string: baidu↙
edlgx
程序的运行结果示例2:
Input a string: xyz↙
abc
输入提示信息:“Input a string:”
输入格式: 用 gets()函数
输出格式:用 puts()函数

代码实现

#include <stdio.h>
#include <stdlib.h>
void Caesar(char c[]);
int main()
{
    char a[101];
    printf("Input a string:");
    gets(a);
    Caesar(a);
    return 0;
}
void Caesar(char c[])
{
    int i,n;
    n=strlen(c);
    for(i=0;i<n;i++)
    {
        if(c[i]=='x')
        {
            c[i]='a';
        }
        else if(c[i]=='y')
        {
            c[i]='b';
        }
        else if(c[i]=='z')
        {
            c[i]='c';
        }
        else
        {
            c[i]+=3;
        }
    }
    puts(c);
}

练兵区

1. 有趣的“回文”检测


题目内容
英文中有很多的回文词,回文词的拼法十分有趣,无论是从前往后拼读,还是从后往前拼读,他们的拼法和词义都不变。例如:dad(爸爸),mum(妈妈),noon(中午),eve(前夕),eye(眼睛),pop(流行),deed(行为),level(水平)等。简单地说,“回文”就是指顺读和倒读都一样的字符串。现在请你编程输入一个单词,判断它是否是回文。
提示
(1)设置两个指针pStart和pEnd,让pStart指向字符串首部,让pEnd指向字符串尾部。
(2)利用循环从字符串两边对指针所指字符进行比较,当对应的两字符相等且两指针未超越对方时,使指针pStart向前移动一个字符位置(加1),使指针pEnd向后移动一个字符位置(减1),一旦发现两字符不等或两指针已互相超越(不可能是回文),则立即停止循环。
(3)根据退出循环时两指针的位置,判断字符串是否为回文。
程序的两次运行结果如下:
第1次
Input string: ABCCBA↙
Yes!
第2次
Input string: student↙
No!
输入提示信息:“Input string:”
输入格式: 用gets()函数
输出格式
输出信息,不是回文:“No!\n”
输出信息,是回文:“Yes!\n”

代码实现

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

int main()
{
    char a[1000];
    int i,y,n,t=1;
    printf("Input string:");
    gets(a);
    n=strlen(a);
    i=0,y=n-1;
    for(;i<=n/2;i++,y--)
    {
        if(a[i]!=a[y])
        {
            t=0;
            break;
        }
    }
    if(t==0)
    {
        printf("No!\n");
    }
    else
    {
        printf("Yes!\n");
    }
    return 0;
}

2. 学生成绩管理系统V1.0


题目内容
某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,用一维数组作函数参数编程实现如下学生成绩管理:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按学号由小到大排出成绩表;
(5)按学号查询学生排名及其考试成绩;
(6)按优秀(90 100)、良好(8089)、中等(70 79)、及格(6069)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比;
(7)输出每个学生的学号、考试成绩。
程序运行结果示例:
Input student number(n<30):
6↙
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
1↙
Input student’s ID, name and score:
11003001 87↙
11003005 98↙
11003003 75↙
11003002 48↙
11003004 65↙
11003006 100↙

Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
2↙
sum=473,aver=78.83
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
3↙
Sort in descending order by score:
11003006 100
11003005 98
11003001 87
11003003 75
11003004 65
11003002 48
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
4↙
Sort in ascending order by number:
11003001 87
11003002 48
11003003 75
11003004 65
11003005 98
11003006 100
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
5↙
Input the number you want to search:
11003004
11003004 65
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
6↙
<60 1 16.67%
60-69 1 16.67%
70-79 1 16.67%
80-89 1 16.67%
90-99 1 16.67%
100 1 16.67%
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
7↙
11003001 87
11003002 48
11003003 75
11003004 65
11003005 98
11003006 100
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
8↙
Input error!
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
0↙
End of program!
输入格式:
( 1 )录入学生的人数:
输入数据格式:"%d"
提示信息:“Input student number(n<30):\n”
( 2 )录入每个学生的学号和考试成绩:
输入数据格式:"%ld%f"
提示信息:“Input student’s ID, name and score:\n”
输出格式
菜单项的输出显示:
Management for Students’ scores
1.Input record
2.Caculate total and average score of course
3.Sort in descending order by score
4.Sort in ascending order by number
5.Search by number
6.Statistic analysis
7.List record
0.Exit
Please Input your choice:
计算课程的总分和平均分:
输出总分与平均分格式:“sum=%.0f,aver=%.2f\n”
按成绩由高到低排出名次表:
输出格式:"%ld\t%.0f\n"
提示信息:“Sort in descending order by score:\n”
按学号由小到大排出成绩表:
输出格式:"%ld\t%.0f\n"
提示信息:“Sort in ascending order by number:\n”
按学号查询学生排名及其考试成绩:
如果未查到此学号的学生,提示信息:“Not found!\n”
如果查询到该学生,输出格式:"%ld\t%.0f\n"
按优秀(90 100)、良好(8089)、中等(70 79)、及格(6069)、不及格(0~59)5个类别,统计每个类别的人数以及所占的百分比:
成绩<60输出格式:"<60\t%d\t%.2f%%\n"
成绩=100输出格式:"%d\t%d\t%.2f%%\n"
其他输出百分比格式:"%d-%d\t%d\t%.2f%%\n"

代码实现

#include <stdio.h>
#include <stdlib.h>
void DD(int a,long *b,float *c);
void EE(int a,long *b,float *c);
void FF(int a,long *b,float *c);
void GG(int a,float *c);
int main()
{
    int n,i,u,a=1,max;
    long b[100];
    float c[100],sum=0;
    printf("Input student number(n<30):\n");
    scanf("%d",&n);
    while(1!=0)
    {
        printf("Management for Students' scores\n\
1.Input record\n\
2.Caculate total and average score of course\n\
3.Sort in descending order by score\n\
4.Sort in ascending order by number\n\
5.Search by number\n\
6.Statistic analysis\n\
7.List record\n\
0.Exit\n\
Please Input your choice:\n");
        scanf("%d",&a);
        if(a==1)
        {
            printf("Input student's ID, name and score:\n");
            for(u=0;u<n;u++)
            {
                scanf("%ld%f",&b[u],&c[u]);
            }
        }
        else if(a==2)
        {
            for(u=0;u<n;u++)
            {
                sum+=c[u];
            }
            printf("sum=%.0f,aver=%.2f\n",sum,sum/n);
        }
        else if(a==3)
        {
            printf("Sort in descending order by score:\n");
            DD(n,&b,&c);
        }
        else if(a==4)
        {
            printf("Sort in ascending order by number:\n");
            EE(n,&b,&c);
        }
        else if(a==5)
        {
            printf("Input the number you want to search:\n");
            FF(n,&b,&c);
        }
        else if(a==6)
        {
            GG(n,&c);
        }
        else if(a==7)
        {
            EE(n,&b,&c);
        }
        else if(a==0)
        {
            printf("End of program!\n");
            break;
        }
        else
        {
            printf("Input error!\n");
        }
    }
    return 0;
}
void DD(int a,long *b,float *c)
{
    int i,y,r;
    float t;
    for(i=0;i<a-1;i++)
    {
        for(y=i+1;y<a;y++)
        {
            if(*(c+i)<=*(c+y))
            {
                t=*(c+y),r=*(b+y);
                *(c+y)=*(c+i),*(b+y)=*(b+i);
                *(c+i)=t,*(b+i)=r;
            }
        }
    }
    for(i=0;i<a;i++)
    {
        printf("%ld\t%.0f\n",*(b+i),*(c+i));
    }
}
void EE(int a,long *b,float *c)
{
    int i,y,r;
    float t;
    for(i=0;i<a-1;i++)
    {
        for(y=i+1;y<a;y++)
        {
            if(*(b+i)<=*(b+y))
            {
                t=*(c+y),r=*(b+y);
                *(c+y)=*(c+i),*(b+y)=*(b+i);
                *(c+i)=t,*(b+i)=r;
            }
        }
    }
    for(i=a-1;i>=0;i--)
    {
        printf("%ld\t%.0f\n",*(b+i),*(c+i));
    }
}
void FF(int a,long *b,float *c)
{
    long int d,t=0,i;
    scanf("%ld",&d);
    for(i=0;i<a;i++)
    {
        if(*(b+i)==d)
        {
            printf("%ld\t%.0f\n",d,*(c+i));
            t=1;
            break;
        }
    }
    if(t==0)
    {
        printf("Not found!\n");
    }
}
void GG(int a,float *c)
{
    int i,q,w,e,r,t,y;
    q=w=e=r=t=y=0;
    for(i=0;i<a;i++)
    {
        if(*(c+i)<60)
        {
            q++;
        }
        else if(*(c+i)<70)
        {
            w++;
        }
        else if(*(c+i)<80)
        {
            e++;
        }
        else if(*(c+i)<90)
        {
            r++;
        }
        else if(*(c+i)<100)
        {
            t++;
        }
        else if(*(c+i)==100)
        {
            y++;
        }
    }
    printf("<60\t%d\t%.2f%%\n",q,(float)(100*q)/a);
    printf("60-69\t%d\t%.2f%%\n",w,(float)(100*w)/a);
    printf("70-79\t%d\t%.2f%%\n",e,(float)(100*e)/a);
    printf("80-89\t%d\t%.2f%%\n",r,(float)(100*r)/a);
    printf("90-99\t%d\t%.2f%%\n",t,(float)(100*t)/a);
    printf("100\t%d\t%.2f%%\n",y,(float)(100*y)/a);
}

3. 程序改错——1


题目内容
下面程序的功能是,从键盘输入两个字符串,分别存放在字符数组d和s中,通过调用子函数MyStrcat( )将这两个字符串连接起来,并将连接后的字符串存放在字符数组r中,同时输出连接后的字符串。已知每个字符数组的最大长度为80。下面给出的程序存在错误,找到错误的原因后,请修改正确。并按照给出的程序运行结果示例检查你的程序。
已知 函数原型:char* MyStrcat(char *dest, char *source);//函数返回连接后的字符串的首地址

#include <stdio.h>
#include <string.h>
int main(void)
{
        char *first, *second, *result;
        printf("Input the first string:\n");
        gets(first);
        printf("Input the second string:\n");
        gets(second);
        result = MyStrcat(first, second);
        printf("The result is : %s\n", result);
        return 0;
}
char* MyStrcat(char *dest, char *source)
{
        int i = 0;
        while (*(dest+i)!='\0')   i++;
        for (; *(source+i)!='\0'; i++)
        {
            *(dest+i) = *(source+i);   
        }
        return dest;
}
  1. 程序运行结果示例1:
    Input the first string:
    fri↙
    Input the second string:
    end↙
    The result is : friend
    程序运行结果示例2:
    Input the first string:
    home↙
    Input the second string:
    work↙
    The result is : homework
    输入提示信息:“Input the first string:\n”
    输入提示信息:“Input the second string:\n”
    输入格式: 使用gets()函数
    输出格式:“The result is : %s\n”

代码实现

#include <stdio.h>
#include <string.h>
int main(void)
{
        char first[200], second[200];
        int i=0,y=0;
        printf("Input the first string:\n");
        gets(first);
        printf("Input the second string:\n");
        gets(second);
        while (*(first+i)!='\0')   i++;
        while (*(second+y)!='\0')   y++;
        for (y=0; *(second+y)!='\0'; y++)
        {
            *(first+i+y) = *(second+y);
        }
        *(first+i+y)=='\0';
        printf("The result is : %s\n", first);
        return 0;
}

4. 程序改错——2


题目内容
下面程序的功能是输出如示例所示形式的数据,目前程序中存在错误,找到错误的原因后,请修改正确,并按照给出的程序运行结果示例检查你的程序。
程序中用到的 函数原型如下:
void YH(int a[][ARR_SIZE], int n);
void PrintYH(int a[][ARR_SIZE], int n);
程序运行结果示例:
1
1 1
1 2 1
1 3 3 1

#include<stdio.h>
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void YH(int a[][ARR_SIZE], int n)
{
        int  i, j ;
        for (i=1; i<=n; i++)
        {  
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<=n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }       
        }
}
void PrintYH(int a[][ARR_SIZE], int n)
{
        int i , j ;
        for (i=1; i<n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
            }
             printf("\n");
        }
  1. 输入格式: 无
    输出格式: “%4d”

代码实现

#include<stdio.h>
#define  ARR_SIZE  5
void  YH(int a[][ARR_SIZE], int  n);
void  PrintYH(int a[][ARR_SIZE], int  n);
int main(void)
{
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void  YH(int a[][ARR_SIZE], int  n)
{
        int  i, j ;
        for (i=1; i<n; i++)
        {
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
}
void  PrintYH(int a[][ARR_SIZE], int  n)
{
        int i , j ,t=1;
        for (i=1; i<n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
                if(j==n-1)
                {
                    t=0;
                }
            }
            if(t==1)
             {
                 printf("\n");
             }
        }
}

5. 出售金鱼


题目内容
买买提将养的一缸金鱼分五次出售:第一次卖出全部的一半加二分之一条;第二次卖出余下的三分之一加三分之一条;第三次卖出余下的四分之一加四分之一条;第四次卖出余下的五分之一加五分之一条;最后卖出剩下的11条。问原来鱼缸中共有几条鱼?
输入格式: 无
输出格式:“There are %d fishes at first.\n”

代码实现

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

int main()
{
    float s=11,i;
    for(i=4;i>0;i--)
    {
        s+=1/(i+1);
        s*=(i+1)/i;
    }
    printf("There are %d fishes at first.\n",(int)s);
    return 0;
}

6. 找最值


题目内容
从键盘任意输入10个整数,用指针变量作函数参数编程计算最大值和最小值,并返回它们所在数组中的位置。函数原型如下所示:
int FindMax(int num[], int n, int *pMaxPos);//函数返回最大值,pMaxPos返回最大值所在的下标
int FindMin(int num[], int n, int *pMinPos);//函数返回最小值,pMaxPos返回最小值所在的下标
程序运行结果示例:
Input 10 numbers:
-1 2 3 45 92 8 9 12 7 8↙
Max=92,Position=4,Min=-1,Position=0
输入提示信息:“Input 10 numbers:\n”
输入格式: “%d”
输出格式:“Max=%d,Position=%d,Min=%d,Position=%d\n”

代码实现

#include <stdio.h>
#include <stdlib.h>
int FindMax(int num[], int n, int *pMaxPos);
int FindMin(int num[], int n, int *pMinPos);
int main()
{
    int a[10],i,b=0,c=0,w,e;
    printf("Input 10 numbers:\n");
    for(i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
    }
    w=FindMax(a,10,&b);
    e=FindMin(a,10,&c);
    printf("Max=%d,Position=%d,Min=%d,Position=%d\n",w,b,e,c);
    return 0;
}
int FindMax(int num[], int n, int *pMaxPos)
{
    int max=num[0],i;
    for(i=0;i<n-1;i++)
    {
        if(max<=num[i+1])
        {
            max=num[i+1];
            *pMaxPos=i+1;
        }
    }
    return max;
}
int FindMin(int num[], int n, int *pMinPos)
{
    int min=num[0],i;
    for(i=0;i<n-1;i++)
    {
        if(min>=num[i+1])
        {
            min=num[i+1];
            *pMinPos=i+1;
        }
    }
    return min;
}

7. 杨辉三角形


题目内容
编程打印具有如下形式的杨辉三角形,其中输出数据的行数n从键盘输入,并且n<=10。
程序运行结果示例1:
Input n (n<=10):
5↙
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
程序运行结果示例2:
Input n (n<=10):
7↙
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
输入提示信息:“Input n (n<=10):\n”
输入格式: “%d”
输出格式
输出数据格式:"%4d"
数据换行: “\n”

代码实现

#include<stdio.h>
void  YH(int a[][10], int  n);
void  PrintYH(int a[][10], int  n);
int main(void)
{
    int ARR_SIZE,a[10][10];
    printf("Input n (n<=10):\n");
    scanf("%d",&ARR_SIZE);
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
}
void  YH(int a[][10], int  n)
{
        int  i, j ;
        for (i=1; i<=n; i++)
        {
             a[i][1] = 1;
             a[i][i] = 1;
        }
        for (i=3; i<=n; i++)
        {
            for (j=2; j<=i-1; j++)
            {
                 a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
        }
}
void  PrintYH(int a[][10], int  n)
{
        int i , j ;
        for (i=1; i<=n; i++)
        {
            for (j=1; j<=i; j++)
            {
                printf("%4d", a[i][j]);
            }
                 printf("\n");
        }
}

8. 颠倒句子中的单词顺序


题目内容
从键盘输入一个句子(假设字符数小于100个),句子中的单词之间用空格分隔,句子必须以一个标点符号作为结尾,句子开头和末尾标点符号前均没有空格,以回车表示输入结束,请编程颠倒句中的单词顺序并输出。
函数原型:int Inverse(char str1[], char str2[][N])

程序运行结果示例1:
Input a sentence: you can cage a swallow can’t you?↙
you can’t swallow a cage can you?
程序运行结果示例2:
Input a string: you are my sunshine!↙
sunshine my are you!
程序运行结果示例3:
Input a sentence: I love you!↙
you love I!
输入提示信息:“Input a sentence:”
输入格式: 用gets()函数
输出格式
每个单词的输出格式:"%s " (注意: %s后面有一个空格)
最后一个单词和标点符号的输出格式:"%s%c\n"

代码实现

#include <stdio.h>
#include <stdlib.h>
int Inverse(char str1[], char str2[][100]);
int main()
{
    char str1[100],str2[100][100],a;
    int i;
    printf("Input a sentence:");
    gets(str1);
    a=str1[strlen(str1)-1];
    str1[strlen(str1)-1]='\0';
    i=Inverse(str1,str2);
    for(;i>0;i--)
    {
        printf("%s ",str2[i]);
    }
    printf("%s%c\n",str2[i],a);
    return 0;
}
int Inverse(char str1[], char str2[][100])
{
    int i=0,j=0,k=0;
    while(str1[i]!='\0')
    {
        k=0;
        while(str1[i]!=' '&&str1[i]!='\0')
        {
            str2[j][k]=str1[i];
            k++;
            i++;
        }
        if(str1[i]=='\0') break;
        str2[j][k]='\0';
        j++;
        i++;
    }
    return j;
}


http://www.niftyadmin.cn/n/1237037.html

相关文章

教你零基础制作3D盒子相册(送给那个她~)

3D立体相册&#xff1f;   是的&#xff0c;相信大家或多或少的会在一些地方看到某些程序员用代码给自己女朋友写3D立体相册&#xff0c;那么你会不会也想自己手动敲出那些炫酷的代码呢&#xff1f;   那你是找对地方了。废话不多说&#xff0c;今天为大家分享利用html实现…

你真的了解String与StringBuffer的区别吗

String与StringBuffer的区别   首先&#xff0c;这两个类都是和字符串有关的。从它们最直接的区别来看&#xff0c;就是对它们创建的时候不一样。String这个类可以用等号直接来赋值&#xff0c;而StringBuffer不行&#xff0c;它只能用调用new构造方法才行&#xff0c;当然&a…

几种方法教你JAVA判断字符串是否为数字

在进行JAVA习题的时候&#xff0c;经常会遇到提取字符串中的数字的问题&#xff0c;怎么解决呢&#xff1f;这里提供几种方法&#xff1a;   几种方法由自己写的方法向JAVA自带的函数转向&#xff0c;代码的执行速度越来越快&#xff0c;执行效率越来越高。 其中第一个方法为…

细谈typedef和define的区别

对于都可以用来给对象取一个别名的Typedef和define来说&#xff0c;是有区别的。本文通过对typedef和define的介绍&#xff0c;来给大家详细的讲解它们存在的本质区别&#xff0c;供参考。 typedef是一种在计算机编程语言中用来声明自定义数据类型&#xff0c;配合各种原有数据…

你真的了解JAVA数组吗?(上)

什么是数组&#xff1f;   数组是相同类型的变量按顺序组成的一种复合数据类型&#xff0c;这些相同类型的变量称为数组的元素或单元。数组通过数组名加索引来使用数组的元素&#xff0c;索引从 0 开始。 1.数组的声明   声明数组包括声明数组的名字、数组元素的数据类型。…

哈工大C语言程序设计精髓第十一周

由于这些代码也是我初学时写的代码&#xff0c;故其中的规范程度及简洁程度并不很好&#xff08;此处我后来写的有可以参考一下->C语言代码规范&#xff09;&#xff0c;但是能很好的接近出初学者的水平&#xff0c;也更有参考价值&#xff01;排版不易&#xff0c;喜欢就点…

教你怎么用cmd 运行带包的java文件!!!

超详细&#xff01; 有三种方法&#xff0c;但是其实本质都一样&#xff0c;都是让javac命令找到所需编译的源文件&#xff0c;然后执行按照classpath寻找要执行的class文件。&#xff08;ps:以下三种方法都是已经进了对应的文件夹后操作的&#xff09; 方法一&#xff1a;直接…

哈工大C语言程序设计精髓第十二周

由于这些代码也是我初学时写的代码&#xff0c;故其中的规范程度及简洁程度并不很好&#xff08;此处我后来写的有可以参考一下->C语言代码规范&#xff09;&#xff0c;但是能很好的接近出初学者的水平&#xff0c;也更有参考价值&#xff01;排版不易&#xff0c;喜欢就点…