各种输入输出

c++常用:

#include <fstream.h>

ifstream filein("data.in");   // 定义一个文件输入流

ofstream fileout("data.out"); //cout<< --> fileout<<

filein.eof() //文件到末尾,返回非零值

data.in表示输入的数据文件

本地测试的话本来输入的数据就要在这个文件里面测试了

建一个本地的文本data.in,可以用记事本的方式打开

注意:文件输入的话,以后的cin>>都要改成filein>>, cout<<都要改成fileout<<。


以下来源于: 点击打开链接

一、四种基本输入形式:


1. 单组输入数据


示例:整数求和

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1001


C语言:

#include <stdio.h>
int main()
{
    int a,b;
    scanf("%d %d",&a, &b);
    printf("%d\n",a+b);
}


C++:

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    cout<<a+b<<endl;
    return 0;
}

注意:输入前无需也不要输出任何提示信息。


2. 多组输入数据,且不说明多少组,直到读至输入文件末尾为止


示例:A + B Problem (1)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1084


C语言:

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

说明:scanf函数返回值就是读出的变量个数,如:scanf( “%d %d”, &a, &b );如果只有输入了一个整数,返回值是1,如果输入了两个,返回值是2,如果一个都没有,则返回值是EOF。EOF是一个预定义的常量,等于-1


C++:

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b)
        cout<<a+b<<endl;
    return 0;
}

说明:表达式cin >> m >> n在读入发生错误返回0,否则返回cin的地址。



3. 多组输入数据,不说明多少组,以某特殊输入为结束标志。


示例:A + B Problem (2)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1085


C语言:

#include<stdio.h>
int main()
{ 
    int a,b;
    while(scanf("%d %d",&a,&b)!=EOF)
    {
            if(a==0&&b==0)
                break;
         printf("%d\n",a+b);
    }
    return 0;
}

C++:

#include<iostream>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        if(a==0&&b==0)
            break;
        cout<<a+b<<endl;
    }
    return 0;
}

说明:当读入的a与b同时为0时,程序终止;



4. 多组输入数据,开始输入一个T,接下来是T组数据


示例:A + B Problem (3)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1086


C语言:

#include<stdio.h>
int main()
{
    int T;
    int a,b;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&a,&b);
        printf("%d\n",a+b);
    }
    return 0;
}


C++:

#include<iostream>
using namespace std;
int main()
{
    int T;
    int a,b;
    cin>>T;
    while(T--)
    {
        cin>>a>>b;
        cout<<a+b<<endl;
    }
    return 0;
}

说明:当T组数据处理完后,程序终止;


关于字符串的读入,这里再做专门讨论:

二、字符串输入

  对字符串的输入分三种情况:

  


    1、每个字符串中不含空格、制表符及回车

  这种情况,用scanf函数是再好不过的了;

例如:要读入字符串"abcdef"

那么只要:

char str[10];
scanf("%s",str);

说明:scanf函数读入字符串时,是以空格、制表符及回车作为不同字符串之间的分隔符的;



  2、字符串中含有空格、制表符,但不含回车

  对于这种情况不能使用scanf,而应该使用gets函数;

例如:要读入字符串 "Hello world!"

那么只要:

char str[10];
gets(str);

说明:gets函数读入字符串时,只以回车作为不同字符串之间的分隔符;另外,如果要用gets读入多个字符串,可以写成 while(gets(str)){......}



  3、字符串中含回车

  在这种情况下,如果没有题目的说明,程序无法知道哪里是字符串的分界。那么,用scanf("%c",&ch)来读,一边读,一边判断分界条件是否满足,如果满足,则把当前读到的东西存到一个字符串中。


三、输出处理


一般来讲,输出处理一般只有两个问题:空行打印问题与浮点数的精度问题;


 1. 关于空行(Blank line)

    很多题目都要求在输出数据的恰当位置加空行。一个空行就是一个单独的"\n"。这里,有的题目说:“After each test case, you should output one blank line”,而有的题目说:“Between each test case, you should ouput one blank line”。要注意After和Between的区别,因为如果多了一或少了空行,将导致Presentation Error甚至Wrong Answer。


(1)After

这种情况最简单,只需要输出结果后,再加一个printf("\n"),:


示例:A + B Problem (4)

http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1087


C语言:

#include<stdio.h>
int main()
{
    int n,sum,a;
    while(scanf("%d",&n) && n)
    {
         sum=0;
         while(n--)
         {
             scanf("%d",&a);
             sum+=a;
         }
         printf("%d\n",sum);
         printf("\n");
    }
    return 0;
}

C++:

#include<iostream>
using namespace std;
int main()
{
    int n,sum,a;
    while(cin>>n&&n)
    {
         sum=0;
         while(n--)
         {
             cin>>a;
             sum+=a;
         }
         cout<<sum<<endl;
         cout<<endl;
    }
    return 0;
}


(2)Between

Between和After不同的是,最后一组结果后面不应该再加单独的"\n",应该像这样:

int i;
for (i = 0; i < 10; i++)
{
    printf("%d\n", a);
    if (i != 9)
        printf("\n");
}

由于有时候我们并不知道测试数据有几组(比如测试数据是以end of file 结束的),用上面的方法就不行了,于是,可以换一种写法:

int a;
bool first = true;
while(scanf("%d", &a) == 1)
{
    if (!first)
        printf("\n");
    else
        first = false;
    printf("%d\n", a);
}

这样,从第二组测试数据起,在输出每组测试数据的结果之前就会输出一个空行,和想要的效果是一样的。

 2.关于精度

 

 (1)结果保留x位小数

这种比较简单,只要 printf("%.xf\n",ans);  即可;

例如,要求保留6位小数: printf("%.6f\n",ans);

 

 (2)没有说明要求保留几位,但要求与结果的误差不大于1e-x;

解决 : printf("%.(x+3)f\n",ans);

例如:要求与结果的误差不大于1e-9 : printf("%.12f\n",ans);


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值