嵙大2022c语言期末 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 1510

 

  Problem ID Title
Y 2683 Problem A 简单计算二
Y 2684 Problem B 奇偶求和
Y 2685 Problem C 打印十字型
Y 2686 Problem D 简单计算二(Append Code)
Y 2687 Problem E 求数组中最大最小值(Append Code)
Y 2688 Problem F 字符串转日期(Append Code)
Y 2689 Problem G 递归求斐波那契数
Y 2690 Problem H 与7无关的数
Y 2691 Problem I 十六进制转十进制
Y 2692 Problem J 尼科梅彻斯定理
Y 2693 Problem K 求下一天
Y 2694 Problem L 查找单词
Y 1510 Problem M 猴子选大王

 

2683

Problem A: 简单计算二

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2238  Solved: 1678
[Submit][Status]

Description

         给定三个整数a、b、c,计算表达式a / (b + c)的值;其中“/”是浮点运算。

 

Input

         输入为一行,包括三个数值较小整数a、b、c,且b + c不为零。

 

Output

         输出一行,即表达式的计算结果,要求精确到小数点后3位。

 

Sample Input

1 1 3

Sample Output

0.250

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
 
 
int main()
{
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    double d=1.0*a/(b+c);
    printf("%.3f\n",d);
    return 0;
}

2684

Problem B: 奇偶求和

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2724  Solved: 1778
[Submit][Status]

Description

         输入若干整数,求其中所有奇数之和与所有偶数之和。

 

Input

         输入只包含若干整数,至EOF结束。所有运算不会超过int存储范围。

 

Output

         输出两个整数,分别是输入的所有奇数之和与所有偶数之和。

 

Sample Input

1 2 3 4 5

Sample Output

9 6

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
 
 
int main()
{
    int a;
    int sumj=0,sumo=0;;
    while(scanf("%d",&a)!=EOF)
    {
        if(a%2==0)
        {
            sumo+=a;
        }
        else
            sumj+=a;
    }
    printf("%d %d\n",sumj,sumo);
    return 0;
}

2685

Problem C: 打印十字型

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 4056  Solved: 1810
[Submit][Status]

Description

         从键盘输入一个整数n(1≤n≤10),打印出指定的十字图形。

 

Input

         输入正整数n(1~10)。

 

Output

         n阶十字型。

 

Sample Input

3

Sample Output

+ + +++++ + +

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
 
 
int main()
{
    int n;
    scanf("%d",&n);
    int i,j;
    if(n==1)
        printf("+");
    else{
          for(i=1;i<=(n-1);i++)//行
    {
        for(j=1;j<=(n-1);j++)
            printf(" ");
 
        printf("+");
 
        for(j=1;j<=(n-1);j++)
            printf(" ");
        printf("\n");
    }
    for(i=1;i<=(2*n-1);i++)
        printf("+");
    printf("\n");
    for(i=1;i<(n-1);i++)//下半部分带空格的行
    {
        for(j=1;j<=(n-1);j++)
            printf(" ");
 
        printf("+");
 
        for(j=1;j<=(n-1);j++)
            printf(" ");
        printf("\n");
    }
    //最后一行不带换行符单独拎出来
    for(j=1;j<=(n-1);j++)
        printf(" ");
 
        printf("+");
 
    for(j=1;j<=(n-1);j++)
            printf(" ");
    }
 
    return 0;
}

2686

 

Problem D: 简单计算二(Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2294  Solved: 1380
[Submit][Status]

Description

         给定三个整数a、b、c,计算表达式a / (b + c)的值;其中“/”是浮点运算。

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

         编写函数 f():

                   原型:void f(int a, int b, int c);

                   功能:根据题意完成计算和输出。

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

 

Input

         输入为一行,包括三个数值较小整数a、b、c,且b + c不为零。

 

Output

         输出一行,即表达式的计算结果,要求精确到小数点后3位。

 

Sample Input

1 1 3

Sample Output

0.250

HINT

 

Append Code

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    f(a, b, c);
    return 0;
}
#include 
#include 
#include 
#include 
#include 
 
void f(int a, int b, int c)
{
    double d=1.0*a/(b+c);
    printf("%.3f\n",d);
}
 
 
int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    f(a, b, c);
    return 0;
}

2687冒泡排序

