【2021嵙大期末c语言】2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 1837 1840

 

 Problem IDTitle
Y2614 Problem A结构体占用的存储空间
Y2615 Problem B第二个数
Y2616 Problem C星期几
Y2617 Problem D倒数第二个数
Y2618 Problem E截取字符串
Y2619 Problem F矩阵元素求和
Y2620 Problem Gmax()函数(Append Code)
Y2621 Problem H递归求倒序数二(Append Code)
Y2622 Problem I数组元素计数 (Append Code)
Y2623 Problem J字符串的分隔 (Append Code)
Y2624 Problem K时间的顺序 (Append Code)
Y2625 Problem L有多少个1
Y1837 Problem M二进制串匹配
Y1840 Problem N幸运单词

 

2614Problem A: 结构体占用的存储空间

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 3026  Solved: 1581
[Submit][Status]

Description

有如下定义的结构体类型:
         struct student
         {
             int serial;
             double score;
             char level;
             char name[14];
         };
请输出该结构体类型所需占用的存储空间大小(以字节为单位)。

 

Input

无。

 

Output

         输出是一个整数,即结构体student 类型占用的字节数。

 

Sample Input

 

Sample Output

 

HINT

 

Append Code

考察sizeof的用法

#include <stdio.h>
#include <stdlib.h>
 
 
//有如下定义的结构体类型:
//
//请输出该结构体类型所需占用的存储空间大小(以字节为单位)。
//Input
//无。
//
//Output
//         输出是一个整数,即结构体student 类型占用的字节数。
//
//Sample Input
//Sample Output
//HINT
//Append Code
//[Submit][St
 
 struct student
         {
             int serial;
             double score;
             char level;
             char name[14];
         };
int main()
{
    struct student a;
    printf("%d",sizeof(a));
 
    return 0;
}

2615Problem B: 第二个数

 

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 1590  Solved: 1462
[Submit][Status]

Description

给出三个整数,输出其中的第二个数。

 

Input

输入为三个整数,两两整数之间用空格分开。

 

Output

输出为一个整数,是输入的第二个数。

 

Sample Input

183 547 29

Sample Output

547

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
 
 
 
int main()
{
    int a[3]={0};
    scanf("%d %d %d",&a[0],&a[1],&a[2]);
    printf("%d\n",a[1]);
    return 0;
}

2616 switch case的用法 后面只能加整型

Problem C: 星期几 

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2327  Solved: 1588
[Submit][Status]

Description

根据输入的表示星期几的数字,对应输出它的英文名称。

         1、星期一:Monday

         2、星期二:Tuesday

         3、星期三:Wednesday

         4、星期四:Thursday

         5、星期五:Friday

         6、星期六:Saturday

         7、星期日:Sunday

错误输入请输出:input error!

 

Input

输入为一个整数。

 

Output

对应输出英文名称。

 

Sample Input

1

Sample Output

Monday

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
 
 
 
int main()
{
    int a;
    scanf("%d",&a);
    switch(a)
    {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("Wednesday\n");
        break;
    case 4:
        printf("Thursday\n");
        break;
    case 5:
        printf("Friday\n");
        break;
    case 6:
        printf("Saturday\n");
        break;
    case 7:
        printf("Sunday\n");
        break;
    default:
        printf("input error!\n");
        break;
    }
    return 0;
}

2617多次提交

Problem D: 倒数第二个数

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 6335  Solved: 2023
[Submit][Status]

Description

给出若干整数,输出其中的倒数第二个数。

 

Input

输入为若干(至少两个)整数,至EOF结束,两两整数之间用空格分开。

 

Output

输出为一个整数,是输入的倒数第二个数。

 

Sample Input

3 0 8 7 5 9

Sample Output

5

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
    
    
    
int main()
{
    int a[100000]={0};
    int i=0;
    while( scanf("%d",&a[i])!=EOF)
    {
//       if(a[i]==-1)
//            break;
        i++;
    
    }
    printf("%d\n",a[i-2]);//结果是i-2的问题 不能用i-1but why
    return 0;
}

