HDU 2012-2030、2032

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”,每组输出占一行。
 
Sample Input
0 1
0 0
 
Sample Output
OK
#include <stdio.h>
#include <math.h>
int main()
{
    int x = 0, y = 0;
    while (scanf("%d%d",&x,&y)&&x!=0,y!=0)//循环输入
    {
        
        if (x < -39 || y > 50 || x >= y)//判断x和y是否满足条件
            return 0;
        int tag = 1;//判断每一个数是否是质数
        for (int n = x; n <= y; n++)
        {
            int s = n * n + n + 41;
            for (int i = 2; i < sqrt(s); i++)
                if (s % i == 0)//如果不是质数,tag置为0
                    tag = 0;
        }
        if (tag)//根据tag判断是否是质数
            printf("OK");
        else
            printf("Sorry");
    }
    return 0;

}

 

2013:

Problem Description
喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵^-^
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?
 
Input
输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。
 
Output
对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。
 
Sample Input
2
4
 
Sample Output
4
22
#include<stdio.h>
int main()
{
    int i, n, sum;
    while (scanf("%d", &n) != EOF)

    {
        sum = 1;
        if (n > 1 && n < 30)
            for (i = 1; i < n; i++)
            {
                sum = (sum + 1) * 2;
            }
        printf("%d\n", sum);
    }
    return 0;
}

心得:递推

递推算法是一种用若干步可重复的简运算(规律)来描述复杂问题的方法.
 
  递推是序列计算机中的一种常用算法。它是按照一定的规律来计算序列中的每个项,通常是通过计算机前面的一些项来得出序列中的指定象的值。其思想是把一个复杂的庞大的计算过程转化为简单过程的多次重复,该算法利用了计算机速度快和不知疲倦的机器特点

2014:

Problem Description
青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。
 
Input
输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。
 
Output
对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。
 
Sample Input
3 99 98 97
4 100 99 98 97
 
Sample Output
98.00
98.50
使用指针:
#include<stdio.h>
#include<stdlib.h>
int cmp(const void* p1, const void* p2)
{
    return *(int*)p1 - *(int*)p2;
}
int main()
{
    int n, score[105], i;
    double s, aver;
    while (scanf("%d", &n) != EOF)
    {

        s = 0;
        for (i = 0; i < n; i++)
            scanf("%d", &score[i]);
        qsort(score, n, sizeof(score[0]), cmp);
        for (i = 1; i < n - 1; i++)
            s = s + score[i];
        aver = (double)s / (double)(n - 2.0);
        printf("%.2lf\n", aver);
    }
    return 0;
}

使用数组:

#include<stdio.h>
int main()
{
    int n, i, j, z, t, a[100], sum, max;
    double ave;
    while (scanf("%d", &n) != EOF)
    {
        sum = 0;
        for (i = 0; i < n; i++)
            scanf("%d", &a[i]);
        for (j = 0; j < n; j++)
        {
            max = j;
            for (z = j + 1; z < n; z++)
                if (a[max] < a[z]) max = z;
            if (a[j] != a[max])
            {
                t = a[max]; a[max] = a[j]; a[j] = t;
            }
        }
        for (i = 0; i < n; i++)
        {
            sum += a[i];
        }
        ave = ((sum - a[0] - a[n - 1]) * 1.0) / (n - 2);
        printf("%.2lf\n", ave);
    }
 

 

2015:

Problem Description
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。
 
Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。
 
Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。
 
Sample Input
3 2
4 2
Sample Output
3 6
3 7
#include<stdio.h>
int main()
{
    int number;
    while (scanf("%d",&number))
    {
        float point, max = -1, min = 65535;
        float total = 0,result;
        for (int i = 0; i < number; i++)
        {
            scanf("%f",&point);
            total += point;
            if (point > max)
                max = point;
            if (point < min)
                min = point;
        }
        result = (total - max - min) / (number - 2);

        printf("%.2f\n",result);
    }
}

 

2016:

Problem Description
输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。
 
Input
输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。
 
Output
对于每组输入数据,输出交换后的数列,每组输出占一行。
 
Sample Input
4 2 1 3 4
5 5 4 3 2 1
0
 
Sample Output
1 2 3 4
1 4 3 2 5
#include<stdio.h>
int main()
{
    int n, i, t, w, j[102], k;
    while (scanf("%d", &n) != EOF)
    {
        if (n == 0)break;
        for (i = 0; i < n; i++)
            scanf("%d", &j[i]);
        w = j[0];

        for (i = 0; i < n; i++)
        {
            if (j[i] <= w)
                k = i;
            w = j[k];
        }
        if (w != j[0])
        {
            t = j[k]; j[k] = j[0]; j[0] = t;
        }
        for (i = 0; i < n; i++)
            if (i == 0)
                printf("%d", j[i]);
            else
                printf(" %d", j[i]);
        printf("\n");
    }
    return 0;
}

 

2017:

Problem Description
对于给定的一个字符串,统计其中数字字符出现的次数。
 
Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
 
Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。
 
Sample Input
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf
 
Sample Output
6
9
#include<stdio.h>
#include<string.h>
int main(void)
{
    char a[1000];
    int n;
    scanf("%d", &n);
    getchar();
    while(n--)
    {
        gets(a);//gets函数本身就是以回车符结束;
        int count = 0;//计数器设置为0
        for (int i = 0; a[i] != '\0'; i++)
        {//从头到尾遍历 ,如果是数字,计数器++ 
            if (a[i] >= '0' && a[i] <= '9')
            {
                count++;
            }
        }
        printf("数字字符个数为:%d\n", count);
    }
}

 

2018:

Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
 
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。
 
Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。
 
Sample Input
2
4
5
0
 
Sample Output
2
4
6
 
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a[55] = { 1,2,3 };
    int i, n;
    for (i = 3; i < 55; i++)
        a[i] = a[i - 1] + a[i - 3];
    while (scanf("%d", &n) != EOF) {
        if (n == 0)
            break;
        printf("%d\n", a[n - 1]);
    }
    system("PAUSE");
    return 0;
}

 