Problem E: 求数组中最大最小值(Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2545  Solved: 1257
[Submit][Status]

Description

         给出若干整数,求其中的最大值和最小值。

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

         编写函数 max_min ():

                   原型:根据“Append Code”进行设计。

                   功能:把数组ar中的最大值和最小值找出来跟别存入max和min。

 

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

 

Input

         首先输入一个整数n(n<100),然后输入n个整数。

 

Output

         输出n个整数中的最小值和最大值。

 

Sample Input

5 1 2 3 4 5

Sample Output

1 5

HINT

 

Append Code

int main()
{
    int max, min, ar[100], i, n;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
        scanf("%d", &ar[i]);
    max_min(&max, &min, ar, n);
    printf("%d %d\n", min, max);
    return 0;
}
#include 
#include 
#include 
#include 
#include 
 
 
void swap(int *a,int *b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}
 
 
void max_min(int *max,int *min, int ar[], int n)
{
    int i,j;
    int flag=1;
    for(i=0;iar[j+1])//这里 核心还是冒泡没学会
                {swap(&ar[j],&ar[j+1]);
                flag=0;}
        }
        if(flag==1)
            break;
        else
            flag=1;
    }
    *min=ar[0];
    *max=ar[n-1];
}
 
int main()
{
    int max, min, ar[100], i, n;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
        scanf("%d", &ar[i]);
    max_min(&max, &min, ar, n);
    printf("%d %d\n", min, max);
    return 0;
}

2688

Problem F: 字符串转日期(Append Code)

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2026  Solved: 1225
[Submit][Status]

Description

         对于用字符串存储的日期,把它的年月日提取出来转换成结构体形式。

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

         编写函数 to_date ():

                   原型:根据“Append Code”进行设计。

                   功能:把字符串s中年月日信息提取出来,并用结构体变量返回。

         函数的调用格式见“Append Code”,完成结构体类型的定义。

 

Input

         输入若干字符串,每个一行。每个字符串为一个合法的日期,其中前4位表示年份,中间两位表示月份,后2位表示日,位数不足会补0。

 

Output

         按照年月日的顺序输出,两两用一个空格分开。

 

Sample Input

20011101 20220909

Sample Output

2001 11 1 2022 9 9

HINT

 

Append Code

int main()
{
    char str[10];
    struct date dt;
    while(gets(str) != NULL)
    {
        dt = to_date(str);
        printf("%d %d %d\n", dt.y, dt.m, dt.d);
    }
    return 0;
}
#include 
#include 
#include 
#include 
#include 
 
struct date{
int d;
int m;
int y;
 
};
struct date to_date(char s[])
{
    struct date a;
    int i;
    a.y=0;
    a.m=0;
    a.d=0;
    for(i=0;i<4;i++)
    {
        a.y*=10;
        a.y+=s[i]-'0';
    }
    for(i=4;i<6;i++)
    {
        a.m*=10;
        a.m+=s[i]-'0';
 
    }
    for(i=6;i<8;i++)
    {
        a.d*=10;
        a.d+=s[i]-'0';
    }
    return a;
}
 
int main()
{
    char str[10];
    struct date dt;
    while(gets(str) != NULL)
    {
        dt = to_date(str);
        printf("%d %d %d\n", dt.y, dt.m, dt.d);
    }
    return 0;
}

2689不超时的斐波那契递归

Problem G: 递归求斐波那契数

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 5709  Solved: 955
[Submit][Status]

Description

         斐波那契数列是1、2、3、5、8、13……。其中,每项是前两项之和。

         现在请你用递归方法编程求斐波纳契数列第n项。

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

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

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

 

Input

         输入一个整数n。

 

Output

         输出斐波那契数列的第n项。

 

Sample Input

5

Sample Output

8

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
long long a[10000]={0};
long long f(int n)
{
    if(a[n]!=0)//如果已经存入数值
        return a[n];
//剩下的都一样
    if(n==1)
        return 1;
    else if(n==2)
        return 2;
    else
    {
        a[n]=f(n-1)+f(n-2);    }
    return a[n];
}
 
int main()
{
    int n;
    scanf("%d",&n);
    printf("%lld\n",f(n));
    return 0;
}
 

2690

Problem H: 与7无关的数

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 3131  Solved: 1316
[Submit][Status]

Description

         一个整数如果每位数字不是7,同时既不是7的倍数,也不是7的平方、立方、四次方……。那么这个整数是与7无关的数

 

Input

         输入若干int范围内的整数,至EOF结束。

 

Output

         若是与7无关的数输出YES,否则输出NO。

 

Sample Input

7 21 49 58 73 101

Sample Output

NO NO NO YES NO YES

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
int main()
{
    int n;
    int a;
    int f=0;
    while(scanf("%d",&n)!=EOF)
    {
        f=0;
        //是不是7den次方
        if(n%7==0)
        {
            f=1;
            printf("NO\n");
            continue;
        }
        //如果不是的话继续判断里面有没有7
        while(n>0 && f==0)//之前提交不上是因为这里没有f==0 可能重判断了
        {
            a=n%10;
            n/=10;
            if(a==7)
            {
                f=1;
                printf("NO\n");
            }
        }
        if(f==0)
        printf("YES\n");
    }
 
    return 0;
}

2691

