山东理工oj 1831答案

来源:互联网 发布:淘宝正品耐克店铺 编辑:程序博客网 时间:2024/04/28 02:11

A

   Problem Description

Your task is to Calculate a + b.

Input

Your task is to Calculate a + b.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.


#include <stdio.h>
int main()
{
    int i, s, n, a, b;
    scanf ("%d", &n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d %d", &a, &b);
        s = a + b;
        printf("%d\n", s);
    }
    return 0;
}

B

Problem Description

输入两个整数,求它们的最大公约数与最小公倍数。

Input

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

Output

第一行输出最大公约数;
第二行输出最小公倍数。

#include <stdio.h>
int main()
{
    int a, b, m, i, t;
    scanf("%d %d", &a, &b);
    if (a < b) {t = a; a = b; b = t;}
    for (i = b; ; i--)
    {
        if (a % i == 0 && b % i == 0)
        {
          break;
        }

    }
    m = a * b / i;
            printf("%d\n", i);
            printf("%d\n", m);
    return 0;
}

C

Problem Description

从键盘上输入任意一个整数,然后判断该数是否为素数。
如果是素数则输出"This is a prime."
否则输出“This is not a prime.”

Input

输入任意一个整数n。

Output

判断n是否为素数,并输出判断结果:
如果n是素数则输出"This is a prime."
否则输出“This is not a prime.”

#include <stdio.h>
int main()
{
     int a, i;
     int flag;
     flag = 1;
     scanf("%d", &a);
     for(i = 2; i <= a - 1 ; i++)
        if ( a % i == 0)
        flag = 0;
     if(flag && a != 1)
        printf("This is a prime.\n");
        else
        printf("This is not a prime.\n");
    return 0;
}

D

   Problem Description

求n个整数中的绝对值最大的数。

Input

输入数据有2行,第一行为n,第二行是n个整数。

Output

输出n个整数中绝对值最大的数。


#include <stdio.h>
int main()
{
     int a, i, n, m, t, p;
     scanf("%d", &a);
     p = 0;
     for( i = 1; i <= a ; i++)
       {
        scanf("%d", &n);
        if (n < 0)
           {
               m = -n;
         if (m > p)
            p = m;
            t = n;
           }
           else if (n > p)
            t = n;
    }

        printf("%d\n", t);
    return 0;
}

E

输入n值,并利用下列格里高里公式计算并输出圆周率: 

Input

输入公式中的n值。

#include <stdio.h>
int main()
{
    int  n, i;
    double a, pi, t;
    scanf("%d", &n);
    t = 0;
    for (i = 1; i <= n ; i++)
    {

        a = (1.0 / (4 * i - 3)) - (1.0 / (4 * i - 1));
        t = a + t;
        }
    pi = 4 * t;
    printf("%.5lf\n", pi);
    return 0;

}

F

Problem Description

数列求和是一类常见的问题,本题有一定的代表性:
求s=a+aa+aaa+aaaa+……+aa…aa(n位)
其中,a的值由键盘输入,位数n也由键盘输入。

Input

第一行输入a的值;
第二行输入位数n。

Output

输出对n个数完成求和运算后的结果。
比如a=3,n=6时,s=3+33+333+3333+33333+333333

#include <stdio.h>
int main()
{
    int a, t, f, s, n, i;
    int b = 10;
    s = 0;
    scanf("%d", &a);
    t = a;
    f = a;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
        {
            s = s + a;
            t = f * b;
            a = t + a;
            b = b * 10;
        }
        printf("%d", s);
    return 0;
}

G

Problem Description

从键盘上输入任意一个整数n,计算1到n的和。

Input

从键盘输入任意整数n。

Output

输出1到n的和。


#include <stdio.h>
int main()
{
    int  a, n, s, i;
    a = 0;
    s = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++ )
    {
         a = i;
         s =  s + a;


}
    printf("%d\n", s);
    return 0;
}

H

Problem Description

从键盘输入任意一个大于等于0的整数n,然后计算n的阶乘,并把它输出。

Input

输入任意一个大于等于0的整数n。

Output

输出n!

#include <stdio.h>
int main()
{
    int  a, n, s, i;
    a = 0;
    s = 1;
    scanf("%d", &n);
    for (i = 1; i <= n; i++ )
    {
         a = i;
         s =  s * a;


}
    printf("%d\n", s);
    return 0;
}

