1、参数解析
void CmdlineParse(const string &str)
{
vector<string> svec;
string temp = "";
bool flag = false; //判断是否处于字符串的状态
for (int i = 0; i<str.size(); ++i){
if (str[i] == '"')//字符串的起始或结束
{
flag = !flag;
}
else if (str[i] == ' '&&!flag) //参数的分隔符或是否为字符串的内容
{
svec.push_back(temp);
temp = "";
}
else
{
temp += str[i];
}
}
svec.push_back(temp); //最后一个参数
cout << svec.size() << endl;
for (int i = 0; i<svec.size(); ++i){
cout << svec[i] << endl;
}
}
int main() {
string str = "";
while (getline(cin, str)) {
CmdlineParse(str);
}
}
2、跳石板(动态规划)
void get_div_num(vector<int>& a, int x) {
for (int i = 2; i <= sqrt(x); ++i) {
if (x % i == 0) {
a.push_back(i);
if (x / i != i)
a.push_back(x / i);
}
}
}
int Jump(int n, int m) {
vector<int> step(m + 1, INT_MAX);
step[n] = 0;
for (int i = n; i < m; ++i) {
if (step[i] == INT_MAX)
continue;
vector<int> a;
get_div_num(a, i);
for (int j = 0; j < a.size(); j++) {
if (i + a[j] <= m && step[i + a[j]] != INT_MAX) {
step[j + a[j]] = step[i + a[j]] > step[i] + 1 ? step[i] + 1 : step[i + a[j]];
}
else if (a[j] + i <= m) {
step[a[j] + i] = step[i] + 1;
}
}
}
return step[m] == INT_MAX ? -1 : step[m];
}
int main() {
int n, m;
int min_step = 0;
while (cin >> n >> m) {
min_step = Jump(n, m);
cout << min_step << endl;
}
return 0;
}
3、计算日期到天数转换
//判断闰年
bool isleap(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
//根据年与月返回当月的天数
int GetDaysByYM(int year, int month)
{
int days[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && isleap(year))
{
month = 0;
}
return days[month];
}
//根据年月日返回当前日期是当年的第几天
int GetdateByYMD(int year, int month, int day)
{
int days = 0;
//循环加上月数减一的整天数
for (int i = 1; i<month; ++i){
days += GetDaysByYM(year, i);
}
//最后加上当月已经过的天数day
days += day;
return days;
}
int main() {
int year, month, day;
while (cin >> year >> month >> day) {
day = GetdateByYMD(year, month, day);
cout << day << endl;
}
}
4、幸运袋子(回溯)
int getLuckyPacket(vector<int> &x, int n, int pos, int sum, int multi){
int count = 0;
for (int i = pos; i<n; i++)
{
sum += x[i];
multi *= x[i];
//判断幸运
if (sum>multi)
{
count += 1 + getLuckyPacket(x, n, i + 1, sum, multi);
}
else if (x[i] == 1) //1 1 3
{
count += getLuckyPacket(x, n, i + 1, sum, multi);
}
else //1 3 1 ----- 1 1 3
{
break;
}
sum -= x[i];
multi /= x[i];
while (i<n - 1 && x[i] == x[i + 1])
{
i++;
}
}
return count;
}
int main() {
int n, m;
while (cin >> n) {
vector<int> x(n);
for (int i = 0; i<n; ++i)
{
cin >> x[i];
}
sort(x.begin(), x.end());
cout << getLuckyPacket(x, n, 0, 0, 1) << endl;
}
return 0;
}