转载自:https://wenku.baidu.com/view/6c203818f01dc281e43af046.html
第一题
代码:
#include <iostream>
#include <string>#include <cstring>
#include <algorithm>
using namespace std;
int days[3][43] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
void check(int &yy, int &mm, int &cnt)
{
if(mm > 12)
{
yy++;
if((yy % 4 == 0 && yy % 100 == 0) || (yy % 400 == 0))
{
cnt = 1;
}
else cnt = 0;
mm = 1;
}
}
int main()
{
int yy = 1777, mm = 4, dd = 30;
int add = 8113, cnt = 0;
//while(cin >> yy >> mm >> dd >> add)
{
add -= (days[cnt][mm] - dd + 1);
mm++;
while(add >= days[cnt][mm])
{
if(yy % 4)
if(cnt == 1) cout << "I***"<< endl;
add -= days[cnt][mm];
mm++;
if(mm > 12)
{
yy++;
if((yy % 4 == 0 && yy % 100 != 0) || (yy % 400 == 0))
{
cnt = 1;
}
else cnt = 0;
mm = 1;
}
}
cout << yy << "-" << mm << "-" << add << endl;
}
return 0;
}
答案:1799-7-16
第二题:
用到了next_permutation函数
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num[10] = {};
bool book[10] = {};
bool dfs(long long nn)
{
long long mm =nn * nn;
int one;
while(mm)
{
one = mm % 10;
if(book[one]) return false;
mm /= 10;
}
return true;
}
int main()
{
for(int i = 0; i <= 9; i++) num[i] = i;
do{
memset(book, false, sizeof(book));
long long ten = 1, add = 0;
for(int i = 0; i <= 5; i++)
{
book[num[i]] = true;
add += ten * num[i];
ten *= 10;
}
if(dfs(add)) cout << add << endl;
}while(next_permutation(num, num + 10));
return 0;
}
答案:639172
第三题
代码:
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int num[34][34] = {}, cnt = 0, rec[10] = {};
bool book[34][34] = {};
int nextx[4] = {0, 1};
int nexty[4] = {1, 0};
bool check()
{
for(int i = 1; i <= 8; i++)
{
if(rec[i] != i) return false;
}
return true;
}
void dfs(int stx, int sty, int step)
{
//cout << stx << " " << sty << " " << step << endl;
if(stx == 4 && sty == 5 && check())
cnt++;
for(int i = 0; i < 2; i++)
{
int xx = stx + nextx[i];
int yy = sty + nexty[i];
if(xx < 1 || xx > 4 || yy < 1 || yy > 5) continue;
if(!book[xx][yy])
{
rec[step] = num[xx][yy];
book[xx][yy] = true;
dfs(xx, yy, step + 1);
book[xx][yy] = false;
rec[step] = -1;
}
}
}
int main()
{
memset(rec, -1, sizeof(rec));
int nn = 0;
for(int i = 1; i <= 4; i++)
{
nn++;
for(int j = 1, kk = nn; j <= 5; j++, kk++)
{
num[i][j] = kk;
//cout << kk << " ";
}
//cout << endl;
}
book[1][1] = true;
rec[1] = 1;
dfs(1, 1, 2);
cout << cnt << endl;
return 0;
}
答案:35
第四题
代码:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int old[7] = {0, 1, 2, 5, 6, 8, 9}, rec[10] = {};
int book[10] = {}, num[5] = {}, cnt = 0;
bool vis[10] = {};
struct node
{
int aa[5];
node(int cc[])
{
for(int i = 0; i < 4; i++)
{
aa[i] = cc[i];
}
}
node() {}
};
vector<node>vec;
void check(int num1[], int num2[])
{
int rec[4];
memset(rec, 0, sizeof(rec));
int ten = 1;
for(int j = 3; j >= 0; j--)
{
rec[0] += num1[j] * ten;
rec[1] += book[num1[3 - j]] * ten;
rec[2] += num2[j] * ten;
rec[3] += book[num2[3 - j]] * ten;
ten *= 10;
}
//cout << rec[0] << " " << rec[1] << " " << rec[2] << " " << rec[3] << endl;
int cc = rec[3] - rec[2] + rec[1] - rec[0];
int a1 = rec[3] - rec[2];
int a2 = rec[1] - rec[0];
if(cc == 558 && a1 > 800 && a1 < 900 && a2 < -200 && a1 > -300)
{
cout << rec[0] << endl;
//system("pause");
}
}
void dfs(int step)
{
if(step == 4)
{
vec.push_back(num);
return;
}
for(int i = 0; i < 7; i++)
{
if((step == 0 || step == 3) && old[i] == 0) continue;
num[step] = old[i];
dfs(step + 1);
num[step] = -1;
}
}
int main()
{
book[0] = 0;
book[1] = 1;
book[2] = 2;
book[5] = 5;
book[6] = 9;
book[8] = 8;
book[9] = 6;
dfs(0);
for(int i = 0; i < vec.size(); i++)
{
for(int j = i + 1; j < vec.size(); j++)
{
check(vec[i].aa, vec[j].aa);
check(vec[j].aa, vec[i].aa);
}
}
return 0;
}
答案:9088
第五题
填空:*(haystack++) != *(needle++)
第六题
乱猜的,,,
填空:evaluate(x + v1.n + 1)
第七题
代码:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int vec[234] = {};
int main()
{
int n, c1, c2;
while(cin >> n)
{
memset(vec, 0, sizeof(vec));
int m, cnt = 0;
char ch;
for(int i = 0; i < n; i++)
{
while(true)
{
scanf("%d%c", &m, &ch);
vec[cnt++] = m;
if(ch != ' ') break;
}
}
sort(vec, vec + cnt);
int ss = vec[1] - vec[0], st = 2;
if(vec[1] == vec[0])
{
c2 = vec[1];
ss = vec[2] - vec[1];
st = 3;
}
for(int i = st; i < cnt; i++)
{
int cc = vec[i] - vec[i - 1];
//cout << vec[i] << " " << vec[i - 1] << " " << cc << " " << ss << endl;
if(vec[i] == vec[i - 1])
{
c2 = vec[i];
continue;
}
if(cc > ss)
{
c1 = vec[i - 1] + ss;
continue;
}
if(ss > cc)
{
c1 = vec[0] + cc;
continue;
}
}
cout << c1 << " " << c2 << endl;
}
return 0;
}
第八题
不会。。。
本文提供了八道编程题目的解答代码及思路,涉及日期计算、排列组合验证、路径搜索算法等,通过具体实例展示了多种算法的应用场景和技术要点。
637

被折叠的 条评论
为什么被折叠?



