1108 Finding Average(20 分)【字符串模拟题】

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/QQN19961117/article/details/82385766

20分的水题,我却写了40多分钟,严重怀疑自己思路错了。A了以后看了别人的解题报告,才发现原来用两个函数就可以很轻松的解决了。。还是要多掌握一些知识呀。。

两个函数:

sscanf() – 从一个字符串中读进与指定格式相符的数据
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中

 

别人AC的简单代码:

#include <string.h>
using namespace std;
int main()
 {
    int n, cnt = 0;
    char a[50], b[50];
    double temp, sum = 0.0;
    cin >> n;
    for(int i = 0; i < n; i++) {
        scanf("%s", a);
        sscanf(a, "%lf", &temp);
        sprintf(b, "%.2lf",temp);
        int flag = 0;
        for(int j = 0; j < strlen(a); j++) {
            if(a[j] != b[j]) {
                flag = 1;
            }
        }
        if(flag || temp < -1000 || temp > 1000) {
            printf("ERROR: %s is not a legal number\n", a);
            continue;
        } else {
            sum += temp;
            cnt++;
        }
    }
    if(cnt == 1) 
        printf("The average of 1 number is %.2lf", sum);
    
    else if(cnt > 1) 
        printf("The average of %d numbers is %.2lf", cnt, sum / cnt);
    
    else 
        printf("The average of 0 numbers is Undefined");
    
    return 0;
}

 

自己的代码(越写越乱,又臭又长TAT)

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7FFFFF

int n;

int main()
{
    cin>>n;
    vector<string> v;
    vector<double> vv;
    string s;
    while(n--){
        cin>>s;
        int l=s.size();
        int f;
        if((s[0]>='0'&&s[0]<='9')||s[0]=='-')
            f=1;
        else
            f=0;

        int t=0;
        for(int i=1;i<l;i++){
            if(s[i]=='.'){
                if(t==0)
                    t=i;
                else
                    f=0;
            }
            else if(s[i]<'0'||s[i]>'9'){
                f=0;
                break;
            }
        }

        if(f)
        {
            if(l-1-t>2&&t!=0)
                v.push_back(s);
            else
            {
                double num=0;
                if(s[0]=='-')
                {
                    for(int i=1; i<l; i++)
                    {
                        if(s[i]!='.')
                            num=num*10+(s[i]-'0');
                    }
                    if(t!=0)
                    {
                        num=num*pow(0.1,l-1-t);
                    }
                    num=0-num;
                }
                else
                {
                    for(int i=0; i<l; i++){
                        if(s[i]!='.')
                            num=num*10+(s[i]-'0');
                    }

                    if(t!=0)
                    {
                        num=num*pow(0.1,l-1-t);
                    }
                }
                if(num>=-1000&&num<=1000)
                    vv.push_back(num);
                else
                    v.push_back(s);
            }
        }
        else
            v.push_back(s);
    }
    for(int i=0;i<v.size();i++)
        cout<<"ERROR: "<<v[i]<<" is not a legal number"<<endl;
    double ave=0;
    for(int i=0;i<vv.size();i++){
        ave+=vv[i];
    }
    ave=ave*1.00/(vv.size());
    if(vv.size()==1)
        printf("The average of 1 number is %.2lf\n",ave);
    else if(vv.size()>1)
        printf("The average of %d numbers is %.2lf\n",vv.size(),ave);
    else
        printf("The average of 0 numbers is Undefined\n");

    return 0;
}

 

展开阅读全文

Finding Rectangles

07-31

