杭电oj2011~2020


2011-多项式求和

Problem Description
多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + …
现在请你求出该多项式的前n项的和。

Input
输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。

Output
对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。

Example
Input
2
1 2

Output
1.00
0.50

思路要点
找规律

AC题解

#include <stdio.h>
int main()
{
    int t;
    scanf("%d", &t);
    while(t --)
    {
        int n, y = 0;
        double s = 0, x = 1;
        scanf("%d", &n);
        
        while(n --)
        {
            if(y%2 == 1) s -= 1/x ++;
            else s += 1/x ++;
            y ++;
        }
        printf("%.2lf\n", s);
    }
    return 0;
}

2012-素数判定

Problem Description
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

Example
Input
0 1
0 0

Output
OK

思路要点
判断素数
从2到根号n无可整除n的数

AC题解

#include <stdio.h>
#include <math.h>
int main()
{
	int x, y;
	while(~scanf("%d%d", &x, &y))
	{
		int flag = 1, n;
		if(x == y && y == 0) break;
		for(int i = x; i <= y; i ++)
		{
			n = i * i + i + 41;
			for(int j = 2; j < sqrt(n); j ++)
			{
				if(n%j == 0)
				{
					flag = 0;
					break;
				}
			}
			if(!flag) break;
		}
		if(flag) printf("OK\n");
		else printf("Sorry\n");
	}
	return 0;
}

2013-蟠桃记

Problem Description
喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵-
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?

Input
输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。

Output
对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Example
Input
2
4

Output
4
22

思路要点
——

AC题解

#include <stdio.h>
int main()
{
    int n;
    while(~scanf ("%d", &n))
    {
        int s = 1;
        while(--n)
        {
            s = (s + 1) * 2;
        }
        printf("%d\n", s);
    }
    return 0;
}

2014-青年歌手大奖赛_评委会打分

Problem Description
青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。

Input
输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。

Output
对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。

Example
Input
3 99 98 97
4 100 99 98 97

Output
98.00
98.50

思路要点
判断最大最小值

AC题解

#include <stdio.h>
int main()
{
    int n, a[200];
    while(~scanf ("%d", &n))
    {
        int s = 0;
        double v;
        for(int i = 1; i <= n; i ++)
        {
            scanf("%d", &a[i]);
            s += a[i];
        }
            
        int max = 0, min = a[1];
        for(int i = 1; i <= n; i ++)
        {
            if(max < a[i]) max = a[i];
            if(min > a[i]) min = a[i];
        }
        s = s - max - min;
        v = (1.0 * s)/(n - 2);
        printf("%.2lf\n", v);
    }
    return 0;
}

2015-偶数求和

Problem Description
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。

Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。

Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。

Example
Input
3 2
4 2

Output
3 6
3 7

思路要点

AC题解

#include <stdio.h>
int main()
{
    int n, m, i;
    int a[200], b[200];
    while(scanf("%d%d", &n, &m) != EOF)
    {
        int c = 0, s = 0, k = 0;
        for(i = 1; i <= n; i ++)
            a[i] = 2 * i;
        for(i = 1; i <= n; i ++)
        {
            c ++;
            s += a[i];
            if(c == m)
            {
                b[k ++] = s/m;
                s = 0;
                c = 0;
            }
        }
        
        if(n%m != 0)
        {
            b[k] = s/(n%m);
            k ++;
        }
        for(i = 0; i < k - 1; i ++)
            printf("%d ", b[i]);
            
        printf("%d\n", b[k - 1]);
    }
    return 0;
}

2016-数据的交换输出

Problem Description
输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。

Input
输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。

Output
对于每组输入数据,输出交换后的数列,每组输出占一行。

Example
Input
4 2 1 3 4
5 5 4 3 2 1
0

Output
1 2 3 4
1 4 3 2 5

思路要点

AC题解

#include <stdio.h>
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        if (n == 0) break;
        int a[200] = {0}, min, min_add = 0, t;
        for (int i = 0; i < n; i ++)
            scanf("%d", &a[i]);
        min = a[0];
        for (int i = 0; i < n; i ++)
            if (min > a[i])
            {
                min = a[i];
                min_add = i;
            } 
        t = a[min_add];
        a[min_add] = a[0];
        a[0] = t;
        for (int i = 0; i < n; i ++)
        {
            printf("%d", a[i]);
            if (i != n - 1) printf(" ");
        }    
        printf("\n");
    }
    return 0;
}

2017-字符串统计

Problem Description
对于给定的一个字符串,统计其中数字字符出现的次数。

Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Example
Input
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Output
6
9

思路要点

AC题解

#include <stdio.h>
int main ()
{
    int n;
    scanf ("%d", &n);
    getchar();
    while (n --)
    {
        char a[800] = {'\0'};
        int s = 0;
        gets(a);
        for (int i = 0; a[i] != '\0'; i ++)
            if (a[i] >= '0' && a[i] <= '9') s ++;
        printf("%d\n", s);
    }
    return 0;
}

2018-母牛的故事

Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Example
Input
2
4
5
0

Output
2
4
6

思路要点

AC题解

#include<stdio.h>
int main()
{
	int n, a[100] = {0, 1, 2, 3, 4};
	while(~scanf("%d",&n))
	{
		if(!n) break;
        for(int i = 5; i <= n; i ++)
		{
			a[i] = a[i - 1] + a[i - 3];
		}
		printf("%d\n", a[n]);
	}
	return 0;
}

2019-数列有序!

Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output
对于每个测试实例,输出插入新的元素后的数列。

Example
Input
3 3
1 2 4
0 0

Output
1 2 3 4

思路要点

AC题解

#include <stdio.h>
int main()
{
    int n, a[200], t;
    while(~scanf("%d%d", &n, &a[0]))
    {
        if (n == 0 && a[0] == 0) break;
        for (int i = 1; i <= n; i ++)
            scanf("%d", &a[i]);
        for (int i = 0; i <= n; i ++)
            for (int j = 0; j <= n - 1 - i; j ++)
                if(a[j] > a[j + 1])
                {
                    t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
        for (int i = 0; i <= n; i ++)
        {
            printf("%d", a[i]);
            if (i != n) printf(" ");
        }
            
        printf("\n");
    }
    return 0;
}

2020-绝对值排序

Problem Description
输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。

Input
输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。

Output
对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。

Example
Input
3 3 -4 2
4 0 1 2 -3
0

Output
-4 3 2
-3 2 1 0

思路要点

AC题解

#include <stdio.h>
int main()
{
    int n, a[200], b[200], t;
    while(~scanf("%d", &n))
    {
        if (n == 0) break;
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &a[i]);
            b[i] = a[i];
            if (b[i] < 0) b[i] = -1 * b[i];
        }
        for(int i = 0; i < n; i ++)
            for (int j = 0; j < n - 1 - i; j ++)
                if(b[j] < b[j + 1])
                {
                    t = b[j];
                    b[j] = b[j + 1];
                    b[j + 1] = t;
                    
                    t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }    
        for (int i = 0; i < n; i ++)
        {
            printf("%d", a[i]);
            if (i != n - 1) printf(" ");
        }
            
        printf("\n");
    }
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值