2019:

Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。
 
Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。
 
Output
对于每个测试实例,输出插入新的元素后的数列。
 
Sample Input
3 3
1 2 4
0 0
 
Sample Output
1 2 3 4
#include<stdio.h>
#include<stdlib.h>
int cmp(const void* p1, const void* p2)
{
    return *(int*)p1 - *(int*)p2;
}
int main()
{
    int n, m, i, a[111];
    while (scanf("%d%d", &n, &m) != EOF)
    {
        if (m == 0 && n == 0)break;
        a[n] = 0;
        for (i = 0; i < n; i++)
            scanf("%d", &a[i]);
        a[n] = m;
        qsort(a, n + 1, sizeof(a[0]), cmp);
        for (i = 0; i < n + 1; i++)
        {
            if (i == 0)
                printf("%d", a[0]);
            else
                printf(" %d", a[i]);
        }
        printf("\n");
    }
    return 0;
}

 

2020

Problem Description
输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。
 
Input
输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。
 
Output
对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。
 
Sample Input
3 3 -4 2
4 0 1 2 -3
0
 
Sample Output
-4 3 2
-3 2 1 0
#include <stdio.h>
#include<math.h>

int main()
{
    int a[100], b, c, n, k, i, t;
    while (scanf("%d", &n) != EOF)
    {
        for (b = 0; b < n; b++)
        {
            scanf("%d", &a[b]);
        }
        for (c = 0; c < n - 1; c++)
        {
            k = c;
            for (i = c + 1; i < n; i++)
            {
                if (abs(a[k]) > abs(a[i]))
                {
                    k = i;
                }
                if (k != c)
                {
                    t = a[c];
                    a[c] = a[k];
                    a[k] = t;
                    k = c;
                }
            }
        }
        for (c = n - 1; c >= 0; c--)
        {
            printf("%d ", a[c]);
        }
        printf(" ");
    }
    return 0;
}

 

2021:

Problem Description
作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵
但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每个老师的工资额都知道,最少需要准备多少张人民币,才能在给每位老师发工资的时候都不用老师找零呢?
这里假设老师的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。
 
Input
输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资。
n=0表示输入的结束,不做处理。
 
Output
对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。
 
Sample Input
3 1 2 3
0
 