nConsider the point sets in figures 1a, 2a, and 3a. Using only those points as vertices, figures 1b, 2b, and 3b show all the rectangles that can be formed with horizontal and vertical sides. No rectangles can be formed from the points in figure 4.nnnnYour task is to write a program that can find all rectangles that can be formed from a given set of points. The example input and output given below correspond to the figures above.nnnInputnnThe input contains one or more point sets, followed by a line containing the number 0 that signals the end of the file. Each point set begins with a line containing n, the number of points, and is followed by n lines that describe the points. Each point description contains a capital letter that is the label of the point, then a space, the horizontal coordinate, a space, and the vertical coordinate. Within each set, points labels occur in alphabetical order.nnNote that since each point is labelled with a capital letter there can be at most 26 points. All coordinates are nonnegative integers less than 50. Points within a set are unique.nnnOutputnnThe output for each point set starts with ``Point set ", followed by the number of the point set and a colon. If there are no rectangles, `` No rectangles" appears after the colon. If there are rectangles, they are listed starting on the next line. A blank precedes each rectangle. Each rectangle is given by its vertex labels, in clockwise order from the upper left, so the order is upper left, upper right, lower right, lower left. The rectangles are listed ten per line, except for the last line, where there may be as few as one. The rectangles are listed in alphabetical order.nnSample Inputnn7nA 1 1 nB 2 1 nC 3 1 nD 2 3 nE 3 3 nF 1 4 nG 3 4 n8 nB 1 1 nD 2 1 nF 4 1 nJ 4 4 nL 2 4nM 2 3nN 4 3 nP 1 2 n12nA 1 5nB 2 5nC 1 4nD 2 4nE 1 3nF 2 3nG 1 2nH 2 2nI 1 1nJ 2 1nK 1 0nL 2 0n5nB 1 1nD 2 1nL 2 4nN 2 3nP 1 2n0nnnSample OutputnnPoint set 1:n DECB FGCAnPoint set 2:n LJFD LJNM MNFDnPoint set 3:n ABDC ABFE ABHG ABJI ABLK CDFE CDHG CDJI CDLK EFHGn EFJI EFLK GHJI GHLK IJLKnPoint set 4: No rectanglesn 问答

Finding Seats

11-26

Problem DescriptionnA group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem instead of going on with informal arguments to decide which tickets to buy.nnThe movie theater has R rows of C seats each, and they can see a map with the currently available seats marked. They decided that seating close to each other is all that matters, even if that means seating in the front row where the screen is so big it’s impossible to see it all at once. In order to have a formal criteria, they thought they would buy seats in order to minimize the extension of their group.nnThe extension is defined as the area of the smallest rectangle with sides parallel to the seats that contains all bought seats. The area of a rectangle is the number of seats contained in it.nnThey’ve taken out a laptop and pointed at you to help them find those desired seats.n nnInputnEach test case will consist on several lines. The first line will contain three positive integers R, C and K as explained above (1 <= R,C <= 300, 1 <= K <= R × C). The next R lines will contain exactly C characters each. The j-th character of the i-th line will be ‘X’ if the j-th seat on the i-th row is taken or ‘.’ if it is available. There will always be at least K available seats in total.nInput is terminated with R = C = K = 0.n nnOutputnFor each test case, output a single line containing the minimum extension the group can have.n nnSample Inputn3 5 5n...XXn.X.XXnXX...n5 6 6n..X.X.n.XXX..n.XX.X.n.XXX.Xn.XX.XXn0 0 0n nnSample Outputn6n9 问答

Finding Nemo

03-27

