2018大疆創新A卷
阿新 • • 發佈:2018-12-31
遍歷一遍字串,找到 j 就去統計前面的d的數量 和 後面 i的數量,然後乘起來,累加到sum中去(sum = 0)。找完所有的 j 前後d和i的數量的乘積後,最後sum就是得到的值
比如 ddabcjabci
只有一個j,前面兩個d * 後面一個 i,==2,輸出結果就是2
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector<string> ve;
for (int i = 0; i < n; ++i)
{
string str;
cin>>str;
ve.push_back(str);
}
for(int i = 0; i < n; ++i)
{
int sum = 0,countd,counti;
string temp = ve[i];
for(int j = 0; j < temp.size(); ++j)
{
countd = 0;
counti = 0 ;
if(temp[j] == 'j')
{
for(int k = 0; k < j; ++k)
{
if(temp[k] == 'd')
countd++;
}
for(int m = j+1; m <= temp.size(); ++m)
{
if (temp[m] == 'i')
counti++;
}
sum += countd*counti;
}
}
cout<<sum<<endl;
}
return 0;
}
兩個數異或,然後統計異或後的數中1的個數,就是不同位的個數
#include <stdio.h>
static unsigned int count = 0;
void print(unsigned int n,unsigned int m)
{
unsigned int k = n^m;
while(k)
{
k = k&(k-1);
count++;
}
}
int main()
{
unsigned int n;
scanf("%ud",&n);
unsigned int a[n];
for(unsigned int i = 0; i < n; ++i)
{
scanf("%ud",&a[i]);
}
for(unsigned int i = 0; i < n; ++i)
{
for(unsigned int j = i+1; j < n; ++j)
{
print(a[i],a[j]);
}
}
printf("%u\n",count);
return 0;
}
比較繁瑣,只能分條件進行遍歷。
#include<iostream>
#include<string>
#include<vector>
#include<unordered_map>
#include<map>
#include<sstream>
#include<algorithm>
using namespace std;
int main()
{
int N;
cin>>N;
vector<string> vec;
map<string, vector<int> > hashtable; //輸出要按照時間大小,所以用map,擁有自動排序功能。
if (getchar() == '\n')
{
;
}
for (int i = 0; i < N; i++)
{
string s;
getline(cin, s);
vec.push_back(s);
}
int mapMonthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //這個對映用來表示每個月有多少天
for (int i = 0; i < vec.size(); i++)
{
string temp = vec[i];
for (int j = 0; j < temp.size(); j++)
{
if (temp[j] == ' ')
{
string key = temp.substr(0, j);
string value = temp.substr(j + 1);
string time = value;
int hour = atoi((time.substr(0, time.find(':'))).c_str());
time = time.substr(time.find(':') + 1);
int min = atoi((time.substr(0, time.find(':'))).c_str());
int second = atoi(time.substr(time.find(':') + 1).c_str());
int seconds = hour * 60 * 60 + min * 60 + second;
if (hour >= 3)
{ //如果時間大於等於凌晨3點
hashtable[key].push_back(seconds - 3 * 60 * 60); //以凌晨3點為基準,重新計算,單位秒,作為時間軸。
} else
{ //時間小於凌晨3點
int months = atoi(key.substr(0, key.find('.')).c_str());
int days = atoi(key.substr(key.find('.') + 1).c_str());
if (days != 1)
{ //如果日期不是1號,要往前算一天
string month = to_string(months);
if (months < 10)
{ //考慮數字是否小於10,小於10,需要在字串中加"0"
month = string("0") + month;
}
string day = to_string(days - 1);
if (days - 1 < 10)
{
day = string("0") + day;
}
key = month + string(".") + day;
}
else
{ //如果是一號,月份同樣往前算一個月(這裡沒有從1月份往前推,估計應該不太可能)
string month = to_string(months - 1);
if (months - 1 < 10)
{
month = string("0") + month;
}
string day = to_string(mapMonthDays[months - 2]);
if (mapMonthDays[months - 2] < 10)
{
day = string("0") + day;
}
key = month + string(".") + day;
}
hashtable[key].push_back(seconds + 21 * 60 * 60); //所有時間以凌晨3點為基準,重新計算
}
break;
}
}
}
map<string, vector<int> >::iterator iter = hashtable.begin();
int idle_start = 12 * 60 * 60 + 30 * 60 - 3 * 60 * 60; //休息起始時間
int idle_end = 14 * 60 * 60 - 3 * 60 * 60; //休息結束時間
for (; iter != hashtable.end(); iter++)
{
if ((iter->second).size() >= 2)
{
sort((iter->second).begin(), (iter->second).end()); //對時間進行排序
vector<int> temp = iter->second;
int result = 0;
if (temp[temp.size() - 1] < idle_start)
{ //考慮工作時間段與休息時間段的重疊,每次先固定最後打卡時間點,再分析最先打卡時間點
result = temp[temp.size() - 1] - temp[0];
}
else if (temp[temp.size() - 1] >= idle_start && temp[temp.size() - 1] < idle_end)
{
if (temp[0] < idle_start)
{
result = idle_start - temp[0];
}
}
else
{
if (temp[0] < idle_start)
{
result = idle_start - temp[0] - (1 * 60 * 60 + 30 * 60);
}
else if(temp[0] >= idle_start && temp[0] < idle_end)
{
result = temp[temp.size() - 1] - idle_end;
}
else
{
result = temp[temp.size() - 1] - temp[0];
}
}
if (result > 0)
{
cout<<iter->first<<" "<<temp[temp.size() - 1] - temp[0]<<endl;
}
}
}
return 0;
}