Sample Output
4
#include<stdio.h>
int main()
{
    int sum,i, n, k, num1[6] = { 100,50,10,5,2,1 };
    while (1) {
        sum = 0;
        scanf("%d", &n);
        if (n == 0)
            break;
        for (int i = 0; i < n; i++) {
            scanf("%d", &k);
            for (int j = 0; j < 6; j++) {
                sum += k / num1[j];
                if (k % num1[j] == 0)
                    break;
                else
                    k = k % num1[j];
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}

 

2022:

Problem Description
potato老师虽然很喜欢教书,但是迫于生活压力,不得不想办法在业余时间挣点外快以养家糊口。
“做什么比较挣钱呢?筛沙子没力气,看大门又不够帅...”potato老师很是无奈。
“张艺谋比你还难看,现在多有钱呀,听说还要导演奥运开幕式呢!你为什么不去娱乐圈发展呢?”lwg在一旁出主意。
嗯,也是,为了生存,就委屈点到娱乐圈混混吧,马上就拍一部激光电影《杭电记忆——回来我的爱》。
说干就干,马上海选女主角(和老谋子学的,此举可以吸引媒体的眼球,呵呵),并且特别规定,演员必须具有ac的基本功,否则直接out!
由于策划师风之鱼(大师级水王)宣传到位,来应聘的MM很多,当然包括nit的蛋糕妹妹等呼声很高的美女,就连zjut的jqw都男扮女装来应聘(还好被安全顾问hdu_Bin-Laden认出,给轰走了),看来娱乐圈比acm还吸引人哪...
面试那天,刚好来了m*n个MM,站成一个m*n的队列,副导演Fe(OH)2为每个MM打了分数,分数都是32位有符号整数。
一开始我很纳闷:分数怎么还有负的?Fe(OH)2解释说,根据选拔规则,头发染成黄色、化妆太浓、穿的太少等等都要扣分数的,扣的多了就可能是负分了,当然,如果发现话语中夹有日语,就直接给-2147483648分了。
分数送上来了,是我做决定的时候了,我的一个选拔原则是,要选一个面试分数绝对值(必须还是32位整数)最大的MM。
特别说明:如果不幸选中一个负分的MM,也没关系,因为我觉得,如果不能吸引你,那要想法恶心你。
 
Input
输入数据有多组,每组的第一行是两个整数m和n,表示应聘MM的总共的行列数,然后是m行整数,每行有n个,m和n的定义见题目的描述。
 
Output
对于每组输入数据,输出三个整数x,y和s,分别表示选中的MM的行号、列号和分数。
note:行号和列号从一开始,如果有多个MM的分数绝对值一样,那么输出排在最前面的一个(即行号最小的那个,如果行号相同则取列号最小的那个)。
 
Sample Input
2 3 1 4 -3 -7 3 0
 
Sample Output
2 1 -7
#include <stdio.h>
#include <math.h>

int main()
{
    int n, m, flagi, flagj, max;
    int a[100][100];
    while (~scanf("%d%d", &m, &n))
    {
        for (int j = 0; j < m; j++)
        {
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &a[i][j]);
            }
        }
        for (int j = 0; j < m; j++)
        {
            for (int i = 0; i < n; i++)
            {
                if (i == 0 && j == 0)
                    max = a[i][j];
                if (fabs(a[i][j]) > fabs(max))
                {
                    max = a[i][j];
                    flagi = i;
                    flagj = j;
                }
            }
        }
        printf("%d %d %d\n", flagj + 1, flagi + 1, max);
    }
    return 0;
}

 

2023:

Problem Description
假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量。
 
Input
输入数据有多个测试实例,每个测试实例的第一行包括两个整数n和m,分别表示学生数和课程数。然后是n行数据,每行包括m个整数(即:考试分数)。
 
Output
对于每个测试实例,输出3行数据,第一行包含n个数据,表示n个学生的平均成绩,结果保留两位小数;第二行包含m个数据,表示m门课的平均成绩,结果保留两位小数;第三行是一个整数,表示该班级中各科成绩均大于等于平均成绩的学生数量。
每个测试实例后面跟一个空行。
 
Sample Input
2 2
5 10
10 20
 
Sample Output
 
7.50 15.00
7.50 15.00
1
#include<stdio.h>
#include<math.h>
int main(void) {
    int m, n;
    while (scanf_s("%d%d", &n, &m) != EOF) {
        int k=0;
        int i, j;
        double sum=0, sum1=0;
        double a[51][6] = { 0 };
        double pj[6] = {0};
        double tx[51] = { 0 };
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                scanf_s("%lf", &a[i][j]);
            }
        }
        for (j = 1; j <= m; j++) {
            for (i = 1; i <= n; i++) {
                pj[j] += a[i][j];
            }
        }
        for (i = 1; i <= m; i++) {
            pj[i]/=n;
        }
        for (i = 1; i <= n; i++) {
            if (a[i][1] >= pj[1] && a[i][2] >= pj[2] && a[i][3] >= pj[3] && a[i][4] >= pj[4] && a[i][5] >= pj[5]) {
                k++;
            }
        }
    
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                tx[i] += a[i][j];
            }
            tx[i] = tx[i] / m;

        }
        for (i = 1; i < n; i++) {
            printf("%.2f ", tx[i]);
        }
        printf("%.2f\n", tx[i]);
        for (i = 1; i < m; i++) {
            printf("%.2f ", pj[i]);
        }
        printf("%.2f\n", pj[i]);
        printf("%d\n\n", k);
        
    }
}

 

