2018年全國多校算法寒假訓練營練習比賽(第一場)I

鏈接:https://www.nowcoder.com/acm/contest/67/I
來源:牛客網

題目描述

    lulu喜歡小於等於1000的正整數,但是如果某個數是a或b的倍數,lulu會討厭這個數。如果某個數裏包含了a和b兩個數裏包含的數,lulu也會討厭。(例如a=14,b=23,如果數字中包含1、2、3、4這四個數中的任意一個數,lulu就會討厭這個數)。現在告訴你a,b,你能說出lulu喜歡的數有多少個麼。

輸入描述:

第一行是樣例數T
第2到2+T-1行每行有2個整數a b。

輸出描述:

輸出lulu喜歡的數的個數
直接暴力破解了,我寫的比較麻煩,不過能過都是可以的;
代碼:
#include<iostream> 
#include<string>
#include<sstream>
using namespace std;
int main()
{
	int T;
	cin >> T;
	getchar();
	while(T--)
	{
		string s,s1;
		getline(cin,s);
		int x = 0;
		int t = 0;
		for(int i = 0;i < s.size();i++)
		{
			if(s[i]!=' ')
			{
				s1.push_back(s[i]);
			}
			else{
				t = i;
			}
		}
		string ss1 = s.substr(0,t+1);
		string ss2 = s.substr(t+1);
		int x1,x2;
		stringstream sx(ss1);
		sx >> x1;
		stringstream sx1(ss2);
		sx1 >> x2;
		int count = 0;
		for(int i = 1; i <= 1000;i++)
		{
			string s2;
			stringstream ss(s2);
			ss << i;
			s2 = ss.str();
			bool isok = 0;
			for(int j = 0;j < s1.size();j++)
			{
				if(s2.find(s1[j])!=string::npos)
				{
					isok = 1;
					break;
				}
			}
			if(isok == 0)
			{
				if(i%x1 == 0 || i%x2 == 0)
				{
					isok = 1;
				}
			}
			if(!isok)
			{
				count++;
			}
		}
		cout << count << endl;
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章