I

Problem Description

求2个数中较大者。

Input

第一行为测试的数据组数N,接下来的N行分别是两个待比较的整数。

Output

输出N行,每一行的值为每组数中较大的整数。
#include <stdio.h>
int main()
{
    int a, b, n, i;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d %d", &a, &b);
        if(b > a)
            a = b;
        printf("%d\n", a);
    }
    return 0;
}

J

Problem Description

请用C语言编写一个程序。此程序接收一个正整数N,然后打印输出“N次N*(1->N)格式”的数据。例如:此程序接收正整数5,那会输出以下格式的数据:
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
 

Input

只有一个正整数N(N<=100)。

#include <stdio.h>
int main()
{
    int n, i;
    scanf("%d", &n);
    if (n <= 100)
    {
        for (i = 1; i <= n; i++)
        {
            printf("%d*%d=%d\n", n, i, n * i);
        }
    }
    return 0;
}

  


K

Problem Description

接受从键盘输入的N个整数,输出其中的最大值、最小值和平均值(平均值为整除的商)。
 

Input

第一行一个正整数N(N<=100);
第二行有N个用空格隔开的整数Ti (1<= i <=N, 0<= Ti <= 1000)
ouput

Output

三个有空格隔开的整数分别为最大值、最小值和平均值,其中平均值为整除的商。

#include <stdio.h>
int main()
{
    int n, a, t, m, ave, s, i;
    scanf("%d", &n);
    if (n <= 100)
    {
        scanf("%d", &a);
        m = a; t = a; s = a;
        for (i = 1; i < n ;i++)
        {
            scanf("%d", &a);
            if (a > m)
                m = a;
            if (a < t)
                t = a;
            s = s + a;
        }
        ave = s / n;
    printf("%d %d %d", m, t, ave);
    return 0;
}
}

L

Problem Description

      小狗对小猫说:你猜猜我的口袋里有几块糖?小猫说:猜对了你给我吃吗?小狗点点头:嗯,猜对了两块都给你!小猫咽了咽口水说:我猜五块!然后,小狗笑着把糖放到小猫手里,说:我还欠你三块。
      既然小猫这么喜欢吃糖,小狗决定每天都给小猫几块糖,但是呢,不能每天都给相同块数的糖,那样就太单调了。于是,第一天小狗给小猫1*1=1块,第二天2*2=4块……第 n 天给的糖数为 n*n 。现在已知小狗家共有 N 块糖,你需要帮他计算下这些糖最多可以给小猫几天?

Input

输入只有一个整数 N (1 < N <=10000)。

#include <stdio.h>
int main()
{
    int n, d, t;
    scanf("%d", &n);
    t = 0;
    for (d = 1; ; d++)
        {
            t = t + d * d;
            if (n - t < 0) break;
        }
        printf("%d", d-1);
    return 0;

}

M

Problem Description

有一个分数序列:2/1, 3/2, 5/3, 8/5, 13/8, …编写程序求出这个序列的前n项之和。

Input

输入只有一个正整数n,1≤n≤10。

Output

输出该序列前n项和,结果保留小数后6位。
#include <stdio.h>
int main()
{   int a, b, t, i, n;
    double s;
    a = 2;
    b = 1;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        s = s + (double)a /(double) b;
        t = a;
        a = a + b;
        b = t;
    }
    printf("%.6lf", s);
        return 0;
}

N

Problem Description

期末考试结束了,老师想要根据学生们的成绩划分出等级。共有5个等级A,B,C,D和E。
划分方法如下,90分(含90)以上的为A,80~90(含80)间的为B,70~80(含70)间的为C,
60~70(含60)的为D,不及格的为E。
根据输入的成绩,编程输出各个级别段人数。
 

Input

输入第一行包含一个正整数N(N<= 100)代表学生的数目,接下来有N行数据每行一个整数(0~100)代表
一个学生的成绩。

Output

输出有五行格式如下:
A nA
B nB
C nC
D nD
E nE
其中A,B,C,D,E代表等级,nA,nB等代表个等级的人数,等级和人数之间有一个空格。