Nemo is a naughty boy. One day he went into the deep sea all by himself. Unfortunately, he became lost and couldn't find his way home. Therefore, he sent a signal to his father, Marlin, to ask for help.nnAfter checking the map, Marlin found that the sea is like a labyrinth with walls and doors. All the walls are parallel to the X-axis or to the Y-axis. The thickness of the walls are assumed to be zero.nnAll the doors are opened on the walls and have a length of 1. Marlin cannot go through a wall unless there is a door on the wall. Because going through a door is dangerous (there may be some virulent medusas near the doors), Marlin wants to go through as few doors as he could to find Nemo.nnFigure-1 shows an example of the labyrinth and the path Marlin went through to find Nemo.nnnnWe assume Marlin's initial position is at (0, 0). Given the position of Nemo and the configuration of walls and doors, please write a program to calculate the minimum number of doors Marlin has to go through in order to reach Nemo.nnInputnnThe input consists of several test cases. Each test case is started by two non-negative integers M and N. M represents the number of walls in the labyrinth and N represents the number of doors. Then follow M lines, each containing four integers that describe a wall in the following format:nx y d tnn(x, y) indicates the lower-left point of the wall, d is the direction of the wall - 0 means it's parallel to the X-axis and 1 means that it's parallel to the Y-axis, and t gives the length of the wall. The coordinates of two ends of any wall will be in the range of [1,199].nnThen there are N lines that give the description of the doors:nnx y dnnx, y, d have the same meaning as the walls. As the doors have fixed length of 1, t is omitted.nnThe last line of each case contains two positive float numbers:nnf1 f2nn(f1, f2) gives the position of Nemo. And it will not lie within any wall or door.nnA test case of M = -1 and N = -1 indicates the end of input, and should not be processed.nnOutputnnFor each test case, in a separate line, please output the minimum number of doors Marlin has to go through in order to rescue his son. If he can't reach Nemo, output -1.nnSample Inputnn8 9n1 1 1 3n2 1 1 3n3 1 1 3n4 1 1 3n1 1 0 3n1 2 0 3n1 3 0 3n1 4 0 3n2 1 1n2 2 1n2 3 1n3 1 1n3 2 1n3 3 1n1 2 0n3 3 0n4 3 1n1.5 1.5n4 0n1 1 0 1n1 1 1 1n2 1 1 1n1 2 0 1n1.5 1.7n-1 -1nSample Outputnn5n-1 问答

Motif Finding

11-01

Problem DescriptionnCell differentiation and development are fundamentally controlled by gene regulation. Only a subset of genes in the genome is expressed in a cell at a given time under given conditions. Regulatory sites on DNA sequence normally correspond to shared conservative sequence patterns among the regulatory regions of correlated genes. We call these conserved sequence motifs. The actual regulatory DNA sites corresponding to a motif are called the instances of that motif. nIdentifying motifs and corresponding instances are very important, so biologists can investigate the interactions between DNA and proteins, gene regulation, cell development and cell reaction.nnnGiven two equal-length strings, the Hamming Distance is the number of positions in which the corresponding characters are different. For example, the Hamming Distance between “ACTG” and “ATCG” is 2 because they differ at the 2nd and 3rd positions.nnYour task is to find a motif for a few DNA sequences. Every DNA sequence consists of only A, C, G, T. If a substring of DNA sequence S has the same length with motif P and their Hamming Distance is not more than d, we say that S includes an instance of P. Given n DNA sequences with length of l. Among them, m sequences are “key sequences”. You need to find out the motif P whose length is w, so that every key sequence includes an instance of motif P. What’s more, the number of DNA sequences which include an instance of P should be as large as possible.nnFor example, n=7, m =7, l=40, w =8, d =2. The sequences are as follows (they are all key sequences):nn![](http://acm.hdu.edu.cn/data/images/con210-1004-1.JPG)nnThe motif P is ATGCAACT.nn nnInputnThe input contains several cases. The first line of each case contains three integers n (1 ≤ n ≤ 15), m (0 ≤ m ≤ n) and l (1 ≤ l ≤ 10,000). The second line contains two integers w (1 ≤ w ≤ 8) and d (0 ≤ d ≤ w). The third line contains m unique integers ranging from 0 to n-1, indicating the key sequences. nnThen followed by n lines, in which each line contains one sequence. The input is terminated by three zeros.n nnOutputnFor each case, output the motif P in one line. If the solution is not unique, then output the lexicographically smallest one. If there is no answer, you should output “No solution”.n nnSample Inputn3 2 4n3 1n0 2nACTTnCGTGnCCCCn3 2 4n3 0n0 2nACTTnCGTGnCCCCn0 0 0n nnSample OutputnCCTnNo solutionn n 问答

没有更多推荐了,返回首页