20191201
#include "iostream"
using namespace std;
bool is_seven(int n){
if(n%7==0) return 1;
while(n){
if(n%10==7) return 1;
n/=10;
}
return 0;
}
int main(int argc, char* argv[])
{
int n;
cin>>n;
int now = 1;
int num_a=0,num_b=0,num_c=0,num_d=0;
int num = 1;
while(n){
switch(now){
case 1:
if(is_seven(num)){
num_a++;
}else{
n--;
}
break;
case 2:
if(is_seven(num)){
num_b++;
}else{
n--;
}
break;
case 3:
if(is_seven(num)){
num_c++;
}else{
n--;
}
break;
case 4:
if(is_seven(num)){
num_d++;
}else{
n--;
}
break;
}
now++;
num++;
if(now == 5) now =1;
}
cout<<num_a<<endl<<num_b<<endl<<num_c<<endl<<num_d;
return 0;
}
20191202
參考了https://blog.csdn.net/richenyunqi/article/details/103988931
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
// 由於unordered_map是採用哈希實現的,對於系統的類型int, string等,都已經定義好了hash函數,
// 所以如果我們引入新的自定義類型的話,系統並不知道如何去計算我們引入的自定義類型的hash值,
// 所以我們就需要自己定義hash函數,告訴系統用這種方式去計算我們引入的自定義類型的hash值
struct arrayHash
{
//我試過return 0,也AC了,是不是用例中沒產生衝突呢?
LL operator()(const array<LL, 2> &p) const { return p[0] * 1e9 + p[1]; }
};
int main()
{
/*
用map存儲整個棋盤的所有棋子
@array<LL,2> 用一個二維數組表示棋子橫縱座標
@array<LL,2> 用一個二維數組表示棋子以下信息:
@@LL 表示該棋子上下左右相鄰點個數
@@LL 表示該棋子對角線相鄰點個數
@array_hash 數組做爲key的哈希函數
*/
unordered_map<array<LL, 2>, array<LL, 2>, arrayHash> um;
LL n;
cin >> n;
while (n--)
{
array<LL, 2> p;
cin >> p[0] >> p[1];
um.insert({p, {0, 0}}); //存儲當前棋子
for (auto &elemnent : um)
{
auto &q = elemnent.first; //q是每個元素的別名
if ((q[0] == p[0] and abs(q[1] - p[1]) == 1) or (q[1] == p[1] and abs(q[0] - p[0]) == 1))
{
um[p][0]++;
um[q][0]++;
//上下左右的棋子數目加一
}
else if (abs(q[0] - p[0]) == 1 and abs(q[1] - p[1]) == 1)
{
um[p][1]++;
um[q][1]++;
//斜對角棋子數目加一
}
}
}
array<LL, 5> ans{};
for (auto &element : um)
{
auto &nums = element.second;
if (nums[0] == 4)
{
ans[nums[1]]++;
}
}
for (auto nums : ans)
{
cout << nums << endl;
}
return 0;
}
20191203
水了50分
刪了
測試用例:
8
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
4Zn(NO3)2+NH4NO3+3H2O=4Zn+10HNO3
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
上面寫的太亂了,重做了下,結果是超時,60分
是因爲string用太多才超時嗎?
#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
int match_num(string s, int &pos)
{ //匹配數組 返回數值 pos存儲當前指針
string num = "";
while (isdigit(s[pos]))
{
num += s[pos];
pos++;
}
return atoi(num.c_str());
}
string match_element(string s, int &pos)
{
string ele = "";
if (s[pos] >= 'A' and s[pos] <= 'Z')
{
ele += s[pos];
pos++;
if (s[pos] >= 'a' and s[pos] <= 'z')
{
ele += s[pos];
pos++;
}
}
return ele;
}
void sum(string s)
{
int p = 0;
int times = 1;
//處理單個單詞的函數
//1.獲取首字母的倍數
if (isdigit(s[0]))
{
times = match_num(s, p);
}
//2.開始匹配
string last;
while (s[p])
{
if (isalpha(s[p]))
{
string ele = match_element(s, p);
last = ele;
mp[ele] += times;
}
else if (isdigit(s[p]))
{
int num = match_num(s, p);
mp[last] -= times;
mp[last] += times * num;
}
else if (s[p] == '(')
{
//假設括號嵌套 不會出現(OP)(OH)這樣的情況
int t = s.find_last_of(')') + 1;
int tmp_t = t; //t值會變,保存下
int num = match_num(s, t);
num *= times;
stringstream ss;
ss << num;
string tmp = ss.str();
for (int i = p + 1; i < tmp_t - 1; i++)
{
tmp += s[i];
}
sum(tmp);
p = t;
}
}
}
void sub(string s)
{
int p = 0;
int times = 1;
//處理單個單詞的函數
//1.獲取首字母的倍數
if (isdigit(s[0]))
{
times = match_num(s, p);
}
//2.開始匹配
string last;
while (s[p])
{
if (isalpha(s[p]))
{
string ele = match_element(s, p);
last = ele;
mp[ele] -= times;
}
else if (isdigit(s[p]))
{
int num = match_num(s, p);
mp[last] -= times * num;
mp[last] += times;
}
else if (s[p] == '(')
{
//假設括號嵌套 不會出現(OP)(OH)這樣的情況
int t = s.find_last_of(')') + 1;
int tmp_t = t; //t值會變,保存下
int num = match_num(s, t);
num *= times;
stringstream ss;
ss << num;
string tmp = ss.str();
for (int i = p + 1; i < tmp_t - 1; i++)
{
tmp += s[i];
}
sub(tmp);
p = t;
}
}
}
void check(string s, void (*func)(string))
{
if (s.size() == 0)
return;
string one = "";
int p = 0;
while (s[p] != '+')
{
one += s[p++];
if (p == s.size())
{
func(one);
return;
}
}
func(one);
check(s.substr(p + 1, s.size()), func);
}
int main()
{
int n;
cin >> n;
cin.get();
while (n--)
{
string s;
getline(cin, s);
auto p = s.find('=');
check(s.substr(0, p), sum);
// for(auto i : mp){
// cout<<i.first<<" "<<i.second<<endl;
// }
check(s.substr(p + 1, s.size()), sub);
// for(auto i : mp){
// cout<<i.first<<" "<<i.second<<endl;
// }
bool f = true;
for (auto i : mp)
{
if (i.second != 0)
{
f = false;
break;
}
}
mp.clear();
if (f == false)
{
cout << "N" << endl;
}
else
{
cout << "Y" << endl;
}
}
return 0;
}