浙大1303 手機靚號
手機靚號
Time Limit:1000MS Memory Limit:32768K
Description:
小風涼剛買了手機,去營業(yè)廳開戶,營業(yè)員讓其挑選自己喜歡的號碼,但那么多的號碼著實讓他眼花,他對號碼的要求是在號碼中6和8的個數(shù)要超過5個,并且沒有數(shù)字4,于是就由我給他編了一個程序。
Input:
輸入不多于50組的數(shù)(手機號碼),每個數(shù)以13或15開頭,長為11位。
Output:
輸出數(shù)中,含有6和8不少于5個,并且沒有4的數(shù),并統(tǒng)計號碼的個數(shù)。
Sample Input:
13656689866
13805880343
15967126781
13808866888
Sample Output:
13656689866
13808866888
2
解答:
#include <iostream>
#include <string>//用string,可以很簡單地檢測出輸入的一串字符的結尾
using namespace std;
bool f(string &str)
{
int c=0,len=str.size(); //不包括最后的結束符
for(int i=0;i<len;i++)//不要寫成i<=len,因為作為數(shù)組下標它是從0到len-1的
{
if(str[i]=='8' || str[i]=='6') c++;
if(str[i]=='4') return false;
}
if(c>=5) return true;
else
return false;
}
int main()
{string str;
bool flag;
int t=0;;
while(cin>>str) //接收到回車就可以判斷出是輸入完成了
{
flag=f(str);//要給參數(shù)
if(flag)
{
cout<<str<<endl;
t++;
}
}
cout<<t<<endl; //要按ctrl+z才能看到輸出,
//在vc6.0下需要按兩次,這是vc6.0的一點bug
system("pause");
return 0;
}
文章來源:
http://www.cnblogs.com/qnbs1/archive/2010/03/21/1691078.html