#include <stdio.h>
int main()
{
    int n, f, i, na, nb, nc, nd, ne;
    na = 0;
    nb = 0;
    nc = 0;
    nd = 0;
    nd = 0;
    ne = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d", &f);
        switch (f / 10)
        {
            case 5 : ne++; break;
            case 6 : nd++; break;
            case 7 : nc++; break;
            case 8 : nb++; break;
            case 9 : na++; break;
            case 10 : na++; break;
            default : ne++; break;
        }

    }
        printf("A %d\nB %d\nC %d\nD %d\nE %d\n", na, nb, nc, nd, ne);
        return 0;
}

O

Problem Description

飞飞特别喜欢平方数,可是他数学并不好,你能帮他计算从n到m之间所有平方数之和吗?
提示:若一个整数的开方还是整数,它就是平方数。例如:4、9、16、25是平方数。

Input

 第一行 T 代表数据的组数。

接下来有 T 行,每行两个整数n,m

Output

 输出一个整数,代表所求区间内平方数之和。

#include<stdio.h>
int main()
{
    int a, b, c, t, n, m, i, y, s;
    scanf("%d", &t);
    s = 0;
    for (i = 1; i <= t; i++)
    {
        scanf("%d %d", &n, &m);
        if (n < m)
            {
                y = n;
                n = m;
                m = y;
            }

              for (b = m; b <= n ; b++)
              {
                for ( a = 1; a <= n; a++ )
                 {if ( a * a == b)
                      {
                      s = s + b;
                      c = s;
                      }

                 }

               }
               s = 0;
        printf("%d\n", c);
           }

return 0;
}

P

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

Input

正整数n(1≤n≤9)。

Output

指定数字图形。

Example Input

5

Example Output

    1   121  12321 1234321123454321 1234321  12321   121    1

#include <stdio.h>
int main()
{
    int i, n, k, h;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        for (k = n; k > i; k--)
            {
                printf(" ");
            }
        for (h = 1; h <= i; h++)
        {

            printf("%d", h);
        }
        for (h = i-1 ; h >= 1 ;h--)
        {
            printf("%d", h);
        }

         printf("\n");
    }
    for (i = n - 1; i > 0; i--)
    {

        for (k = 1; k <= n - i; k++)
             {
                printf(" ");
            }
        for (h = 1 ; h <= i ;h++)
        {
            printf("%d", h);
        }
         for (h = i - 1; h >= 1; h--)
        {
            printf("%d", h);
        }

         printf("\n");
    }
    return 0;
}

Q

Problem Description

从键盘输入一个整数n(1≤n≤9),打印出指定的菱形。

Input

正整数n(1≤n≤9)。

Output

指定的菱形。
第一行前面有n-1个空格,第二行有n-2个空格,依此类推。

Example Input

5

Example Output

    *   ***  ***** **************** *******  *****   ***    *

#include <stdio.h>
int main()
{
    int i, n, k, h;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        for (k = n; k > i; k--)
            {
                printf(" ");
            }
        for (h = 1; h <= i; h++)
        {

            printf("*");
        }
        for (h = i-1 ; h >= 1 ;h--)
        {
            printf("*");
        }

         printf("\n");
    }
    for (i = n - 1; i > 0; i--)
    {

        for (k = 1; k <= n - i; k++)
             {
                printf(" ");
            }
        for (h = 1 ; h <= i ;h++)
        {
            printf("*");
        }
         for (h = i - 1; h >= 1; h--)
        {
            printf("*");
        }

         printf("\n");
    }
    return 0;
}

I

Problem Description

春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33
现在要求输出所有在m和n范围内的水仙花数。

Input

输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

Output

对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
#include <stdio.h>
int main()
{
    int a, b, c, n, m, i, flag, skip;
    while (scanf("%d %d", &m, &n) != EOF)
    {
        flag = 0, skip = 1;
        for (i = m; i <= n; i++)
        {

            a = i / 100;
            b = (i / 10) % 10;
            c = i % 10;
            if (i == a * a * a + b * b * b + c * c * c)
                {
                    if (skip)
                    {
                    skip = 0;
                    printf("%d", i);
                     flag = 1;
                    }
                    else
                    {
                    printf(" ");
                    printf("%d", i);
                    }
                }
        }
        if (flag ==1)
        printf("\n");
        if (flag == 0)
        printf("no\n");
    }

return 0;
}

S

输入n的值,计算cos(x)。

Input

输入数据有多行,每行两个数,包括x和n。第一数据为x,第二个数据为n。

Output

输出cos(x)的值,保留4位小数。

Example Input

