-
啤酒和饮料
啤酒每罐2.3元,饮料每罐1.9元。小明买了若干啤酒和饮料,一共花了82.3元。
我们还知道他买的啤酒比饮料的数量少,请你计算他买了几罐啤酒。
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
double a = 2.3, b = 1.9;
int main(){
for(int i = 0; i <= 100; i++){
for(int j = 0; j <= 100; j++){
if(i < j && i * a + b * j == 82.3){
a = i;
b = j;
}
}
}
cout << "啤酒: "<< a << " 饮料: " << b << endl;
return 0;
}
// 啤酒: 11 饮料: 30
-
切面条
一根高筋拉面,中间切一刀,可以得到2根面条。
如果先对折1次,中间切一刀,可以得到3根面条。
如果连续对折2次,中间切一刀,可以得到5根面条。
那么,连续对折10次,中间切一刀,会得到多少面条呢?
答案是个整数,请通过浏览器提交答案。不要填写任何多余的内容。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int a = 2;
int main(){
for(int i = 1; i <= 10; i++){
a = a * 2 - 1;
cout << "对折"<< i << "次, 可以得到 " << a << "根面条" << endl;
}
cout << a << endl;
return 0;
}
/* 结果:
对折1次, 可以得到 3根面条
对折2次, 可以得到 5根面条
对折3次, 可以得到 9根面条
对折4次, 可以得到 17根面条
对折5次, 可以得到 33根面条
对折6次, 可以得到 65根面条
对折7次, 可以得到 129根面条
对折8次, 可以得到 257根面条
对折9次, 可以得到 513根面条
对折10次, 可以得到 1025根面条
*/
-
李白打酒
扫描二维码关注公众号,回复: 9063371 查看本文章话说大诗人李白,一生好饮。幸好他从不开车。
一天,他提着酒壶,从家里出来,酒壶中有酒2斗。他边走边唱:
无事街上走,提壶去打酒。
逢店加一倍,遇花喝一斗。这一路上,他一共遇到店5次,遇到花10次,已知最后一次遇到的是花,他正好把酒喝光了。
请你计算李白遇到店和花的次序,可以把遇店记为a,遇花记为b。则:babaabbabbabbbb 就是合理的次序。像这样的答案一共有多少呢?请你计算出所有可能方案的个数(包含题目给出的)。
注意:通过浏览器提交答案。答案是个整数。不要书写任何多余的内容。
思路
限定最后一次是花,直接暴力搜索
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int ans;
void dfs(int a, int b, int c){
if(a == 0 && b == 0 && c == 1){
ans++;
}
if(a < 0 || b < 0 || c < 0) return ;
dfs(a - 1, b, c * 2);
dfs(a, b - 1, c - 1);
}
int main(){
dfs(5, 9, 2);
cout << ans << endl;
return 0;
}
// 14
-
史丰收速算
史丰收速算法的革命性贡献是:从高位算起,预测进位。不需要九九表,彻底颠覆了传统手算!
速算的核心基础是:1位数乘以多位数的乘法。
其中,乘以7是最复杂的,就以它为例。
因为,1/7 是个循环小数:0.142857…,如果多位数超过 142857…,就要进1
同理,2/7, 3/7, … 6/7 也都是类似的循环小数,多位数超过 n/7,就要进n
下面的程序模拟了史丰收速算法中乘以7的运算过程。
乘以 7 的个位规律是:偶数乘以2,奇数乘以2再加5,都只取个位。
乘以 7 的进位规律是:
满 142857… 进1,
满 285714… 进2,
满 428571… 进3,
满 571428… 进4,
满 714285… 进5,
满 857142… 进6请分析程序流程,填写划线部分缺少的代码。
//计算个位
int ge_wei(int a)
{
if(a % 2 == 0)
return (a * 2) % 10;
else
return (a * 2 + 5) % 10;
}
//计算进位
int jin_wei(char* p)
{
char* level[] = {
"142857",
"285714",
"428571",
"571428",
"714285",
"857142"
};
char buf[7];
buf[6] = '\0';
strncpy(buf,p,6);
int i;
for(i=5; i>=0; i--){
int r = strcmp(level[i], buf);
if(r<0) return i+1;
while(r==0){
p += 6;
strncpy(buf,p,6);
r = strcmp(level[i], buf);
if(r<0) return i+1;
______________________________; //填空
}
}
return 0;
}
//多位数乘以7
void f(char* s)
{
int head = jin_wei(s);
if(head > 0) printf("%d", head);
char* p = s;
while(*p){
int a = (*p-'0');
int x = (ge_wei(a) + jin_wei(p+1)) % 10;
printf("%d",x);
p++;
}
printf("\n");
}
int main()
{
f("428571428571");
f("34553834937543");
return 0;
}
注意:通过浏览器提交答案。只填写缺少的内容,不要填写任何多余的内容(例如:说明性文字)
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
//计算个位
int ge_wei(int a)
{
if(a % 2 == 0)
return (a * 2) % 10;
else
return (a * 2 + 5) % 10;
}
//计算进位
int jin_wei(char* p)
{
char* level[] = {
"142857",
"285714",
"428571",
"571428",
"714285",
"857142"
};
char buf[7];
buf[6] = '\0';
strncpy(buf,p,6);
int i;
for(i=5; i>=0; i--){
int r = strcmp(level[i], buf);
if(r<0) return i+1;
while(r==0){
p += 6;
strncpy(buf,p,6);
r = strcmp(level[i], buf);
if(r<0) return i+1;
if(r > 0) return i;
}
}
return 0;
}
//多位数乘以7
void f(char* s)
{
int head = jin_wei(s);
if(head > 0) printf("%d", head);
char* p = s;
while(*p){
int a = (*p-'0');
int x = (ge_wei(a) + jin_wei(p+1)) % 10;
printf("%d",x);
p++;
}
printf("\n");
}
int main()
{
f("428571428571");
f("34553834937543");
return 0;
}
-
打印图形
小明在X星球的城堡中发现了如下图形和文字:
rank=3
*
* *
* *
* * * *
rank=5
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
ran=6
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
小明开动脑筋,编写了如下的程序,实现该图形的打印。
#define N 70
void f(char a[][N], int rank, int row, int col)
{
if(rank==1){
a[row][col] = '*';
return;
}
int w = 1;
int i;
for(i=0; i<rank-1; i++) w *= 2;
____________________________________________;
f(a, rank-1, row+w/2, col);
f(a, rank-1, row+w/2, col+w);
}
int main()
{
char a[N][N];
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++) a[i][j] = ' ';
f(a,6,0,0);
for(i=0; i<N; i++){
for(j=0; j<N; j++) printf("%c",a[i][j]);
printf("\n");
}
return 0;
}
请仔细分析程序逻辑,填写缺失代码部分。
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
//计算个位
#define N 70
void f(char a[][N], int rank, int row, int col)
{
if(rank==1){
a[row][col] = '*';
return;
}
int w = 1;
int i;
for(i=0; i<rank-1; i++) w *= 2;
f(a, rank - 1, row, col + w /2);//处理上面部分
f(a, rank-1, row+w/2, col);//处理左下部分
f(a, rank-1, row+w/2, col+w);//处理右下部分
}
int main()
{
char a[N][N];
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++) a[i][j] = ' ';
f(a,6,0,0);
for(i=0; i<N; i++){
for(j=0; j<N; j++) printf("%c",a[i][j]);
printf("\n");
}
return 0;
}
-
奇怪的分式
上小学的时候,小明经常自己发明新算法。一次,老师出的题目是:
1/4 乘以 8/5
小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (参见图1.png)
老师刚想批评他,转念一想,这个答案凑巧也对啊,真是见鬼!
对于分子、分母都是 1~9 中的一位数的情况,还有哪些算式可以这样计算呢?
请写出所有不同算式的个数(包括题中举例的)。
显然,交换分子分母后,例如:4/1 乘以 5/8 是满足要求的,这算做不同的算式。
但对于分子分母相同的情况,2/2 乘以 3/3 这样的类型太多了,不在计数之列!
注意:答案是个整数(考虑对称性,肯定是偶数)。请通过浏览器提交。不要书写多余的内容
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int ans;
int main()
{
for(int a = 1; a <= 9; a += 1)
for(int b= 1; b <= 9; b += 1)
for(int c = 1; c <= 9; c += 1)
for(int d = 1; d <= 9; d += 1){
if(a == b && c == d) continue;
// if((double)(a * c) / (b * d) == (double)(a * 10 + c) / (b * 10 + d)) ans++, printf("%d %d %d %d\n", a, b, c, d);
if((a * c) * (b * 10 + d) == (b * d) * (a * 10 + c)) ans++, printf("%d %d %d %d\n", a, b, c, d);
}
cout << ans << endl;
return 0;
}
// 12
-
六角填数
如图所示六角形中,填入1~12的数字。
使得每条直线上的数字之和都相同。
图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?
请通过浏览器提交答案,不要填写多余的内容。
代码
方法一:DFS暴搜
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int a[6][4] = {{1,3,6,8},{1,4,7,11},{8,9,10,11},{2,3,4,5},{2,6,9,12},{5,7,10,12}};
bool vis[13];
int w[13];
int ans[6];
int sum;
void dfs(int x, int n){
if(n == 12){
for(int i = 0; i <= 5; i++){
sum = 0;
for(int j = 0; j < 4; j++){
sum += w[a[i][j]];
}
ans[i] = sum;
}
sum = ans[0];
for(int i = 1; i <= 5; i++){
if(ans[i] != sum) return;
}
cout << w[6] << endl;
}
for(int i = 1; i <= 12; i++){
if(vis[i]) continue;
vis[i] = true;
w[x] = i;
dfs(x + 1, n + 1);
vis[i] = false;
w[x] = 0;
}
}
int main()
{
w[1] = 1, vis[1] = true;
w[2] = 8, vis[8] = true;
w[12] = 3, vis[3] = true;
dfs(3, 3);
return 0;
}
方法2:全排列, 本质是一样的,只是全排列感觉要好写一些
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int a[6][4] = {{1,3,6,8},{1,4,7,11},{8,9,10,11},{2,3,4,5},{2,6,9,12},{5,7,10,12}};
int ans[6];
int sum;
int b[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int main()
{
do{
if(b[1] == 1 && b[2] == 8 && b[12] == 3){
for(int i = 0; i <= 5; i++){
sum = 0;
for(int j = 0; j < 4; j++){
sum += b[a[i][j]];
}
ans[i] = sum;
}
sum = ans[0];
bool flag = 1;
for(int i = 1; i <= 5; i++){
if(ans[i] != sum) flag = 0;
}
if(flag){
cout << b[6] << endl;
for(int i = 1; i <= 12; i++)
cout << b[i] << " ";
}
}
}while(next_permutation(b + 1, b + 13));
return 0;
}
-
蚂蚁感冒
长100厘米的细长直杆子上有n只蚂蚁。它们的头有的朝左,有的朝右。
每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒。
当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行。
这些蚂蚁中,有1只蚂蚁感冒了。并且在和其它蚂蚁碰面时,会把感冒传染给碰到的蚂蚁。
请你计算,当所有蚂蚁都爬离杆子时,有多少只蚂蚁患上了感冒。
【数据格式】
第一行输入一个整数n (1 < n < 50), 表示蚂蚁的总数。
接着的一行是n个用空格分开的整数 Xi (-100 < Xi < 100), Xi的绝对值,表示蚂蚁离开杆子左边端点的距离。正值表示头朝右,负值表示头朝左,数据中不会出现0值,也不会出现两只蚂蚁占用同一位置。其中,第一个数据代表的蚂蚁感冒了。
要求输出1个整数,表示最后感冒蚂蚁的数目。
例如,输入:
3
5 -2 8
程序应输出:
1
再例如,输入:
5
-10 8 -20 12 25
程序应输出:
3
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
代码
问题模型来着《挑战程序设计竞赛》里面一道蚂蚁的题
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int ans = 1, n, a[55];
int main()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int x = a[1];
if(x > 0){
for(int i = 2; i <= n; i++){
if(a[i] < 0 && -a[i] > x) ans++;
}
if(ans > 1){
for(int i = 2; i <= n; i++){
if(a[i] > 0 && a[i] < x) ans++;
}
}
}
if(x < 0){
for(int i = 2; i <= n; i++){
if(a[i] > 0 && a[i] < -x) ans++;
}
if(ans > 1){
for(int i = 2; i <= n; i++){
if(a[i] < 0 && a[i] < x) ans++;
}
}
}
cout << ans << endl;
return 0;
}
-
地宫取宝
X 国王有一个地宫宝库。是 n x m 个格子的矩阵。每个格子放一件宝贝。每个宝贝贴着价值标签。
地宫的入口在左上角,出口在右下角。
小明被带到地宫的入口,国王要求他只能向右或向下行走。
走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿)。
当小明走到出口时,如果他手中的宝贝恰好是k件,则这些宝贝就可以送给小明。
请你帮小明算一算,在给定的局面下,他有多少种不同的行动方案能获得这k件宝贝。
【数据格式】
输入一行3个整数,用空格分开:n m k (1<=n,m<=50, 1<=k<=12)
接下来有 n 行数据,每行有 m 个整数 Ci (0<=Ci<=12)代表这个格子上的宝物的价值
要求输出一个整数,表示正好取k个宝贝的行动方案数。该数字可能很大,输出它对 1000000007 取模的结果。
例如,输入:
2 2 2
1 2
2 1
程序应该输出:
2
再例如,输入:
2 3 2
1 2 3
2 1 5
程序应该输出:
14
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
思路
普通的DFS + 记忆性递归优化
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
int n, m, k, mem[55][55][15][15];
int map[55][55];
ll dfs(int x, int y, int Max, int cnt){
if(mem[x][y][Max + 1][cnt] != -1) return mem[x][y][Max + 1][cnt];
if(x > n || y > m || cnt > k) return 0;
ll ans = 0;
int val = map[x][y];
if(x == n && y == m && (cnt == k || (cnt == k - 1 && val > Max))){
ans++;
return ans % mod;
}
if(val > Max){
ans += dfs(x + 1, y, val, cnt + 1);
ans += dfs(x, y + 1, val, cnt + 1);
ans %= mod;
}
ans += dfs(x + 1, y, Max, cnt);
ans += dfs(x, y + 1, Max, cnt);
ans %= mod;
return mem[x][y][Max + 1][cnt] = ans;
}
int main(){
memset(mem, -1, sizeof mem);
cin >> n >> m >> k;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> map[i][j];
printf("%lld\n", dfs(1, 1, -1, 0));
return 0;
}
- 小朋友排队
n 个小朋友站成一排。现在要把他们按身高从低到高的顺序排列,但是每次只能交换位置相邻的两个小朋友。
每个小朋友都有一个不高兴的程度。开始的时候,所有小朋友的不高兴程度都是0。
如果某个小朋友第一次被要求交换,则他的不高兴程度增加1,如果第二次要求他交换,则他的不高兴程度增加2(即不高兴程度为3),依次类推。当要求某个小朋友第k次交换时,他的不高兴程度增加k。
请问,要让所有小朋友按从低到高排队,他们的不高兴程度之和最小是多少。
如果有两个小朋友身高一样,则他们谁站在谁前面是没有关系的。
【数据格式】
输入的第一行包含一个整数n,表示小朋友的个数。
第二行包含 n 个整数 H1 H2 … Hn,分别表示每个小朋友的身高。
输出一行,包含一个整数,表示小朋友的不高兴程度和的最小值。
例如,输入:
3
3 2 1
程序应该输出:
9
【样例说明】
首先交换身高为3和2的小朋友,再交换身高为3和1的小朋友,再交换身高为2和1的小朋友,
每个小朋友的不高兴程度都是3,总和为9。
【数据规模与约定】
对于10%的数据, 1<=n<=10;
对于30%的数据, 1<=n<=1000;
对于50%的数据, 1<=n<=10000;
对于100%的数据,1<=n<=100000,0<=Hi<=1000000。
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
思路
树状数组求逆序对
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N = 100005;
int c[N * 10], n, a[N], maxh;
ll cnt[N];
int lowbit(int x){
return x & -x;
}
void update(int n, int idx, int val){
for(int i = idx; i <= n; i += lowbit(i))
c[i] += val;
}
int sum(int n, int idx){
int sum = 0;
for(int i = idx; i > 0; i -= lowbit(i)){
sum += c[i];
}
return sum;
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", a + i);
maxh = max(maxh, a[i] + 1);
}
for(int i = n; i >= 1; i--){
cnt[i] += sum(maxh + 1, a[i] + 1);//右边比他小的数
int t = sum(maxh + 1, a[i] + 1) - sum(maxh + 1, a[i]);
if(t > 0) cnt[i] -= t; //相同的数不做计数
update(maxh + 1, a[i] + 1, 1);
}
memset(c, 0, sizeof c);
for(int i = 1; i <= n; i++){
cnt[i] += i - sum(maxh + 1, a[i] + 1) - 1;//左边比他大的数的个数
update(maxh, a[i] + 1, 1);
}
ll ans = 0;
for(int i = 1; i <= n; i++){
ans += (cnt[i] + 1) * cnt[i] / 2;
}
cout << ans << endl;
return 0;
}