Yt初试锋芒--题与分析

求奇数的乘积

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 107 Accepted Submission(s) : 55

Problem Description

给你n个整数,求他们中所有奇数的乘积。

Input

输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。

Output

输出每组数中的所有奇数的乘积,对于测试实例,输出一行。

Sample Input

3 1 2 3

4 2 3 4 5

Sample Output

3

15 

题解:

1.多组测试数据,需要多组数据的输入while(cin>>n)这里还有很多方法。P.s.c++语言的形式易limit,建议最好使用c语言。

2.对于多组数据有多个输入for(i=0;i<n;i++)n表示该组数据输入了n个数。P.s.小于比小于等于要快。

3.求奇数乘积,检测输入的数是不是奇数if(a%2==0)

4.如果是奇数,则算乘积,s的初始值为1

5.最后按格式输出结果即可。


#include<iostream>

using namespace std;

int main()

{

    int n,a;

    while(cin>>n)

    {

        int i,s=1;

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

    {

        cin>>a;

        if(a%2!=0)

        s=s*a;

    }

    cout<<s<<endl;

    }

    return 0;

}

绝对值排序

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

1.多组测试数据,需要多组数据的输入while(cin>>n,n)(,n表示n不为0)这里还有很多方法。P.s.c++语言的形式易limit,建议最好使用c语言。

2.对于多组数据有多个输入for(i=0;i<n;i++)n表示该组数据输入了n个数,cin>>a[i];因为后面要挨个进行处理,因此用数组要简便的多。P.s.小于比小于等于要快。

3.绝对值排序,首先要负数先求绝对值,abs()函数求绝对值,需要#include<cmath>

#include<cstdlib>头文件。

3.排序, 选择排序法

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

        { 

            k=i; 

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

                if(abs(a[j])>abs(a[k])) 

                    k=j; 

                    t=a[k]; 

                    a[k]=a[i]; 

                    a[i]=t; 

        } 

4.将排序好的数按顺序存在了数组中,再按顺序输出。

P.s.多组数据输出时要有换行。

#include<iostream>

#include<cmath>

#include<cstdlib>

using namespace std;

int main()

{

    int n,a[1000];

    while(cin>>n,n)

    {

        int i,j,k,t;

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

            cin>>a[i];

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

        { 

            k=i; 

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

                if(abs(a[j])>abs(a[k])) 

                    k=j; 

                    t=a[k]; 

                    a[k]=a[i]; 

                    a[i]=t; 

        } 

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

        {

            cout<<a[i];

            if(i!=n-1)

                cout<<" ";

        }

        cout<<endl;

    }

        return 0; 

}

计算两点间的距离

Problem Description

输入两点坐标(X1,Y1,X2,Y2,计算并输出两点间的距离。

Input

输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。

Output

对于每组输入数据,输出一行,结果保留两位小数。

Sample Input

0 0 0 1

0 1 1 0

Sample Output

1.00

1.41

1.多组测试数据,需要多组数据的输入while,因为每组数据都是固定的两个点(x1,y1)(x2,y2),因此直接while(cin>>x1>>y1>>x2>>y2)即可。P.s.本题数据类型为float型。

2.计算两点间的距离,有公式 n=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));需要头文件<cmath>

3.按输出格式输出。

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

int main()

{

    float n,x1,x2,y1,y2;

    while(cin>>x1>>y1>>x2>>y2)

    {

        n=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

        cout<<setiosflags(ios::fixed)<<setprecision(2)<<n;

        cout<<endl;

    }

        return 0; 

}

三角形

Problem Description

给定三条边,请你判断一下能不能组成一个三角形。

Input

输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C。其中A,B,C <1000;

Output

对于每个测试实例,如果三条边长A,B,C能组成三角形的话,输出YES,否则NO

Sample Input

2

1 2 3

2 2 2

Sample Output

NO

YES