2618多次提交

Problem E: 截取字符串

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 5221  Solved: 1219
[Submit][Status]

Description

对给定的一个字符串,截取其中一部分输出。

 

Input

输入为两行,第一行为一个字符串s,长度至少为1且不超过20个字符;第二行为两个整数p、q,满足p<=q。

 

Output

输出s中下标从p到q的那部分,且p或q在s下标范围外的部分不会产生输出。若s[p]到s[q]不产生任何输出,则仅输出“[]”。

 

Sample Input

communication -10 10

Sample Output

[communicati]

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
  
  
//对给定的一个字符串,截取其中一部分输出。
//
//输入为两行,第一行为一个字符串s,
//长度至少为1且不超过20个字符;
//第二行为两个整数p、q,满足p<=q。
//输出s中下标从p到q的那部分,
//且p或q在s下标范围外的部分不会产生输出。
//若s[p]到s[q]不产生任何输出,则仅输出“[]”。
//
//Sample Input
//communication
//-10 10
//Sample Output
//[communicati]
//HINT
//Append Code
  
int main()
{
    char s[21]="\0";
    gets(s);
    int i;
    int len=strlen(s);
//  for(i=0;i<len;i++)
//  {
//      printf("%c",s[i]);
//  }
    int p,q;
    scanf("%d %d",&p,&q);
    if(p<0)
        p=0;
    if(q>(len-1))
        q=(len-1);
  
    printf("[");
    for(i=p;i<=q;i++)
    {
        printf("%c",s[i]);
    }
    printf("]");
}
  

2619二维数组

Problem F: 矩阵元素求和

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2053  Solved: 1390
[Submit][Status]

Description

输入一个n阶方阵a,输出其部分元素之和。

 

Input

输入为两部分。第一部分第一行为一个整数n(n<=10),后接n行n列个数字,表示n阶方阵。第二部分为若干行,至EOF结束,每行四个整数,前两个整数为求和区域左上角元素坐标p、q,后两个为右下角坐标r、s。输入数据保证p、q、r、s均在方阵的有效下标范围内。

 

 

Output

每行输出一个整数m,对应输入第二部分的每一行,m为方阵a[p][q]~a[r][s] 区域内所有元素之和。

 

Sample Input

5 13 66 53 89 55 11 72 61 62 46 90 10 38 10 55 95 20 43 27 19 60 12 66 16 96 1 1 3 2 0 0 4 0 2 1 2 4

Sample Output

244 269 113

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
 
 
 
