ACM競賽之輸入輸出

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

 

比賽描述函數

字符串的輸入輸出處理。測試

 

輸入spa

 

第一行是一個正整數N,最大爲100。code

以後輸入多行字符串(行數大於N),  每一行字符串可能含有空格,且字符數不超過1000。blog

 

輸出ci

 

對於前N行字符串,按原樣輸出;字符串

對於其他的字符串以空格符爲分割依次按行輸出。get

注意:每行輸出之間均要輸出一個空行。io

 

樣例輸入

2

N U P Ter

樣例輸出



N

U

P

Ter

 

提示

 

對於輸入輸出仍有困惑的同窗請仔細閱讀如下內容:

 

在ACM競賽中,對於數據的讀入,通常有如下四種狀況:

 

1、四種基本輸入形式:

 

1. 單組輸入數據

 

示例:整數求和

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

 

C語言:

1 #include <stdio.h>
2 int main()
3 {
4     int a,b;
5     scanf("%d %d",&a, &b);
6     printf("%d\n",a+b);
7 }

 

 

C++:

1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5     int a,b;
6     cin>>a>>b;
7     cout<<a+b<<endl;
8     return 0;
9 }

 

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

 

2. 多組輸入數據,且不說明多少組,直到讀至輸入文件末尾爲止

 

示例:A + B Problem (1)

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

 

C語言:

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

 

說明:scanf函數返回值就是讀出的變量個數,如:scanf( 「%d %d」, &a, &b );若是隻有輸入了一個整數,返回值是1,若是輸入了兩個,返回值是2,若是一個都沒有,則返回值是EOF。EOF是一個預約義的常量,等於-1

 

C++:

1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5     int a,b;
6     while(cin>>a>>b)
7         cout<<a+b<<endl;
8     return 0;
9 }

 

說明:表達式cin >> m >> n在讀入發生錯誤返回0,不然返回cin的地址。

 

 

3. 多組輸入數據,不說明多少組,以某特殊輸入爲結束標誌。

 

示例:A + B Problem (2)

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

 

C語言:

 1 #include<stdio.h>
 2 int main()
 3 { 
 4     int a,b;
 5     while(scanf("%d %d",&a,&b)!=EOF)
 6     {
 7             if(a==0&&b==0)
 8                 break;
 9          printf("%d\n",a+b);
10     }
11     return 0;
12 }

 

C++:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a,b;
 6     while(cin>>a>>b)
 7     {
 8         if(a==0&&b==0)
 9             break;
10         cout<<a+b<<endl;
11     }
12     return 0;
13 }

 

說明:當讀入的a與b同時爲0時,程序終止;

 

 

4. 多組輸入數據,開始輸入一個T,接下來是T組數據

 

示例:A + B Problem (3)

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

 

C語言:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int T;
 5     int a,b;
 6     scanf("%d",&T);
 7     while(T--)
 8     {
 9         scanf("%d%d",&a,&b);
10         printf("%d\n",a+b);
11     }
12     return 0;
13 }

 

 

C++:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int T;
 6     int a,b;
 7     cin>>T;
 8     while(T--)
 9     {
10         cin>>a>>b;
11         cout<<a+b<<endl;
12     }
13     return 0;
14 }

 

說明:當T組數據處理完後,程序終止;

 

關於字符串的讀入,這裏再作專門討論:

 

2、字符串輸入

  對字符串的輸入分三種狀況:

  

 

    一、每一個字符串中不含空格、製表符及回車

  這種狀況,用scanf函數是再好不過的了;

 

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

那麼只要:

char str[10]; 

  

scanf("%s",str); 

 

說明:scanf函數讀入字符串時,是以空格、製表符及回車做爲不一樣字符串之間的分隔符的;

 

 

  二、字符串中含有空格、製表符,但不含回車

  對於這種狀況不能使用scanf,而應該使用gets函數;

 

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

那麼只要:

  char str[10];

  

 gets(str);  

 

 

說明:gets函數讀入字符串時,只以回車做爲不一樣字符串之間的分隔符;另外,若是要用gets讀入多個字符串,能夠寫成 while(gets(str)){......}

 

 

  三、字符串中含回車

  在這種狀況下,若是沒有題目的說明,程序沒法知道哪裏是字符串的分界。那麼,用scanf("%c",&ch)來讀,一邊讀,一邊判斷分界條件是否知足,若是知足,則把當前讀到的東西存到一個字符串中。

 

3、輸出處理

 

通常來說,輸出處理通常只有兩個問題:空行打印問題與浮點數的精度問題;

 

 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語言:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,sum,a;
 5     while(scanf("%d",&n) && n)
 6     {
 7          sum=0;
 8          while(n--)
 9          {
10              scanf("%d",&a);
11              sum+=a;
12          }
13          printf("%d\n",sum);
14          printf("\n");
15     }
16     return 0;
17 }

 

C++:

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,sum,a;
 6     while(cin>>n&&n)
 7     {
 8          sum=0;
 9          while(n--)
10          {
11              cin>>a;
12              sum+=a;
13          }
14          cout<<sum<<endl;
15          cout<<endl;
16     }
17     return 0;
18 }

 

 

(2)Between

Between和After不一樣的是,最後一組結果後面不該該再加單獨的"\n",應該像這樣:

1 int i;
2 for (i = 0; i < 10; i++)
3 {
4     printf("%d\n", a);
5     if (i != 9)
6         printf("\n");
7 }

 

因爲有時候咱們並不知道測試數據有幾組(好比測試數據是以end of file 結束的),用上面的方法就不行了,因而,能夠換一種寫法:

 1 int a;
 2 bool first = true;
 3 while(scanf("%d", &a) == 1)
 4 {
 5     if (!first)
 6         printf("\n");
 7     else
 8         first = false;
 9     printf("%d\n", a);
10 }

 

這樣,從第二組測試數據起,在輸出每組測試數據的結果以前就會輸出一個空行,和想要的效果是同樣的。

 

 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);

 

 

 

 

題目來源

NUPT

相關文章
相關標籤/搜索