【LeetCode & 劍指offer刷題】熟悉OJ平台2:如何處理輸入問題


筆試中的編程題一般用OJ平台(如牛客網),而這些平台絕大部分都會要求自己寫輸入部分(不同於leetcode),如果對輸入部分不熟悉的話會浪費很多時間,所以這一部分需熟練掌握。

輸入問題
 
1 整數輸入問題
//最簡單的輸入,輸入單行
Sample Input 1 2 
Sample Output 3
 
#include <iostream> 
using namespace std ;  
int   main ()  
{  
    int a , b ;  
    cin >> a >> b ;  
    cout << a + b << endl ;    //對其他題目,換成要求的復雜處理與輸出 
    return 0 ;  
}
 
/*
輸入多行數時,直到讀至輸入文件末尾(EOF)為止
說明1:當讀到輸入結束時,cin >> a >> b返回 0,循環也結束。
說明2:在調試程序時,鍵盤輸入的數據,用CTRL-Z(即按住CTRL鍵不放,再按下Z)組合作為輸入結束,此謂鍵盤輸入設備的“文件末尾”。
重點掌握
 
Sample Input 
1 5 
10 20 
400 516 
Sample Output 
30 
916
*/
#include <iostream>  
using namespace std ;  
int main ()  
{  
    int a , b ;  
    while ( cin >> a >> b )  //當題目輸入行數不確定時使用此方法
    {  
        cout << a + b << endl ;  
    }  
    return 0 ;  
}
/*
多組由兩個整數(a和b)構成的輸入,a和b之間用空格隔開,每組輸入單獨占一行。
當輸入為 0 0 時,輸入結束
Sample Input 
1 5 
10 20 
0 0 
Sample Output 
30
*/
#include<iostream> 
using namespace std ;  
int main ()  
{  
    int a , b ;  
    while ( cin >> a >> b &&( a || b ))  
    {  
        cout << a + b << endl ;  
    }  
    return 0 ;  
}  
 
/*
第一行是數據的組數N
從第二行是N組由兩個整數(a和b)構成的輸入,a和b之間用空格隔開,每組輸入單獨占一行 
重點掌握
 
Sample Input 
1 5 
10 20 
Sample Output 
30
*/
#include <iostream> 
using namespace std ;  
int main () {  
    int a , b , n ;  
    cin >> n; 
    for (int  i = 0 ; i < n ; i ++)  
    {  
        cin >> a >> b ;  //cin以空格或者回車作為輸入輸出分隔符
        cout << a + b << endl ;  
    }  
    return 0 ;  
}
 
 
// 利用文件重定向提高調試效率
#include<iostream>
#include<cstdio>
using namespace std ;
int main ()
{
    freopen ( "input.txt" , "r" , stdin );   // 將輸入重定向到文件 input.txt (注意文件路徑)
    int a , b ;
    cin >> a >> b ;
    cout << a + b << endl ;
    return 0 ;
} // 在運行程序前,將本該由鍵盤輸入的數據,寫到文件 input.txt 中。而在運行程序時,數據將不再需要人去輸入
 
2 讀取和解析標點字符(如逗號)分隔數據
 
/*
處理輸入問題:讀取以逗號間隔的數字到數組中
例:
輸入:1,12,123
數組a:a[0] = 1,a[1] = 12, a[2] = 123
*/
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
using namespace std ;
int main ()
{
    vector < int > a ;
    string s ;
    cin >> s ; //讀取輸入字符串到s
    stringstream input ( s ); //將字符串s轉化為流
    string numstr ;
     while ( getline ( input , numstr , ',' )) //按逗號分隔為字符串( getline每次讀一個
     {
        a . push_back ( stoi ( numstr ));
     }
     return 0 ;
}
 
/* 例子
輸入:
2
19:90:23
23:59:59
輸出:
19:00:23
23:59:59
*/
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std ;
int main ()
{
     int n ;
    cin >> n ;
     for ( int i = 1 ; i <= n ; i ++)
     {
        string numstr ;
        string output ;
        string inputstr ;
        cin >> inputstr ; // 讀取一行字符串,方便處理
        stringstream input ( inputstr ); // 將字符串 s 轉化為流 , cin 需要考慮回車問題,用流就比較方便(因為 inputstr 已經跳過回車字符)
        
         int count = 1 ;
         while ( getline(input, numstr, ':' )) //getline 會將其分隔轉換為字符串
         {
             int num = stoi ( numstr ); // 假設 num 一定為正數
             if ( count == 1 )
             {
                 if ( num > 23 )
                    numstr [ 0 ] = '0' ;
                output += numstr + ':' ;
             }
             else
             {
                 if ( num > 59 )
                    numstr [ 0 ] = '0' ;
                output += numstr + ':' ;
             }
            
            count ++;
         }
        output . pop_back (); // 移除末尾字符 ':'
        cout << output << endl ;
     }
}*
 
 
思路:使用 getline 和 stringstream 以   ','   為分隔符來 切分數據 ,然后使用標准庫 string 的數值轉換函數例如字符串轉整形   stoi  進行解析
注意: 當數據以空格分隔時,可以直接用cin來讀入!
 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM