2015年12月7日

来源:互联网 发布:学编程要会英语吗 编辑:程序博客网 时间:2024/05/04 02:12

嗯…啊…
没想是这个时候更新的日志。
我在做计蒜课的题目呢,先放代码。
超时了的程序,我在改进。

#include <iostream>#include <cstdio>using namespace std;int main(){    int x;    int i;    while(scanf("%d",&x)!=EOF)    {        for(i=1;1;i++)        {            if(x==0){i=0;break;}            else if((i-1)*(i-1)==x){i-=1;break;}            else if((i+1)*(i+1)==x){i+=1;break;}            else if(((i-1)*(i-1))<x&&x<((i+1)*(i+1)))break;            else continue;        }        printf("%d\n",i);    }    return 0;}

mark一下。
下面是解出一组数据的代码:
用了牛顿迭代法

#include <iostream>#include <cstdio>using namespace std;double my_sqrt(double a){    double x;    x=a;    for(int i=1;i<=10;i++)    //要求精度高的话,可以设置次数多些,比如100        x=(x+a/x)/2;    return x; }int main(){    int x;    int i;    int mid;    while(scanf("%d",&x)!=EOF)    {        if(x==0)            i=0;        else if(x<0)            i=-1;        else             i=my_sqrt(x);        printf("%d\n",int(i));    }    return 0;}

先End。

0 0
原创粉丝点击