Problem I: 十六进制转十进制

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 1992  Solved: 1289
[Submit][Status]

Description

         十六进制是计算机中数据的一种表示方法,与十进制的对应关系是:十六进制的0~9对应十进制数值0-9,A~F对应十进制数值10-15。

         现在你编写一个程序,完成一个十六进制数到十进制表示的转换。

 

Input

         输入为多行,至EOF结束。每行为一个十六进制无符号整数n,n不超过int范围。n只包括数字0~9和大写字母A~F。

 

Output

         输出有多行,每一行输出与输入相对应,是n的十进制。

 

Sample Input

11 FF 379A

Sample Output

17 255 14234

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
int main()
{
    char s[100]="\0";
    int len ,i;
    int a;
    int sum=0;
    while(scanf("%s",&s)!=EOF)
    {
        sum=0;
        len=strlen(s);
        for(i=len-1;i>=0;i--)
        {
            //得到每一位的十进制表示
           if(s[i]>='A' && s[i]<='F')//65-90->10-15
           {
               a=s[i]-55;
               //printf("%d",a);
           }
            else
                a=s[i]-'0';
            sum+=a*pow(16,len-1-i);
        }
         printf("%d\n",sum);
    }
    return 0;
}
 

2692找规律型

Problem J: 尼科梅彻斯定理

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 2016  Solved: 1264
[Submit][Status]

Description

         任何一个正整数n的立方都可以唯一地写成n个相邻奇数之和。这就是尼科梅彻斯定理。例如,

         13 = 1

         23 = 8 = 3 + 5

         33 = 27 = 7 + 9 + 11

         43 = 64 = 13 + 15 + 17 + 19

现在你编程序计算n3是哪些相邻奇数之和。

 

Input

         输入一个正整数n,且n<1291。

 

Output

         输出为n个相邻整数,它们的和为n3。

 

Sample Input

5

Sample Output