2024:

Problem Description
输入一个字符串,判断其是否是C的合法标识符。
 
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
 
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
 
Sample Input
3
12ajf f
i8x_a
ff  ai_2
 
Sample Output
no
yes
no
#include <string.h>
#include <stdio.h>
int h(char a)/*判断是否是字母或数字*/
{
    if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z' || a >= '0' && a <= '9')
        return 1;
    return 0;
}
int hs(char a)/*判断是否是字母*/
{
    if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z')
        return 1;
    return 0;
}
int main()
{
    int n, d, i;
    char sym[64];
    scanf("%d%*c", &n);/*输入n组,注意%*c是为了跳过回车键的输入*/
    while (n--)
    {
        gets(sym);
        if (sym[0] != '_' && !hs(sym[0]))
        {
            puts("no");
            continue;
        }
        for (d = 1, i = 1; sym[i]; i++)
        {
            if (!h(sym[i]) && sym[i] != '_')
            {
                d = 0;
                break;
            }
        }
        puts(d ? "yes" : "no");
    }
    return 0;
}

 

2025:

Problem Description
对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。
 
Input
输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。
 
Output
对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。
 
Sample Input
abcdefgfedcba xxxxx
 
Sample Output
abcdefg(max)fedcba x(max)x(max)x(max)x(max)x(max)
#include <stdio.h>
int main()
{
    char str[100], ch;
    while (~scanf("%s", str))
    {
        ch = str[0];
        for (int i = 1; str[i]; i++)
        {
            if (str[i] > ch)
                ch = str[i];
        }
        for (int i = 0; str[i]; i++)
        {
            printf("%c", str[i]);
            if (str[i] == ch)
                printf("(max)");
        }
        printf("\n");
    }
    return 0;
}

 

2026:

Problem Description
输入一个英文句子,将每个单词的第一个字母改成大写字母。
 
Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。
 
Output
请输出按照要求改写后的英文句子。
 
Sample Input
i like acm i want to get an accepted
 
Sample Output
I Like Acm I Want To Get An Accepted
#include<stdio.h>
#include<string.h>
int main()
{
    char a[500];
    int i, k;
    while (gets(a) != NULL)
    {
        k = strlen(a);
        a[0] -= 32;
        for (i = 0; i < k; i++)
        {
            if (a[i] == ' ')
                a[i + 1] -= 32;
        }
        puts(a);
        //printf("\n");
    }
    //printf("\n");
    return 0;
}

 

2027:

Problem Description
统计每个元音字母在字符串中出现的次数。
 

 

Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
 

 

Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。

请特别注意:最后一块输出后面没有空行:)
 
Sample Input
2 aeiou my name is ignatius
 
Sample Output
a:1 e:1 i:1 o:1 u:1 a:2 e:1 i:3 o:0 u:1
#include<stdio.h>
#include<string.h>
int main()
{
    char a[200];
    int i, k, n, b[5];
    scanf("%d", &n);
    getchar();//注意把getchar放while循环外部,但是并不造是为什么. 
    while (n--)
    {
        gets(a);
        k = strlen(a);
        for (i = 0; i < 5; i++)
            b[i] = 0;
        for (i = 0; i < k; i++)
        {
            if (a[i] == 'a')
                b[0]++;
            if (a[i] == 'e')
                b[1]++;
            if (a[i] == 'i')
                b[2]++;
            if (a[i] == 'o')
                b[3]++;
            if (a[i] == 'u')
                b[4]++;
        }
        printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n", b[0], b[1], b[2], b[3], b[4]);
        if (n != 0)//这样是为了符合题目要求最后一次没有换行。
            printf("\n");
    }
    return 0;
}

 

2028:

Problem Description
求n个数的最小公倍数。
 
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
 
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
 
Sample Input
2 4 6
3 2 5 7
 