0.0 100

Example Output

1.0000

#include <stdio.h>
#include <math.h>
int main()
{
    int n, i, j;
    double s, k, a;
    double x;
    while(~ scanf("%lf %d", &x, &n)) {
        s = 1;
        for (i = 1; i <= n; i++)
        {
            a = 1;
            for (j = 1; j <= 2 * i; j++)
            {
                a *= j;
            }

            s = s + pow(-1,i) * pow(x,2 * i) / a;
            //printf("%d %.4f %.4f\n", i, a, s);
        }
        printf("%.4lf\n", s);
    }

    return 0;
}


T

Problem Description

小瑜3岁了,很喜欢玩皮球,看来今后喜欢打篮球的^_^。最近她发现球从手中落下时,每次落地后反跳回原高度的一半,再落下,每次球落地时数球跳了几次,数到n次时爸爸在边上喊停,问小瑜现在球到底总共走了多少距离,小瑜故作沉思状,爸爸又问接下来小球能跳多高啊,小瑜摇摇头,心想还没跳我怎么知道啊,难道爸爸是神啊!这时的你在边上出主意想给小瑜写个程序计算一下,因此任务就交给你啦!假设球的初始高度为h,计算第n次落地时球经过的距离,以及落地后反弹能有多高。

Input

输入数据有多组,第一行为数据的组数t,下面t行为t组数据,每行有两个数h和n,分别用空格分隔。

Output

输出第n次反弹时球经过的距离和球最后的高度,保留小数点后2位。
#include<stdio.h>
#include<math.h>
int main()
{
    int t, i, j;
    double b, h, n, s, l;
    scanf("%d", &t);
    for (i = 1; i <= t; i++)
    {
        scanf("%lf %lf", &h, &n);
        s = h;
        b = h;
        for (j = 1 ; j <= n; j++)
        {
            if (j == 1)
                {s = h;}
            else
            {
            h = h * 0.5;
            s = s + 2 * h;
            }

        }

        l = b * pow(0.5 , n);
        printf("%.2f %.2f\n", s, l);
}
return 0;
}



U

Problem Description

      素数又称质数。指一个大于1的自然数,除了1和此整数自身外,不能被其他自然数整除的数。我们定义:如果一个素数是完美的素数,当且仅当它的每一位数字之和也是一个素数。现在给你一个正整数,你需要写个程序判断一下这个数按照上面的定义是不是一个完美的素数。

Input

输入包含多组测试数据。
每组测试数据只包含一个正整数 n (1 < n < 10^6)。

Output

对于每组测试数据,如果 n 是完美的素数,输出“YES”,否则输出“NO”(输出均不含引号)。//坛子菌提供代码