21 23 25 27 29

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
int main()
{
    //如果是n前面就有1+2+3+。。。+n-1个数用过
    //也就是n*(n-1)/2个数
//    int a[900000]={0};
//    1 1
//    2 3
//    3 5
//    4 71
//    an里面存的是2*n-1
    int n,i;
    scanf("%d",&n);
    for(i=n*(n-1)/2+1;i

2693年月日型

Problem K: 求下一天

Time Limit: 1 Sec  Memory Limit: 2 MB
Submit: 3984  Solved: 1193
[Submit][Status]

Description

         给出一个日期,输出下一天的日期。

 

Input

         输入若干日期至EOF结束,格式为y-m-d,其中y、m、d是三个正整数表示年月日,均为合法日期,其中y的输入范围是1000~9999年。

 

Output

         输出每个日期下一天的日期。

 

Sample Input

2019-07-18 2022-01-01 2011-12-30 2033-11-30

Sample Output

2019-07-19 2022-01-02 2011-12-31 2033-12-01

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
//输出下一天
//先判断天数是否超过29 30 31
 
//判断是几月——根据第几天给月进位
    //如果是2月 判断是否是闰年
    //若是13578 10 12 31
    //如果是12月 判断是否年进位
//2019-07-18
//2022-01-01
//2011-12-30
//2033-11-30
//0101-01-01
int main()
{
    int yy,mm,dd;
    int ifrun=0;//1表示是闰年
    while(scanf("%d-%d-%d",&yy,&mm,&dd)!=EOF)
    {
        ifrun=0;
 
        if(yy%400==0 || (yy%4==0 && yy %100 !=0))
            ifrun=1;
 
        if(dd==28 && ifrun==0 &&mm==2)
        //在平年二月需要进位 其他任何时刻年不变月不变天数加一直接输出
        {
            printf("%d-03-01\n",yy);
        }
 
        else if(dd==29 && ifrun==1 && mm==2)
        //同上 仅闰年二月进位 其他任何时刻不变
        {
            printf("%d-03-01\n",yy);
        }
 
        else if(dd==30)
        //仅4 6 9 11 四个月需要进位 其他任何时刻天数+1不变
        {
            if(mm==4 || mm==6 || mm==9 ||mm==11)
            {
                printf("%d-%02d-01\n",yy,mm+1);
            }
            else
                printf("%d-%02d-%02d\n",yy,mm,dd+1);
        }
 
        else if(dd==31)//此时只有31天的月 必进位
        {
            if(mm==12)
            {
                printf("%d-01-01\n",yy+1);
            }
            else
                printf("%d-%02d-01\n",yy,mm+1);
        }
        else
            printf("%d-%02d-%02d\n",yy,mm,dd+1);
        //1 3 5 7 8 10均需月++
        //12月单独处理
    }
 
 
   return 0;
}

※(>^ω^<)2694非常天才的空格输出思路

Problem L: 查找单词

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 3165  Solved: 647
[Submit][Status]

Description

          给出n个英文单词,查找出其中正数或倒数第p个字符是c有哪些。

 

Input

          输入分为两部分。

         第一部分首先是一个整数n(n<=500),后接n个长度小于30的英文单词,这些单词均为小写字母组成。

         第二部分是不超过100行,每行是一次查找,至EOF结束。每次查找的输入为一个英文字母c和一个整数p,用一个分隔开。若p为正,则在n个单词中查找左起第p个字符为c的所有单词;若p为负,则在n个单词中查找右起第p个字符为c的所有单词。

Output

          输出为每次查找单词的结果,与输入的第二部分对应。每次查找的结果输出一行,若有多个单词符合查找条件,按照输入的顺序依次输出,两两单词间用一个空格分隔。若没有找到符合条件的单词,那么输出一个空行。

 

Sample Input

15 to too two teem tool taste tooth be bee bed box bend book below bench t 1 e 2 o 3 h -1 e -2 b -3

Sample Output

to too two teem tool taste tooth teem be bee bed bend below bench too two tool tooth book tooth bench teem bee bed bee bed box

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
 
//用二维数组更方便
//这个处理空格和换行的思路真的是教科书级别的
 
//处理空格:输出第一个之后马上标志位改变 只要标志位改变后就输出空格
//最后空行
int main() {
    char s[500][31]; // 存储单词
    int n, i;
 
    // 输入单词数量和单词列表
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%s", s[i]);
    }
 
    char c;
    int p;
    while (scanf(" %c%d", &c, &p) != EOF) 
    {
        int found = 0; // 标志是否找到符合条件的单词
        for (i = 0; i < n; i++)
        {
            int len = strlen(s[i]);
 
            // 根据 p 的正负判断查找方向
            if ((p > 0 && p <= len && s[i][p - 1] == c) || (p < 0 && -p <= len && s[i][len + p] == c)) 
            {
                if (found==1) 
                {
                    printf(" "); // 非第一个单词前输出空格
                }
                printf("%s", s[i]);
                found = 1;
            }
        }
 
 
            printf("\n"); // 找到单词后换行
    }
    return 0;
}

(>^ω^<)1510 猴子选大王约瑟夫问题的数组实现

Problem M: 猴子选大王

Time Limit: 2 Sec  Memory Limit: 16 MB
Submit: 6451  Solved: 3895
[Submit][Status]

Description

有一群进化程度很高的猴子,它们不再通过群殴产生猴王,而是采用一种非常文明的方法选出新的猴王。

假设猴群里有m只猴子,它们在篝火旁围坐成一个圈:某只猴子是1号,沿顺时针方向是2号、3号……m号,然后回到1号。由上一代猴王说出一个数字n,从1号猴子开始报数,报到n的猴子出局;再从刚出局猴子的下一个位置开始报数,报到n的猴子出局……如此重复,直至剩下一只猴子,它就成为新的猴王。

例如,当m=6、n=5时,依次出局的猴子序号是5、4、6、2、3,最后剩下1号是新猴王。

你来编写一个程序,模拟这个过程,算出第几号猴子会成为新的猴王。

 

Input

输入最多不超过100行,至EOF结束。每行一组数据,每组数据包含两个整数m和n(0

 

Output

输出与输入对应,每行输出一个整数k(1<=k<=m),表示第k号猴子成为新的猴王。

 

Sample Input

6 5 8 3 1 5 2 3

Sample Output

1 7 1 2

HINT

 

Append Code

#include 
#include 
#include 
#include 
#include 
 
 
int main()
{
    int m,n;
    int i;
    int monkeys[1000]={0};
    int remainmonkeys=0;
    int cnt=0;
    //一共m只猴子 放进数组 每只猴子对应标号1-m 在数组里对应下标0-m-1
    //出局:数组值由0变1 当数组里前m个里面只有一个为0的时候停止——选个东西记录剩下的猴子数量
    //计数法:当cnt等于n(报数到n)的时候 cnt重新从1开始
    //i++会超过猴子数量 所以要记得%m 保持一直在1和m 之间 ——过零处理
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        remainmonkeys=m;//剩下猴子的数量
        cnt=1;//初始计数wei 1
        //数组清零
        for(i=0;i1)
        {
            //这里的问题是如果里面存的不是0而是1 i就不会++了——把i++移到外面
            if(monkeys[i]==0 && cnt==n)
            {
                monkeys[i]=1;
                remainmonkeys--;
                cnt=1;//重新从1开始计数
            }
            else if(monkeys[i]==0)
            {
                cnt++;
            }
            i=(i+1)%m;//处理方式
        }
        for(i=0;i

 

 

你可能感兴趣的:(嵙大作业实验考试题,c语言)