网友年龄
某君新认识一网友。
当问及年龄时,他的网友说:
“我的年龄是个2位数,我比儿子大27岁,
如果把我的年龄的两位数字交换位置,刚好就是我儿子的年龄”请你计算:网友的年龄一共有多少种可能情况?
提示:30岁就是其中一种可能哦.
请填写表示可能情况的种数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
思路:
挺好分析的,因为网友比儿子大27岁,所以直接从27开始遍历(<100岁)在利用年龄关系式
\[(Dad年龄)ab -27 == ba ? count++: count+=0 \]
#include<bits/stdc++.h>
using namespace std;
int main() {
int count = 0;
for (int Dad = 27; Dad < 100; ++Dad) {
int Son = Dad - 27;
int t = Dad % 10 * 10 + Dad / 10;
if (t == Son)count++;
}
cout << count << endl;
return 0;
}
生日蜡烛
某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。
现在算起来,他一共吹熄了236根蜡烛。
请问,他从多少岁开始过生日party的?
请填写他开始过生日party的年龄数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
思路:直接枚举开始过生日的那一岁即可
#include<bits/stdc++.h>
using namespace std;
int main() {
for (int i = 0; i < 100; ++i) {
int sum = 0;
for (int j = i; j < 100; ++j) {
sum += j;
if (sum > 236)break;
if (sum == 236)cout << i << "->" << j << endl;
}
}
return 0;
}
方格填数
填入0~9的数字。要求:连续的两个数字不能相邻。 (左右、上下、对角都算相邻)
一共有多少种可能的填数方案?
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
#include<bits/stdc++.h>
using namespace std;
bool nums[10];//0-9用过就标true
const int dx[] = { 0,-1,-1,-1 };
const int dy[] = { -1,-1,0,1 };
int count_, a[5][5];
bool check(int n, int x, int y) {
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || yy < 1 || xx>3 || yy>4) continue;
if (abs(n - a[xx][yy]) == 1) return false;
}
return true;
}
void dfs(int x, int y) {
if (x == 3 && y == 4) {
count_++; return;
}
for (int i = 0; i <= 9; i++) {
if (!nums[i] && check(i, x, y)) {
a[x][y] = i;
nums[i] = true;
if (y == 4) dfs(x + 1, 1);
else dfs(x, y + 1);
nums[i] = false;//还原
a[x][y] = -1e9;
}
}
}
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4; j++) {
a[i][j] = -1e9;
}
}
dfs(1,2);
cout << count_ << endl;
return 0;
}
快速排序
排序在各种场合经常被用到。 快速排序是十分常用的高效率的算法。
其思想是:先选一个“标尺”, 用它把整个队列过一遍筛子,
以保证:其左边的元素都不大于它,其右边的元素都不小于它。
这样,排序问题就被分割为两个子区间。 再分别对子区间排序就可以了。
下面的代码是一种实现,请分析并填写划线部分缺少的代码。
#include <stdio.h>
void swap(int a[], int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
int partition(int a[], int p, int r)
{
int i = p;
int j = r + 1;
int x = a[p];
while(1){
while(i<r && a[++i]<x);
while(a[--j]>x);
if(i>=j) break;
swap(a,i,j);
}
//答案:swap(a,p,j)
______________________;
return j;
}
void quicksort(int a[], int p, int r)
{
if(p<r){
int q = partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
int main()
{
int i;
int a[] = {5,13,6,24,2,8,19,27,6,12,1,17};
int N = 12;
quicksort(a, 0, N-1);
for(i=0; i<N; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
消除尾一
下面的代码把一个整数的二进制表示的最右边的连续的1全部变成0 如果最后一位是0,则原数字保持不变。 如果采用代码中的测试数据,应该输出:
00000000000000000000000001100111 ->00000000000000000000000001100000
00000000000000000000000000001100 ->00000000000000000000000000001100
#include <stdio.h>
void f(int x)
{
int i;
for(i=0; i<32; i++) printf("%d", (x>>(31-i))&1);
printf(" ");
//答案: x&(x+1)
x = _______________________;
for(i=0; i<32; i++) printf("%d", (x>>(31-i))&1);
printf("\n");
}
int main()
{
f(103);
f(12);
return 0;
}
感觉上这道题需要去理解二进制的原码,补码和反码问题和位运算符的使用。在看dalao解法的时候还了解到 lowbit 这个知识点(涨知识了)
寒假作业
现在小学的数学题目也不是那么好玩的。
看看这个寒假作业:
□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □
每个方块代表1~13中的某一个数字,但不能重复。
比如:
6 + 7 = 13
9 - 8 = 1
3 * 4 = 12
10 / 2 = 5
以及:
7 + 6 = 13
9 - 8 = 1
3 * 4 = 12
10 / 2 = 5
就算两种解法。(加法,乘法交换律后算不同的方案)
你一共找到了多少种方案?
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
思路:1. dfs搜索下,注意等式判断问题
2. 1-13全排列,取前面12个数组合
/*
dfs搜索
*/
#include<bits/stdc++.h>
using namespace std;
bool book[15];
int a[15];
int ans = 0;
void dfs(int dep)
{
if (dep == 13) {
//必须整除,变成乘法判断
if (a[10] == a[11] * a[12]) ans++;
return;
}
if (dep == 10) {
if (a[7] * a[8] != a[9]) return;
}
if (dep == 7) {
if (a[4] - a[5] != a[6]) return;
}
if (dep == 4) {
if (a[1] + a[2] != a[3]) return;
}
for (int i = 1; i <= 13; i++) {
if (!book[i]) {
book[i] = true;
a[dep] = i;
dfs(dep + 1);
a[dep] = -1;
book[i] = false;
}
}
}
int main() {
dfs(1);
cout << ans << endl;
return 0;
}
/*
全排列
*/
#include<bits/stdc++.h>
using namespace std;
int a[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13 };
bool check()
{
bool b1 = (a[0] + a[1] == a[2]);
bool b2 = (a[3] - a[4] == a[5]);
bool b3 = (a[6] * a[7] == a[8]);
bool b4 = (fabs((a[9] * 1.0) / (a[10] * 1.0) - a[11] * 1.0) <= 0.00000000000001);
if (b1 && b2 &&b3 && b4)
return true;
else
return false;
}
int main()
{
int res = 0;
do
{
if (check())
{
res++;
}
} while (next_permutation(a, a + 13));
cout << res << endl;
return 0;
}
//PS.实在太暴力,效率很低
剩下4道后续补上
来源:https://www.cnblogs.com/Kanna/p/12335074.html