#include <stdio.h>
int main ()
{
    int n, t, b, s, t1, t2;
    while((scanf("%d",&n))!=EOF)
    {
        if(n < 2)
        {
            printf("NO\n");
            continue;
        }
        b = 0;
        for (t = 2; t * t <= n; t++)
        {
            if (n % t==0)
            {
                b++;
            }
        }
        if(b == 0)
        {
            s = 0;
            for(; n > 0; n /= 10)
            {
                t1 = n % 10;
                s += t1;
            }
            for (t2 = 2; t2 * t2 <= s; t2++)
            {
                if (s % t2==0)
                {
                    b++;
                }
            }
        }
        if (b)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}


  

V

Problem Description

Your task is to Calculate the sum of some integers.

Input

Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

Example Input

4 1 2 3 45 1 2 3 4 50

Example Output

1015

#include <stdio.h>
int main()
{
    int i, n, s, a;
    while ((scanf("%d", &a)) != EOF)
    {s = 0;
    if (a != 0)
   {
        for (i = 1; i <= a; i++)
    {
        scanf("%d", &n);
        s = s + n;

    }
    printf("%d\n", s);
   }

    }
return 0;
}

W

Problem Description

众所周知,C语言的学习是我们程序设计基础的重点和主要内容。
有一天,小金(a1s4z5)觉得好饿、好饿,于是去地里找玉米吃。他拿了一个很大的背包,可以装下很多很多玉米。
他掰玉米有一个习惯,第1次的时候掰1个,第2次的时候掰2个,第3次的时候掰3个...第n次的时候掰n个,他打算掰完第n次的时候就回家吃玉米。
在苞米地里,他越掰越高兴越掰越高兴,终于当他摩擦到要停不下来的时候,发现自己根本背不动他的背包了。于是他要将前m次掰的玉米全都扔掉才能回家开饭。但是小金的数学很不(li)好(hai),请你帮他算一算袋子里还有多少玉米。

Input

多组输入。
第一行输入两个空格隔开的整数n和m,含义如题意描述。(0 < m < n < 10^4)

Output

输出小金的背包里最后剩下多少玉米。输入输出各占一行,保证数据合法。

#include <stdio.h>
int main()
{
    int i, n, m, a, s, k;

    while (scanf("%d %d", &n, &m) != EOF)

    {
        s = 0;
    a = 0;
        for (i = 1; i <= n; i++)
    {
        s = s + i;
    }
    for (k = 1; k <= m; k++)
    {
        a = a + k;
    }
    printf("%d\n", s - a);

}
return 0;
}

X

Problem Description

求小于n的所有素数,按照每行10个显示出来。

Input

输入整数n(n<10000)。

Output

每行10个依次输出n以内的所有素数。如果一行有10个素数,每个素数后面都有一个空格,包括每行最后一个素数。

Example Input

100

Example Output

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

#include <stdio.h>
#include <math.h>
int main()
{
    int a, m, k, i, count = 0;
    scanf("%d", &m);
    for (i = 2; i <= m; i++)
    {
        for (k = 2; k < i; k++)
            if (i % k == 0) break;
        if (k >= i)
        {
            printf("%d ", i);
            count++;
            if(count % 10 == 0)
                printf("\n");
        }
    }
    printf("\n");
    return 0;
}

Y

Problem Description

输出100->200之间的素数的个数,以及所有的素数。

Input

Output

100->200之间的素数的个数,以及所有的素数。

#include <stdio.h>
#include <math.h>
int main()
{
    int a, m, k, i;
    a = 0;
    for (i = 101; i <= 200; i++)
    {
        for (k = 2; k < i; k++)
        if (i % k == 0) break;
        if (k >= i)
        a++;
}
printf("%d\n", a);
for (i = 101; i <= 200; i++)
    {
        for (k = 2; k < i; k++)
        if (i % k == 0) break;
        if (k >= i)
        a++;
          if (k >= i)
           printf("%d ", i);
    }
    printf("\n");



    return 0;
}

Z

Problem Description

通过使用双重for循环语句,打印下列图形:

#include <stdio.h>
int main()
{
    int i, n, k, h;
    n = 4;
    for (i = 1; i <= n; i++)
    {
        for (k = n; k > i; k--)
            {
                printf(" ");
            }
        for (h = 1; h <= i; h++)
        {

            printf("*");
        }
        for (h = i-1 ; h >= 1 ;h--)
        {
            printf("*");
        }

         printf("\n");
    }
    for (i = n - 1; i > 0; i--)
    {

        for (k = 1; k <= n - i; k++)
             {
                printf(" ");
            }
        for (h = 1 ; h <= i ;h++)
        {
            printf("*");
        }
         for (h = i - 1; h >= 1; h--)
        {
            printf("*");
        }

         printf("\n");
    }
    return 0;
}

AA

Problem Description

输入n值,打印下列形状的金字塔,其中n代表金字塔的层数。

Input

输入只有一个正整数n。

Output

打印金字塔图形,其中每个数字之间有一个空格。
#include <stdio.h>
int main()
{
    int i, n, k, h;
    scanf("%d", &n);
    for (i = n; i >= 1; i--)
    {
        for (k =2 * (i - 1); k > 0; k--)
            {
                printf(" ");
            }
        for (h = 1 ; h <= n - i + 1; h++)
        {
            if (n - i + 1 == 1)
            printf("%d", h);
            else
            printf("%d ", h);
        }
        for (h = n - i; h >= 1; h--)
        {

        if (h == 1)
            printf("%d", h);
        else
            printf("%d ", h);
        }

         printf("\n");
    }
    return 0;
}


AB

Problem Description

      九九乘法表是数学学习的基础,今天我们就来看看乘法表的相关问题。《九九乘法歌诀》,又常称为“小九九”,如下图所示。你的任务是写一个程序,对于给定的一个正整数 n ,输出“九九乘法表”的前 n 行。例如,输入 n 为 9,你的程序的输出将为下图: 

Input

输入包含多组测试数据,以 EOF 结束。每组测试数据只包含一个正整数 n (0 < n < 10)。

Output

对于每组测试数据,输出上图所示“九九乘法表”的前 n 行。
#include <stdio.h>
int main()
{
    int i, n, k, h;

    while (scanf("%d", &n) != EOF)
    {
     // scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        for (k = 1; k <= i; k++)
        {
            if (k == i)
            printf("%d*%d=%d", k, i, k * i);

            else
            printf("%d*%d=%d ", k, i, k * i);

        }
        printf("\n");
    }
    }
    return 0;
}

AC

Problem Description

X晚上睡不着的时候不喜欢玩手机,也不喜欢打游戏,他喜欢数星星。

Input

 多组输入。
每组先输入一个整数N(N <= 10000),接着输入两个点代表矩形的左下点B(x,y)和右上点T(x,y),然后输入N个(X,Y)代表N颗星星。问有多少颗星星在窗子内部,在窗边上的不计。

Output

 输出一个整数,代表有多少颗星星在窗子内部。

Example Input

30 13 41 12 23 3

Example Output

1

#include <stdio.h>
int main()
{
    int i, n, a, b, x1, x2, y1, y2, count;
    while (scanf("%d", &n) != EOF)
    {scanf("%d %d", &x1, &y1);
    scanf("%d %d", &x2, &y2);
    count = 0;
    for (i = 1; i <= n; i++)
        {
            scanf("%d %d", &a, &b);
            if (a > x1 && a < x2 && b > y1 && b < y2)
            {
                count++;
            }

        }
printf("%d\n", count);
}
return 0;
    }


AD

SubmitStatistic

Problem Description

图形的规则如下 ,要求输入n的值,按照图形的打印规则打印出相关的图形:

Input

输入整数n。

Output

按图形的规律打印出相关的图形。
 
#include <stdio.h>
int main()
{
    int i, n, k, h;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        for (k = n; k > i; k--)
            {
                printf(" ");
            }
        for (h = 1; h <= i; h++)
        {
            if (h == 1)
                printf("+");
            else
            printf("*");
        }
        for (h = i-1 ; h >= 1 ;h--)
        {
             if (h == 1)
                printf("+");
            else
            printf("*");
        }
         printf("\n");
    }
    for (i = n - 1; i > 0; i--)
    {
        for (k = 1; k <= n - i; k++)
             {
                printf(" ");
            }
        for (h = 1 ; h <= i ;h++)
        {
             if (h == 1)
                printf("+");
            else
                printf("*");
        }
         for (h = i - 1; h >= 1; h--)
        {
            if (h == 1)
                printf("+");
            else
            printf("*");
        }
         printf("\n");
    }
    return 0;
}

AE

Problem Description

今天,小鑫在山上玩的时候,意外被推下了悬崖。

当然,掉下悬崖之后必然有奇遇。(剧情就是这么坑爹)就狗血的碰到了野人A和野人B。然后两位野人就给了他一本武功秘籍。

 这是一本强大的武功秘籍(好像武功秘籍一直都很强大)。共有40层的内功心法。当他练到第n层的时候,就可以借助高强的武功离开这个地方。你已经知道的是:练成第一层需要一天,练成第二层需要两天,此后每一层武功要练成所需的天数是前两层所需天数之和。也就是说第三层需要三天才能练成,因为1+2=3嘛。

当然,当他练成的那一天,他一定会去感谢野人们对他的帮助。那天他们会嗨到很晚,只能第二天在离开。

你能预测出多少天后小鑫能离开么?

Input

输入有多组,以文件结尾结束。

每组只有一行,n0<=n<=40

Output

 输出小鑫第几天才能离开,当然当n=0时,输出0,因为他不需要秘籍也能离开,也不需要感谢野人。

Example Input

120

Example Output

240

#include <stdio.h>
int main()
{    int i, n, s, a, b, x;   
while ((scanf("%d", &n)) != EOF)   

{       

s = 0;     

a = 1;      

b = 2;       

for (i = 1; i <= n; i++)    

{            if (i == 1 || i == 2)          

      x = i;      

      else           

     {          x = a + b;   

                a = b;         

                b = x;              

  }    

            s = s + x;        } 

       if (s != 0)      

      s++;        printf("%d\n", s);   

}return 0;

}

2 0
原创粉丝点击