1.煤球数目
有一堆煤球,堆成三角棱锥形。具体:
第一层放1个,
第二层3个(排列成三角形),
第三层6个(排列成三角形),
第四层10个(排列成三角形),
....
如果一共有100层,共有多少个煤球?
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
int count=0;
int now=0;
for(int i=1;i<=100;i++){
now+=i;
count+=now;
}
cout<<count<<endl;
return 0;
}
//171700
//找规律,每一层比上一层增加的煤球数量为当前的层数
2.生日蜡烛
某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。
现在算起来,他一共吹熄了236根蜡烛。
请问,他从多少岁开始过生日party的?
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
int sum;
for(int i=1;;i++){
sum=0;
for(int j=i;sum<236;j++){
sum+=j;
}
if(sum==236){
printf("%d\n",i);
break;
}
}
}
//26
3.凑算式
这个算式中A~I代表1~9的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。
这个算式一共有多少种解法?
//法一:通分
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int judge(int a[]){
int n1=a[6]*100+a[7]*10+a[8];
int n2=a[3]*100+a[4]*10+a[5];
int n4=a[1]*n1;
int n5=a[2]*n2;
int n6=a[2]*n1;
if((n4+n5)%n6==0&&(n4+n5)/n6+a[0]==10){
return 1;
}
return 0;
}
int main(){
int count=0;
int a[9]={1,2,3,4,5,6,7,8,9};
do{
if(judge(a)){
count++;
}
}while(next_permutation(a,a+9));
cout<<count<<endl;
}
//29
//法二:两边同乘,使式子不出现分式
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int judge(int a[]){
int n1=a[6]*100+a[7]*10+a[8];
int n2=a[3]*100+a[4]*10+a[5];
if(a[0]*a[2]*n1+a[1]*n1+a[2]*n2==10*a[2]*n1){
return 1;
}
return 0;
}
int main(){
int count=0;
int a[9]={1,2,3,4,5,6,7,8,9};
do{
if(judge(a)){
count++;
}
}while(next_permutation(a,a+9));
cout<<count<<endl;
}
//29
4.快速排序
排序在各种场合经常被用到。
快速排序是十分常用的高效率的算法。
其思想是:先选一个“标尺”,
用它把整个队列过一遍筛子,
以保证:其左边的元素都不大于它,其右边的元素都不小于它。
这样,排序问题就被分割为两个子区间。
再分别对子区间排序就可以了。
下面的代码是一种实现,请分析并填写划线部分缺少的代码。
#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);
}
#########(11);//填空位置
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;
}
答案
5.抽签
X星球要派出一个5人组成的观察团前往W星。
其中:
A国最多可以派出4人。
B国最多可以派出2人。
C国最多可以派出2人。
....
那么最终派往W星的观察团会有多少种国别的不同组合呢?
下面的程序解决了这个问题。
数组a[] 中既是每个国家可以派出的最多的名额。
程序执行结果为:
DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
....
(以下省略,总共101行)
#include <stdio.h>
#define N 6
#define M 5
#define BUF 1024
void f(int a[], int k, int m, char b[]){
int i,j;
if(k==N){
b[M] = 0;
if(m==0) printf("%s\n",b);
return;
}
for(i=0; i<=a[k]; i++){
for(j=0; j<i; j++) b[M-m+j] = k+'A';
__________________; //填空位置
}
}
int main(){
int a[N] = {4,2,2,1,1,3};
char b[BUF];
f(a,0,M,b);
return 0;
}
答案
6.方格填数
如下的10个格子
填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?
/*
思路:
利用全排列函数填数,然后判断每个格子是否满足条件
亮点为一维数组位置和二维数组位置的转化
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int a[12];
int Next[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int judge(){
int x,y;
int x2,y2;
for(int i=1;i<=10;i++){
x=i/4;
y=i%4;
for(int j=0;j<8;j++){
x2=x+Next[j][0];
y2=y+Next[j][1];
int adj=x2*4+y2;
if(x2<0||x2>2||y2<0||y2>3||adj==0||adj==11){
continue;
}
if((int)abs(a[i]-a[adj])==1){
return 0;
}
}
}
return 1;
}
int main(){
int count=0;
for(int i=1;i<=10;i++){
a[i]=i-1;
}
do{
if(judge()){
count++;
}
}while(next_permutation(a+1,a+11));
cout<<count<<endl;
return 0;
}
//1580
7.剪邮票
如【图1.jpg】, 有12张连在一起的12生肖的邮票。
现在你要从中剪下5张来,要求必须是连着的。
(仅仅连接一个角不算相连)
比如,【图2.jpg】,【图3.jpg】中,粉红色所示部分就是合格的剪取。
请你计算,一共有多少种不同的剪取方法。
/*
原图编号为1~12,这样不方便一维数组和二维数组的转化,因此把原图看做0~11
先利用一维数组选取五个位置,然后标记在图中,搜索是否连通
*/
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
int a[5];//一维数组选位置
int map[3][4];//建图
int Next[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
struct node{
int x;
int y;
};
int judge(){
struct node start,temp;
int count,book[3][4];
memset(book,0,sizeof(book));
memset(map,0,sizeof(map));
for(int i=0;i<5;i++){//标记
int x=a[i]/4;
int y=a[i]%4;
map[x][y]=1;
}
start.x=a[0]/4;
start.y=a[0]%4;
book[start.x][start.y]=1;
count=1;
queue<struct node> q;
q.push(start);
while(!q.empty()){//搜索
struct node head=q.front();
q.pop();
for(int i=0;i<4;i++){
int xt=head.x+Next[i][0];
int yt=head.y+Next[i][1];
if(xt>=0&&xt<=2&&yt>=0&&yt<=3&&map[xt][yt]&&!book[xt][yt]){
count++;
temp.x=xt;
temp.y=yt;
q.push(temp);
book[xt][yt]=1;
}
}
}
if(count==5)
return 1;
else
return 0;
}
int main(){
int count=0;
for(a[0]=0;a[0]<=7;a[0]++){
for(a[1]=a[0]+1;a[1]<=8;a[1]++){
for(a[2]=a[1]+1;a[2]<=9;a[2]++){
for(a[3]=a[2]+1;a[3]<=10;a[3]++){
for(a[4]=a[3]+1;a[4]<=11;a[4]++){
if(judge()){
count++;
}
}
}
}
}
}
cout<<count<<endl;
}
//116
8.四平方和
四平方和定理,又称为拉格朗日定理:
每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。
比如:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^符号表示乘方的意思)
对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法
程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开
例如,输入:
5
则程序应该输出:
0 0 1 2
再例如,输入:
12
则程序应该输出:
0 2 2 2
再例如,输入:
773535
则程序应该输出:
1 1 267 838
//法一:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int book[5000005];
int main(){
int n,top,flag=1;
cin>>n;
top=(int)sqrt(n);
memset(book,0,sizeof(book));
for(int i=0;i<=top;i++){
for(int j=i;j<=top;j++){
if(i*i+j*j>n){
break;
}
book[i*i+j*j]=1;
}
}
for(int i=0;i<=top&&flag;i++){
for(int j=i;j<=top&&flag;j++){
if(i*i+j*j>n){
break;
}
if(book[n-i*i-j*j]){
for(int t=j;t<=top&&flag;t++){
int z=n-i*i-j*j-t*t;
double y=sqrt(z);
if(abs(y-floor(y))<=1e-6){
printf("%d %d %d %d\n",i,j,t,(int)sqrt(z));
flag=0;
}
}
}
}
}
return 0;
}
//法二:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int main(){
int n,top,flag=1;
cin>>n;
top=(int)sqrt(n);
for(int i=0;i<=top&&flag;i++){
for(int j=i;j<=top&&flag;j++){
if(i*i+j*j>n){
break;
}
for(int t=j;t<=top&&flag;t++){
if(i*i+j*j+t*t>n){
break;
}
int z=n-i*i-j*j-t*t;
if(fabs(sqrt(z)-floor(sqrt(z)))>1e-6){
continue;
}
printf("%d %d %d %d\n",i,j,t,(int)sqrt(z));
flag=0;
}
}
}
return 0;
}
9.交换瓶子
有N个瓶子,编号 1 ~ N,放在架子上。
比如有5个瓶子:
2 1 3 5 4
要求每次拿起2个瓶子,交换它们的位置。
经过若干次后,使得瓶子的序号为:
1 2 3 4 5
对于这么简单的情况,显然,至少需要交换2次就可以复位。
如果瓶子更多呢?你可以通过编程来解决。
输入格式为两行:
第一行: 一个正整数N(N<10000), 表示瓶子的数目
第二行:N个正整数,用空格分开,表示瓶子目前的排列情况。
输出数据为一行一个正整数,表示至少交换多少次,才能完成排序。
例如,输入:
5
3 1 2 5 4
程序应该输出:
3
再例如,输入:
5
5 4 3 2 1
程序应该输出:
2
/*
贪心算法
记录每个瓶子的位置
从第一个位置往后,如果该位置的瓶子不对,则把该有的瓶子换过来
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
int n,t,count=0;
int a[10005];
int loc[10005];
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
loc[a[i]]=i;
}
for(int i=1;i<=n;i++){
if(i!=a[i]){
count++;
t=a[i];
swap(a[i],a[loc[i]]);
loc[t]=loc[i];
loc[a[i]]=i;
}
}
cout<<count<<endl;
return 0;
}
10.最大比例
X星球的某个大奖赛设了M级奖励。每个级别的奖金是一个正整数。
并且,相邻的两个级别间的比例是个固定值。
也就是说:所有级别的奖金数构成了一个等比数列。比如:
16,24,36,54
其等比值为:3/2
现在,我们随机调查了一些获奖者的奖金数。
请你据此推算可能的最大的等比值。
输入格式:
第一行为数字 N (0<N<100),表示接下的一行包含N个正整数
第二行N个正整数Xi(Xi<1 000 000 000 000),用空格分开。每个整数表示调查到的某人的奖金数额
要求输出:
一个形如A/B的分数,要求A、B互质。表示可能的最大比例系数
测试数据保证了输入格式正确,并且最大比例是存在的。
例如,输入:
3
1250 200 32
程序应该输出:
25/4
再例如,输入:
4
3125 32 32 200
程序应该输出:
5/2
再例如,输入:
3
549755813888 524288 2
程序应该输出:
4/1
//此题没有什么意义,简单的说一下自己的思路(正确性待定):
对于所有的数据,去重,由小到大排序,算出每一项和前项的比,化简为互质的形式,所有的比去重,排序。
若所有比相同,则即为结果。
若不相同,再算比之间的比,取最小的一个。