Sample Output
12 70
#include<stdio.h>
int main()
{
    int x_y(int x, int y);
    int i, n, a[100], s;
    while (scanf("%d", &n) != EOF)
    {
        for (i = 0; i < n; i++)
            scanf("%d", &a[i]);
        if (n == 1)printf("%d\n", a[0]);
        else
        {
            s = x_y(a[0], a[1]);
            for (i = 1; i < n; i++)
                s = x_y(s, a[i]);
        }
        printf("%d\n", s);
    }
    return 0;
}
int x_y(int x, int y)
{
    int a, b, t, m;
    a = x; b = y;
    if (x > y)
    {
        t = x; x = y; y = t;
    }
    for (; x != 0;)
    {
        t = y % x;
        y = x;
        x = t;
    }
    m = (a / y) * b;
    return m;
}

 

2029:

Problem Description
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
 
Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。
 
Output
如果一个字符串是回文串,则输出"yes",否则输出"no".
 
Sample Input
4 level abcde noon haha
 
Sample Output
yes no yes no#include<stdio.h>
#include<string.h>
int main()
{
    int n, i, t1, len, j;
    char a[100], b[100];               //定义数组a和b
    while (scanf("%d", &n) == 1)
    {
        getchar();                //吸收"\n"
        while (n--)
        {
            int count = 0;
            t1 = 0;               //统计a数组和b数组是否全部相等     
            gets(a);
            len = strlen(a);
            for (j = len - 1; j >= 0; j--)
            {
                b[count++] = a[j];
            }

            for (i = 0; i < len; i++)
            {
                if (a[i] == b[i])
                    t1++;
            }

            if (t1 == len)
                printf("yes\n");           //如果a和b相等输出yes
            else
                printf("no\n");              //否则输出no
}
} return 0; }

 

2030:

Problem Description
统计给定文本文件中汉字的个数。
 
Input
输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。
 
Output
对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。

[Hint:]从汉字机内码的特点考虑~

Sample Input
2 WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa! 马上就要期末考试了Are you ready?
 
Sample Output
14 9
#include<stdio.h>
#include<string.h>
int main()
{
    char a[200];
    int n, k, i, t;
    scanf("%d", &n);
    getchar();
    while (n--)
    {
        t = 0;

        gets(a);
        k = strlen(a);
        for (i = 0; i < k; i++)
            if (a[i] < 0)
                t++;
        printf("%d\n", t / 2);
    }
    return 0;
}

 

2032:

Problem Description
还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
 
Input
输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。
 
Output
对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。
 
Sample Input
2 3
 
Sample Output
1
1 1
 
1
1 1
1 2 1
方法一(二维数组):
#include<stdio.h> 
int main()
{
    int a[30][30], i, j, n;
    
    while (scanf("%d", &n))
    {
        for (i = 0; i < n; i++)
        {
            a[i][i] = 1;
            a[i][0] = 1;
        }
        for (i = 2; i < n; i++)
        {
            for (j = 1; j < i; j++)
            {
                a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
            }
        }
        for (i = 0; i < n; i++)
        {
            for (j = 0; j <= i; j++)
            {
                if (j == 0)
                    printf("%d", a[i][j]);

                else
                    printf(" %d", a[i][j]);
            }   printf("\n");
        }
  
    }
}

方法二(递推法):

#include<stdio.h> 
int main()
{
    int a[30][30], i, j, n;
    
    while (scanf("%d", &n))
    {
        for (int i = 1; i <= n; i++)
        {
            int b = 1;
            for (int j = 1; j <= i; j++)
            {
                printf("%d", b);
                b = b * (i - j) / j;  // 下一个元素值=上一个*(行数-列数)/列数 
                if (j < i)
                    printf(" ");
            }
            printf("\n");
        }
    }
}

方法三:

//多项式 杨辉三角中每一行的值为C(n-1,m),其中n为行数,m为列数索引
#include<stdio.h> 
int main()
{
    int a[30][30], i, j, n;
    
    while (scanf("%d", &n))
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                printf("%d", fun(i, j));

                if (j < i)
                    printf(" ");

            }
            printf("\n");
        }
    }
}
int fun(int n, int m)
{
    int s1 = 1, j = 1;
    for (int i = n; i > n - m; i--)
    {
        s1 = s1 * i / j;
        j++;
    }
    return s1;
}

杨辉三角方法引用自:https://blog.csdn.net/SinclairWang/article/details/88367151

posted @ 2020-06-07 14:08  热心市民陆女士  阅读(72)  评论(0编辑  收藏  举报