4399编程题
1,好像是求巡回数?欢迎讨论#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
using namespace std;
int main()
{
string str;
int len;
string str1;
for (int i = 12; i < 1000; i++)
{
stringstream ss;
ss << i;
ss >> str;
str1 = str;
len = str1.length() ;
if (str[len - 1] == str[len - 2])
continue;
str1 = str.substr(0, len-1);
reverse(str1.begin(), str1.end());
cout << (str + str1) << endl;
}
system("pause");
}
第二题
求是否休息,写的比较乱,就是先求相隔的天数,再求是否是周末
#include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
string res;
string cal(int sumdays)
{
if (sumdays % 7 != 6 && sumdays % 7 != 0)
res = "no";
else
{
int time = sumdays / 7;
if (time % 2 == 1)
res = "yes";
else
{
if (sumdays % 7 == 6)
res = "no";
}
}
return res;
}
int isLeapyear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
else
return 0;
}
int main()
{
int year, month, day;
string str;
int arr[2][12] = { {31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31} };//非瑞年二月为29天,非瑞年二月为28天
int sumdays = 0;//相隔的天数
int year0 = 2018, month0 = 8, day0 = 14;//14是周天
while (cin >> str)
{
stringstream ss;
string str1, str2, str3;
int pos1 = str.find(',');
int pos2 = str.rfind( ',');
str1 = str.substr(0, pos1);
str2 = str.substr(pos1 + 1, pos2 - pos1-1);
str3 = str.substr(pos2 + 1);
ss << str1;
ss >> year;
stringstream ss1;
ss1 << str2;
ss1 >> month;
stringstream ss2;
ss2 << str3;
ss2 >> day;
if (year == year0 && month == month0)
{
sumdays = day - day0;
cal(sumdays);
}
else
if (year == year0)
{
sumdays += 31 - day0 + day;
for (int i = month0 ; i < month-1; i++)
{
sumdays += arr[0][i];
}
cal(sumdays);
}
else
{
sumdays += arr[0][month0] - day0;
for (int i = month0; i < 12; i++)
sumdays += arr1[i];
for (int i = year0 + 1; i < year; i++)
{
if (isLeapyear(i))
{
sumdays += 366;
}
else
{
sumdays += 365;
}
}
for (int i = 0; i < month - 1; i++)
sumdays += arr[isLeapyear[year]][i];
sumdays += day;
cal(sumdays);
}
cout << res << endl;
}
}
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
string res;
string cal(int sumdays)
{
if (sumdays % 7 != 6 && sumdays % 7 != 0)
res = "no";
else
{
int time = sumdays / 7;
if (time % 2 == 1)
res = "yes";
else
{
if (sumdays % 7 == 6)
res = "no";
}
}
return res;
}
int isLeapyear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
else
return 0;
}
int main()
{
int year, month, day;
string str;
int arr[2][12] = { {31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31} };//非瑞年二月为29天,非瑞年二月为28天
int sumdays = 0;//相隔的天数
int year0 = 2018, month0 = 8, day0 = 14;//14是周天
while (cin >> str)
{
stringstream ss;
string str1, str2, str3;
int pos1 = str.find(',');
int pos2 = str.rfind( ',');
str1 = str.substr(0, pos1);
str2 = str.substr(pos1 + 1, pos2 - pos1-1);
str3 = str.substr(pos2 + 1);
ss << str1;
ss >> year;
stringstream ss1;
ss1 << str2;
ss1 >> month;
stringstream ss2;
ss2 << str3;
ss2 >> day;
if (year == year0 && month == month0)
{
sumdays = day - day0;
cal(sumdays);
}
else
if (year == year0)
{
sumdays += 31 - day0 + day;
for (int i = month0 ; i < month-1; i++)
{
sumdays += arr[0][i];
}
cal(sumdays);
}
else
{
sumdays += arr[0][month0] - day0;
for (int i = month0; i < 12; i++)
sumdays += arr1[i];
for (int i = year0 + 1; i < year; i++)
{
if (isLeapyear(i))
{
sumdays += 366;
}
else
{
sumdays += 365;
}
}
for (int i = 0; i < month - 1; i++)
sumdays += arr[isLeapyear[year]][i];
sumdays += day;
cal(sumdays);
}
cout << res << endl;
}
}
第三题
暴力递归(进阶版,非进阶类似)
#include<iostream>
using namespace std;
int res ;
int m, n;
void dfs(int x, int y)
{
if (x > m || y > n)
return;
if (x == m && y == n)
res++;
if (x < m)
{
dfs(x + 1, y);
dfs(x + 2, y);
dfs(x + 3, y);
}
if (y < n)
{
dfs(x, y + 1);
dfs(x, y + 2);
dfs(x, y + 3);
}
}
int main()
{
while (cin >> m >> n)
{
res = 0;
dfs(0, 0);
cout << res << endl;
}
}
using namespace std;
int res ;
int m, n;
void dfs(int x, int y)
{
if (x > m || y > n)
return;
if (x == m && y == n)
res++;
if (x < m)
{
dfs(x + 1, y);
dfs(x + 2, y);
dfs(x + 3, y);
}
if (y < n)
{
dfs(x, y + 1);
dfs(x, y + 2);
dfs(x, y + 3);
}
}
int main()
{
while (cin >> m >> n)
{
res = 0;
dfs(0, 0);
cout << res << endl;
}
}
#4399游戏##笔试题目#