踩水坑系列一

1、.2lf确实有四舍五入的功能,只是有浮点误差,如用2lf输出1.825和1.815前者是1.83,后者调试是1.814999999999,最终后者输出就是1.81;

*100等扩大范围处理,或化为字符串处理。

例:牛客常大寒假新生赛H题 https://www.nowcoder.net/acm/contest/78/H

 

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#define eps 0.000000001
using namespace std;
//将数扩大100进行处理
//int comp(double x)
//{
//    if(abs(x-0)<eps)    //x==0
//    return 1;
//    else
//    return 0;
//}
int main()
{
    int t;
    double a,b,c,ans;
    //freopen("Atext.in","r",stdin);
    cin >>t;
    while(t--)
    {
        cin >> a >> b >> c;
        ans=a*c*100.0/b;
        if(ans-(int)ans<0.499)      //第三位为4
            ans=(int)ans/100.0;
        else if(ans-(int)ans>0.501)  //第三位为6或为5后面有有效数字
            ans=((int)ans+1)/100.0;
        else
        {
            if(((int)ans)%2==1)  //5前为奇数
                ans=((int)ans+1)/100.0;
            else
                ans=(int)ans/100.0;
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}

 

2、cin确实比较慢,上次CF用cin读入一个字符串二维数组,TLE了,改成%s一行行的读入,就对了,毕竟其实OJ测评只看输出结果;

3、string类如果用scanf得先用a.resize(100)预先分配空间,然后scanf(“%d”,&a[0]),用printf()输出,也得a.c_str();  string不能用cin读入时接受空格;

4.getline有两种用法,可接受任意字符并以‘\n’为结束标志;

getline(cin , s);  或getline(cin , s ,' 分割符');

PAT的ccccL1-025;

题面:

L1-025. 正整数A+B

本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000]。稍微有点麻烦的是,输入并不保证是两个正整数。

输入格式:

输入在一行给出A和B,其间以空格分开。问题是A和B不一定是满足要求的正整数,有时候可能是超出范围的数字、负数、带小数点的实数、甚至是一堆乱码。

注意:我们把输入中出现的第1个空格认为是A和B的分隔。题目保证至少存在一个空格,并且B不是一个空字符串。

输出格式:

如果输入的确是两个正整数,则按格式“A + B = 和”输出。如果某个输入不合要求,则在相应位置输出“?”,显然此时和也是“?”。

输入样例1:
123 456
输出样例1:
123 + 456 = 579
输入样例2:
22. 18
输出样例2:
? + 18 = ?
输入样例3:
-100 blabla bla...33
输出样例3:
? + ? = ?
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
const int maxn=1005;

using namespace std;

int f(string &x)                 //判断正整数
{
    int t,i,flag=0;
    t=x.size();
    //cout  << t << endl;
    if(x=="0"||(t>=4&&x!="1000")||t<1)  // !!!注意坑点不是长度超1000
        return 1;                //0不是正整数
    for(i=0;i<t;i++)
    {
        if(x[i]>='0'&&x[i]<='9')
            continue;
        else
        {
            flag=1;                  //返1---不是正整数;
            break;
        }
    }
    return flag;
}
string sum(string &xx,string &yy)    //大数和
{
    int len1,len2,i,n;
    string maxlen;
    len1=xx.size();
    len2=yy.size();
    if(len1>len2)
        maxlen=xx;
    else
        maxlen=yy;
    n=max(len1,len2);
    while(len1>0&&len2>0)
        maxlen[--n]=xx[--len1]-'0'+yy[--len2];
    n=maxlen.size();
    for(i=n-1;i>0;i--)
    {
        if(maxlen[i]>'9')   //大于9,进位
        {
             maxlen[i]-=10;
             maxlen[i-1]+=1;
        }
    }
    if(maxlen[0]>'9')
    {
        maxlen[0]-=10;
        maxlen="1"+maxlen;
    }
    return maxlen;
}
int main()
{
    int m,n;
    string a,b;
   //a.resize(1000);如果用scanf输入得预先分配空间,printf输出也是
    getline(cin,a,' ');     //!!!注意坑点,A可能为空字符串
    getline(cin,b);
//    cout << b << endl;
    m=f(a);
    n=f(b);
    if(m==1&&n==1)
        cout << "? + ? = ?" << endl;
    else if(m==1&&n==0)
        cout << "? + " << b << " = ?" << endl;
    else if(m==0&&n==1)
        cout << a << " + ? = ?"<< endl;
    else
    {
        cout << a << " + "<< b << " = " << sum(a,b) << endl;
    }
    return 0;
}
//还可以用Java异常处理做;

 5、(亲测)main函数里的int型数组最多可以开到519100(即5*10^5),栈上一般大小为2M或1M,全局或静态即堆上或动态内存分配一般为2G,最多可以开到409000000(即4*10^8),但是同时要注意不能超过数组下标的数据范围,数组下标为std::size_t,32位程序里为unsigned int(4 Bytes)(即2^32-1------4294967295),64位程序为unsigned long(即2^64-1);二维数组的话最大堆上能开22000*22000(亲测);

例:poj3278

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 105001   Accepted: 32843

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
#include <iostream>
#include <cstring>
#include <queue>
const int maxn=200003;  //RE是因为vis定为100000会数组越界,不是queue内存问题!!!
using namespace std;

int n,k;
struct node{
    int x,step;
};
int vis[maxn];
queue<node> Q;
int bfs()
{
    node a,next;
    a.x=n;  a.step=0;
    Q.push(a);      //起点入队首
    while(Q.size()) //队列非空
    {
        a=Q.front();//取队首元素
        //cout << a.x << " ";
        Q.pop();
        if(a.x==k)              //满足条件结束
            return a.step;
        for(int i=0;i<3;i++)
        {
//            if((a.x<=k/2&&i!=2&&a.x!=0)||(a.x<n/2)||a.x<0)//不剪枝会RE(no)受了网上的误导
//                continue;
            if(i==0)            //三种状态转移
                next.x=a.x-1;
            else if(i==1)
                next.x=a.x+1;
            else if(i==2)
                next.x=a.x*2;
            if(next.x>=0&&next.x<=100000&&vis[next.x]==0)//满足条件且未被访问
            {
                next.step=a.step+1;
                Q.push(next);
            }
            if(next.x>=0)
                vis[next.x]=1;
        }
    }
    return 0;
}
int main()
{
    int ans;
    cin >> n >> k;
    memset(vis,0,sizeof(vis));
    if(k<=n)
        ans=n-k;
    else
        ans=bfs();
    cout << ans << endl;
    return 0;
}

 

posted @ 2018-02-14 11:24  Akmpfen  阅读(838)  评论(0编辑  收藏  举报