//二维数组
int main()
{
    int n;
    int a[10][10];
    scanf("%d",&n);
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    int sum;
    int p1,p2,q1,q2;
    while(scanf("%d %d %d %d",&p1,&p2,&q1,&q2)!=EOF)
    {
        sum=0;
        for(i=p1;i<=q1;i++)
        {
 
            for(j=p2;j<=q2;j++)
            {
                sum+=a[i][j];
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

2620

Problem G: max()函数(Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2658  Solved: 1353
[Submit][Status]

Description

编写一个函数max(),返回三个参数中的最大整数。

 

max()函数的调用格式见append.c

Input

输入为三个整数。

Output

输出为三个整数中的最大值。

Sample Input

15 10 20

Sample Output

20

HINT

 

Append Code

int main()
{
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    printf("%d\n", max(a, b, c));
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
 
int max(int a,int b,int c)
{
    int mm=a;
    if(b>mm)
        mm=b;
    if(c>mm)
        mm=c;
    return mm;
}
 
int main()
{
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    printf("%d\n", max(a, b, c));
}

 

2621递归倒序

Problem H: 递归求倒序数二(Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2077  Solved: 1152
[Submit][Status]

Description

         给出一个无符号整数n<264,求出n的倒序数字。

 

         请根据“Append Code”完成程序。append.c中将n输入到一个字符串num中,调用了一个函数rev (num, i),用来倒转输出数字n,其中i为串的下标。用C语言或C++编写自定义类型和函数实现,函数的原型为:

                  int rev(char s[], int i);

         函数的调用格式见“Append Code”。

         -----------------------------------------------------------------------------

         Invalid Word(禁用单词)错误:在解决这个题目时,某些关键词是不允许被使用的。如果提交的程序中包含了下列的关键词之一,就会产生这个错误。

 

         被禁用的关键字:for, while, do, break, continue, goto。

 

Input

         输入一个小于264的无符号整数n。

 

Output

         输出n的倒序数。

 

Sample Input

110

Sample Output

011

HINT

 

Append Code

int main()
{
    char num[MAX_LEN];
    gets(num);
    rev(num, 0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
#define MAX_LEN 1000
 
 
void rev(char s[],int i)//i表示下标
{
    int len=strlen(s);
    if(i<len)
    {printf("%c",s[len-1-i]);
    rev(s,i+1);}
}
 
 
 
int main()
{
    char num[MAX_LEN];
    gets(num);
    rev(num, 0);
}
 

2622

Problem I: 数组元素计数 (Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2052  Solved: 1565
[Submit][Status]

Description

输出一个数组中与指定数字相同的元素的个数。

-----------------------------------------------------------------------------

 结合“Append Code”中的代码,编写以下函数:

 原型:int count_array(int a[], int n, int v);

 功能:从数组a[]中统计与v相同的元素个数并返回,其中n是数组a[]的长度。

 

Input

输入为三行整数,第一行为n,表示数组长度(n<=100);第二行为n个整数;第三行为一个整数,为指定数字m。

 

Output

输出为n个数中与m相同的数的个数。

 

Sample Input

3 2 3 2 2

Sample Output

2

HINT

Append Code

int main()
{
    int a[100], i, n, val, cnt;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
        scanf("%d", &a[i]);
    scanf("%d", &val);
    cnt = count_array(a, n, val);
    printf("%d\n", cnt);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
int count_array(int a[], int n, int v)
{
    int cnt=0;
    int i;
    for(i=0;i<n;i++)
    {
        if(a[i]==v)
            cnt++;
    }
    return cnt;
 
}
 
int main()
{
    int a[100], i, n, val, cnt;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
        scanf("%d", &a[i]);
    scanf("%d", &val);
    cnt = count_array(a, n, val);
    printf("%d\n", cnt);
}

 

2623字符串数组记得补结束符

Problem J: 字符串的分隔 (Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2659  Solved: 1140
[Submit][Status]

Description

将输入的一个字符串s中按下标分隔为两个串,一个由奇数下标的字符(s[1],s[3],s[5]…)按顺序的组成,一个由偶数下标的字符(s[0],s[2],s[4]…)按顺序的组成。

-----------------------------------------------------------------------------

编写一个函数str_sep():

原型:void  str_sep(char s[],  char t[],  char r[]);

功能:把串r中的字符按奇偶数分隔为s、t,偶数下标的放入s中,奇数下标的放入t中。

函数的调用格式见“Append Code”。

Input

输入为一个串s。输入最少为一个字符,最多不会超过100个字符。

Output

输出为转换后的串。

Sample Input

abcde1357

Sample Output

ace37 bd15

HINT

Append Code

int main()
{
    char r[101], s[101], t[101];
    gets(r);
    str_sep(s, t, r);
    puts(s);
    puts(t);
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
void  str_sep(char s[],  char t[],  char r[])
{
    int i;
    int a=0,b=0;
    int len=strlen(r);
    //s偶 t奇
    for(i=0;i<len;i++)
    {
        if(i%2==0)//偶数下标
        {
            s[a]=r[i];
            a++;
        }
        else
        {
            t[b]=r[i];
            b++;
        }
    }
    //wa是因为忘了这个
    //只要是字符串数组一定记得补结束符
    s[a]='\0';
    t[b]='\0';
}
 
int main()
{
    char r[101], s[101], t[101];
    gets(r);
    str_sep(s, t, r);
    puts(s);
    puts(t);
    return 0;
}

2624

Problem K: 时间的顺序 (Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 1488  Solved: 869
[Submit][Status]

Description

输入不超过100个(24小时制)时间,把它们按从前到后进行排序。

-----------------------------------------------------------------------------

至少编写三个函数完成程序:

原型:int get_time(struct time tm[]);

功能:按格式从输入读取若干时间存入结构体数组tm[]中,返回值为实际输入的时间个数。

原型:int sort_time(struct time tm[], int n);

功能:将结构体数组tm中的时间按从小到大排序。

原型:int put_time(struct time tm[], int n);

功能:将结构体数组tm中的时间按顺序输出,格式见样例。

函数的调用格式见“Append Code”,结构体“struct time”的类型定义根据“Append Code”自行给出。

 

Input

输入为两部分,第一部分为整数n,n<=100,第二部分为n行,每行三行个整数,表示时间的小时、分钟、秒。

 

 

Output

输出为n个从小到大的时间,格式为”hh:mm:ss”,时分秒均输出2位,不足前补0。

 

Sample Input

5 12 0 30 12 3 30 11 1 30 11 3 20 12 3 20

Sample Output

11:01:30 11:03:20 12:00:30 12:03:20 12:03:30

HINT

 

Append Code

int main()
{
    struct time tm[100];
    int i, n;
    n = get_time(tm);
    sort_time(tm, n);
    put_time(tm, n);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
 
 
struct time{
int hh;
int mm;
int ss;
};
int get_time(struct time tm[])
{
    int n;
    scanf("%d",&n);
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d %d %d",&tm[i].hh,&tm[i].mm,&tm[i].ss);
    }
    return n;
}
int sort_time(struct time tm[], int n)
{
 
    struct time t;
    int i,j;
    //冒泡从小到大排序
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(tm[j].hh>tm[j+1].hh)
            {
                t=tm[j];
                tm[j]=tm[j+1];
                tm[j+1]=t;
            }
            else if(tm[j].hh==tm[j+1].hh && tm[j].mm>tm[j+1].mm)
            {
                t=tm[j];
                tm[j]=tm[j+1];
                tm[j+1]=t;
            }
            else if(tm[j].hh==tm[j+1].hh && tm[j].mm==tm[j+1].mm&&tm[j].ss>tm[j+1].ss)
            {
                t=tm[j];
                tm[j]=tm[j+1];
                tm[j+1]=t;
            }
        }
    }
}
int put_time(struct time tm[], int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%02d:%02d:%02d\n",tm[i].hh,tm[i].mm,tm[i].ss);
    }
}
//24小时制时间
//先判断小时 小时越大越靠后
//小时之后分钟 同分钟判断秒
 
int main()
{
    struct time tm[100];
    int i, n;
    n = get_time(tm);
    sort_time(tm, n);
    put_time(tm, n);
}
 

2625进制转换

Problem L: 有多少个1

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 1930  Solved: 1185
[Submit][Status]

Description

给出一个十进制非负整数,求其二进制表示中有多少个1。

 

Input

输入为若干int范围内的整数,每行一个为n至EOF结束。

 

 

Output

输出对应输入每个n的二进制中1的个数。

Sample Input

0 1 2 3

Sample Output

0 1 1 2

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
 
 
int main()
{
    int a;
    int s,y;//商和余数
    int cnt;
 
    while(scanf("%d",&a)!=EOF)
    {
        cnt=0;
       while(a>0)
       {
           y=a%2;
           a/=2;
           if(y==1)
            cnt++;
       }
        printf("%d\n",cnt);
    }
    return 0;
}
 

1837

Problem M: 二进制串匹配

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 1194  Solved: 817
[Submit][Status]

Description

给定两个二进制串A和B,只有“0”和“1”组成。现在的任务是求出串A在串B中出现多少次?

例如,串B是“1001110110”而串A为“11”,你应该输出3。

Input

第一行为一个整数n,表示下面有n组样例。在每一组样例中有两行,第一行给出了二进制串A,A长度不超过10,第二行给出了二进制串B,B的长度不超过1000。并且保证B总是长于A。

Output

 对于每一组输入,输出一行包含一个整数,表示A在B中出现的次数。

Sample Input

3 11 1001110110 101 110010010010001 1010 110100010101011

Sample Output

3 0 3

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
 
 
int main()
{
    char a[11]="\0";
    char b[1002]="\0";
    int n;
    scanf("%d",&n);
    int i,j;
    int lena,lenb;
    int flag,cnt,p;
    for(i=0;i<n;i++)
    {
        scanf("%s",&a);
        scanf("%s",&b);
 
        lena=strlen(a);
        lenb=strlen(b);
        cnt=0;
        for(j=0;j<lenb;j++)//遍历b里的每个字符
        {
            flag=1;
            for(p=0;p<lena;p++)
            {
                if(b[j+p]!=a[p])
                    flag=0;
            }
            if(flag==1)
                cnt++;
        }
        printf("%d\n",cnt);
    }
 
    return 0;
}

1840调试很久 注意只能是出现过的字母 所以不能为空

Problem N: 幸运单词

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 1993  Solved: 759
[Submit][Status]

Description

小明的词汇量很小,所以每次做英语选择题的时候都很头疼。但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大!
这种方法的具体描述如下:假设maxn是单词中出现次数最多的字母的出现次数,minn是单词中出现次数最少的字母的出现次数,如果maxn-minn是一个质数,那么小明就认为这是个Lucky Word,这样的单词很可能就是正确的答案。

Input

第一行数据N(0<N<100)表示测试数据组数。
每组测试数据输入只有一行,是一个单词,其中只可能出现小写字母,并且长度小于100。

Output

每组测试数据输出共两行,第一行是一个字符串,假设输入的的单词是Lucky Word,那么输出“Lucky Word”,否则输出“No Answer”;
第二行是一个整数,如果输入单词是Lucky Word,输出maxn-minn的值,否则输出0

Sample Input

2 error olympic

Sample Output

Lucky Word 2 No Answer 0

HINT

 

Append Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
 
 
 
 
int isprime(int a)
{
    int i;
    int prime=1;//是素数
    if(a==0 || a==1)
        prime=0;
    else
    {
        for(i=2;i<=sqrt(a);i++)
        {
            if(a%i==0)
            prime=0;
 
        }
 
    }
 
    return prime;
}
 
void swap(int *a,int *b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}
int main()
{
    int max,min;//字母的最大出现次数和最小出现次数
    int n;
    scanf("%d",&n);//n组测试用例
    int i;
    char s[101]="\0";
    char b[27]="abcdefghijklmnopqrstuvwxyz";
    int c[26]={0};
    int len;
    int j,p,flag,d;
    for(i=0;i<n;i++)
    {
        for(j=0;j<26;j++)
        {
            c[j]=0;
        }
        scanf("%s",&s);
        len =strlen(s);
        for(j=0;j<len;j++)//遍历s
        {
            for(p=0;p<26;p++)
            {
                if(b[p]==s[j])
                    {c[p]++;
                    continue;}
            }
        }
        for(j=0;j<26;j++)
        {
            if(c[j]!=0)
            {
                max=c[j];
                min=c[j];
                break;
            }
        }
        for(j=0;j<26;j++)
        {
            if(c[j]>max && c[j]!=0)
                max=c[j];
            if(c[j]<min && c[j]!=0)
                min=c[j];
        }
 
        //顺序排好
//        for(j=0;j<25;j++)
//        {
//            for(p=0;j<25-i;j++)
//            {
//                if(c[j]>c[j+1])
//                {
//                    flag=1;
//                    swap(&c[j],&c[j+1]);
//                }
//            }
//            if(flag==0) break;
//            else flag=0;
//        }
        d=max-min;
        if(isprime(d))
        {
            printf("Lucky Word\n");
            printf("%d\n",d);
        }
        else{
            printf("No Answer\n");
            printf("0\n");
        }
    }
 
 
 
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值