1.奖券数目
有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。
虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。
请提交该数字(一个整数),不要写任何多余的内容或说明性文字。
水题
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool check( int num ) {
while(num) {
if( num % 10 == 4 ) return 0;
num /= 10;
}
return 1;
}
int main() {
for( int i = 10000; i <= 99999; i++ )
if( check(i) ) ans++;
cout << ans << endl;
return 0;
} //ans = 52488
/*****************************************************************************/
2.星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19
请严格按照格式书写。不能出现其它文字或符号。
Excel算一下
ans = 2017-08-05
/*****************************************************************************/
3.三羊献瑞
观察下面的加法算式:
祥 瑞 生 辉
+ 三 羊 献 瑞
-------------------
三 羊 生 瑞 气
(如果有对齐问题,可以参看【图1.jpg】)
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
枚举一手,怎么说
#include <map>
#include <set>
#include <queue>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int num[10];
bool vis[10];
void dfs( int step ) {
if( step == 8 ) {
if( !num[4] ) return;
int num1 = num[0]*1000+num[1]*100+num[2]*10+num[3];
int num2 = num[4]*1000+num[5]*100+num[6]*10+num[1];
int num3 = num[4]*10000+num[5]*1000+num[2]*100+num[1]*10+num[7];
if( num1+num2==num3 ) {
cout << num[0] << num[1] << num[2] << num[3];
cout << "+";
cout << num[4] << num[5] << num[6] << num[1] << "=";
cout << num[4] << num[5] << num[2] << num[1] << num[7];
cout << endl;
}
return;
}
for( int i = 0; i <= 9; i++ ) {
if( vis[i] ) continue;
vis[i] = 1;
num[step] = i;
dfs(step+1);
vis[i] = 0;
}
}
int main() {
dfs(0);
return 0;
}
//运行结果9567+1085=10652
//ans = 1085
/*****************************************************************************/
4.格子中输出
StringInGrid函数会在一个指定大小的格子中打印指定的字符串。
要求字符串在水平、垂直两个方向上都居中。
如果字符串太长,就截断。
如果不能恰好居中,可以稍稍偏左或者偏上一点。
下面的程序实现这个逻辑,请填写划线部分缺少的代码。
#include <stdio.h>
#include <string.h>
void StringInGrid(int width, int height, const char* s)
{
int i,k;
char buf[1000];
strcpy(buf, s);
if(strlen(s)>width-2) buf[width-2]=0;
printf("+");
for(i=0;i<width-2;i++) printf("-");
printf("+\n");
for(k=1; k<(height-1)/2;k++){
printf("|");
for(i=0;i<width-2;i++) printf(" ");
printf("|\n");
}
printf("|");
printf("%*s%s%*s",_____________________________________________); //填空
printf("|\n");
for(k=(height-1)/2+1; k<height-1; k++){
printf("|");
for(i=0;i<width-2;i++) printf(" ");
printf("|\n");
}
printf("+");
for(i=0;i<width-2;i++) printf("-");
printf("+\n");
}
int main()
{
StringInGrid(20,6,"abcd1234");
return 0;
}
对于题目中数据,应该输出:
+------------------+
| |
| abcd1234 |
| |
| |
+------------------+
(如果出现对齐问题,参看【图1.jpg】)
注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。
(width-2-strlen(buf))/2," ",buf,(width-2-strlen(buf))/2," "
/*****************************************************************************/
5.九数组分数
1,2,3...9 这九个数字组成一个分数,其值恰好为1/3,如何组法?
下面的程序实现了该功能,请填写划线部分缺失的代码。
#include <stdio.h>
void test(int x[])
{
int a = x[0]*1000 + x[1]*100 + x[2]*10 + x[3];
int b = x[4]*10000 + x[5]*1000 + x[6]*100 + x[7]*10 + x[8];
if(a*3==b) printf("%d / %d\n", a, b);
}
void f(int x[], int k)
{
int i,t;
if(k>=9){
test(x);
return;
}
for(i=k; i<9; i++){
{t=x[k]; x[k]=x[i]; x[i]=t;}
f(x,k+1);
_____________________________________________ // 填空处
}
}
int main()
{
int x[] = {1,2,3,4,5,6,7,8,9};
f(x,0);
return 0;
}
注意:只填写缺少的内容,不要书写任何题面已有代码或说明性文字。
回溯一手,怎么说
{t=x[k]; x[k]=x[i]; x[i]=t;}
/*****************************************************************************/
6.加法变乘法
我们都知道:1+2+3+ ... + 49 = 1225
现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015
比如:
1+2+3+...+10*11+12+...+27*28+29+...+49 = 2015
就是符合要求的答案。
请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。
注意:需要你提交的是一个整数,不要填写任何多余的内容。
枚举一手,怎么说
#include <map>
#include <set>
#include <queue>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
for( int i = 1; i <= 48; i++ ) {
int cur = 1225 - (i+(i+1))+i*(i+1);
for( int j = i+1; j <= 48; j++ ) {
if( cur - (j+(j+1))+j*(j+1) == 2015 ) {
printf("one:%d two:%d\n", i, j);
}
}
}
return 0;
}
/**
*运行结果
*one:10 two:27
*one:16 two:24
*ans = 16
*/
/*****************************************************************************/
7.牌型种数
小明被劫持到X赌城,被迫与其他3人玩牌。
一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张。
这时,小明脑子里突然冒出一个问题:
如果不考虑花色,只考虑点数,也不考虑自己得到的牌的先后顺序,自己手里能拿到的初始牌型组合一共有多少种呢?
请填写该整数,不要填写任何多余的内容或说明文字。
暴力一手,怎么说
#include <map>
#include <set>
#include <queue>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define for1(a) for( int a = 0; a <= 4; a++)
using namespace std;
// 1 2 3 4 5 6 7 8 9 10 11 12 13
bool book[5][5][5][5][5][5][5][5][5][5][5][5][5];
int ans;
int main() {
for1(a1){
for1(a2){
for1(a3){
for1(a4){
for1(a5){
for1(a6){
for1(a7){
for1(a8){
for1(a9){
for1(a10){
for1(a11){
for1(a12){
for1(a13){
if( a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13 == 13 && !book[a1][a2][a3][a4][a5][a6][a7][a8][a9][a10][a11][a12][a13] ) {
book[a1][a2][a3][a4][a5][a6][a7][a8][a9][a10][a11][a12][a13] = 1;
ans++;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
cout << ans << endl;
return 0;
} //ans = 3598180
/*****************************************************************************/
移动距离
X星球居民小区的楼房全是一样的,并且按矩阵样式排列。其楼房的编号为1,2,3...
当排满一行时,从下一行相邻的楼往反方向排号。
比如:当小区排号宽度为6时,开始情形如下:
1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 .....
我们的问题是:已知了两个楼号m和n,需要求出它们之间的最短移动距离(不能斜线方向移动)
输入为3个整数w m n,空格分开,都在1到10000范围内
w为排号宽度,m,n为待计算的楼号。
要求输出一个整数,表示m n 两楼间最短移动距离。
例如:
用户输入:
6 8 2
则,程序应该输出:
4
再例如:
用户输入:
4 7 20
则,程序应该输出:
5
资源约定:
峰值内存消耗 < 256M
CPU消耗 < 1000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
求一手曼哈顿距离,怎么说
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cctype>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define for1(a) for( int a = 0; a <= 4; a++)
using namespace std;
int main() {
int w, m, n, x1, x2, y1, y2;
cin >> w >> m >> n;
x1 = (m-1)/w+1;
x2 = (n-1)/w+1;
if( x1 & 1 ) {
if(m%w) y1 = m % w;
else y1 = w;
}
else y1 = w - m%w + 1;
if( x2 & 1 ) {
if(n%w) y2 = n % w;
else y2 = w;
}
else y2 = w - n%w + 1;
//test
//printf("Point1: (%d,%d)\n", x1, y1);
//printf("Point1: (%d,%d)\n", x2, y2);
cout << abs(x1-x2)+abs(y1-y2) << endl;
return 0;
}
在线丢人,就在明天qwq