1.多组测试数据,需要多组数据的输入while(cin>>n)(这里还有很多方法。P.s.c++语言的形式易limit,建议最好使用c语言。

2.对于多组数据有多个输入for(i=0;i<n;i++)n表示该组数据输入了n个数。P.s.小于比小于等于要快。

3.判断是否为三角形,两边之和大于第三边,两边之差小于第三边。if((a+b>c)&&(a+c>b)&&(b+c>a)&&(abs(a-b)<c)&&(abs(a-c)<b)&&(abs(c-b)<a));

4.按输出要求输出即可。

P.s.注意英文的大小写。

#include<iostream>

#include<cstdlib>

using namespace std;

int main()

{

    int n;

    float a,b,c;

    while(cin>>n)

    {

        int i;

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

        {

            cin>>a>>b>>c;

            if((a+b>c)&&(a+c>b)&&(b+c>a)&&(abs(a-b)<c)&&(abs(a-c)<b)&&(abs(c-b)<a))

                cout<<"YES"<<endl;

            else cout<<"NO"<<endl;

        }

    }

        return 0; 

}

成绩转换

Problem Description

输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下:
90~100A;
80~89B;
70~79C;
60~69D;
0~59E;

Input

输入数据有多组,每组占一行,由一个整数组成。

Output

对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“Score is error!”

Sample Input

56

67

100

123

Sample Output

E

D

A

Score is error!

1.多组测试数据,需要多组数据的输入while(cin>>n)(这里还有很多方法。P.s.c++语言的形式易limit,建议最好使用c语言。

2.对于输入的数先检验是否符合要求 if(n>=0&&n<=100),符合要求的进行分级操作,这种类型用switch比较方便。

3.注意不满足条件是语句中的大小写,以及标点符号。

4.按要求输出。

#include<iostream>

using namespace std;

int main()

{

    int n;

    while(cin>>n)

    {

        if(n>=0&&n<=100)

        switch(n/10){

        case 10:

        case 9:cout<<"A"<<endl;break;

        case 8:cout<<"B"<<endl;break;

        case 7:cout<<"C"<<endl;break;

        case 6:cout<<"D"<<endl;break;

        default :cout<<"E"<<endl;break;

        }

        else

        cout<<"Score is error!"<<endl;

    }

        

        return 0; 

}

Let the Balloon Rise

Problem Description

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you. 

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

Output

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.

Sample Input

5

green

red

blue

red

red

3

pink

orange

pink

0

Sample Output

red

pink

题目大意:

让气球飞

多组数据输入输出,输入气球的颜色,其中颜色出现最多的最受欢迎,即要输出该种气球的颜色。

1.多组测试数据,需要多组数据的输入while(cin>>n)(这里还有很多方法。P.s.c++语言的形式易limit,建议最好使用c语言。

2.输入气球的颜色(属于字符串),用string或者二维数组。注意不要数组范围不要越界。

3.比较字符串,再设一个数组变量对应每一种颜色,通过比较字符串是否相同来统计出现的次数for(j=0;j<n;j++)

                if(strcmp(a[i],a[j])==0)

                    k[i]=t++;注意t值,每改变一种颜色要重新归零。

4.在比较出现次数,直接用选择排序遍历一次找出最大的即可。

5.输出对应次数最大的颜色。

注意单词大小写。

#include<iostream>

#include<cstring>

using namespace std;

int main()

{

    int n,k[1000];

    char a[1000][100];

    while(cin>>n,n)

    {

        int i,j,t;

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

            cin>>a[i];

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

        {

            t=0;

            for(j=0;j<n;j++)

                if(strcmp(a[i],a[j])==0)

                    k[i]=t++;

        }

        t=0;

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

            if(k[i]>k[t])

                t=i;

        cout<<a[t]<<endl;

    }

    return 0;

}

Elevator

Problem Description

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input

There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.

Output

Print the total time on a single line for each test case. 

Sample Input

1 2

3 2 3 1

0

Sample Output

17

41

题目大意:

电梯

多组数据,0为结束标志

假设电梯从0层开始,给出电梯到达层数,计算电梯完成工作总共需要多长时间。

每一层,上升需要6秒,静止需要5秒,下降需要4秒。

1.多组测试数据,需要多组数据的输入while(cin>>n,n)(,n表示n不为0)这里还有很多方法。P.s.c++语言的形式易limit,建议最好使用c语言。

2对于多组数据有多个输入for(i=0;i<n;i++)n表示该组数据输入了n个数,cin>>a[i];因为后面要挨个进行处理,因此用数组要简便的多。P.s.小于比小于等于要快。

3.先判断是上升,还是下降,通过后一层减前一层的差的正负。

      if(a[i]-a[i-1]>0)

                a[0]=a[0]+6*(a[i]-a[i-1]);

            else

                a[0]=a[0]+4*(a[i-1]-a[i]);(本题用了a[0]来保存计算的和,注意i值)

4.输出a[0],即需要的时间。

#include<iostream>

using namespace std;

int main()

{

    int n,a[100];

    while(cin>>n,n)

    {

        a[0]=0;

        int i;

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

            cin>>a[i];

        a[0]=5*(i-1)+6*a[1];

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

        {

            if(a[i]-a[i-1]>0)

                a[0]=a[0]+6*(a[i]-a[i-1]);

            else

                a[0]=a[0]+4*(a[i-1]-a[i]);

        }

        cout<<a[0]<<endl;

 

    }

    return 0;

}

一只小蜜蜂...

Problem Description

有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数。
其中,蜂房的结构如下所示。

Input

输入数据的第一行是一个整数N,表示测试实例的个数,然后是行数据,每行包含两个整数ab(0<a<b<50)

Output

对于每个测试实例,请输出蜜蜂从蜂房a爬到蜂房b的可能路线数,每个实例的输出占一行。

Sample Input

2

1 2

3 6

Sample Output

1

3

题意分析:

在第n个格子中可以爬到第n+1n+2的格子里,即有fn=fn+1+fn+2),因此为斐波那契数列。

1.多组数据输入输出,除去常用的还可以这种组合whileM--scanf........);

2.计算数据,用上述公式带入计算即可。

3.由于斐波那契数列中求出的数都是固定的,而且若每输入一组数就要从头累计算一遍很慢,所以这里用了数组全部计算出来存上,当输入时直接对应输出即可。

4.按要求格式输出。

#include <stdio.h>    

int main()  

{  

    double c[50];  

    int M, a, b, i;  

    c[1] = 1;  

    c[2] = 2;  

    for(i=3; i<50; i++)  

        c[i] = c[i-1]+c[i-2];  

    scanf("%d", &M);  

    while(M--)  

    {  

        scanf("%d%d", &a, &b);  

        printf("%.lf\n", c[b-a]);  

    }  

    return 0;  

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值