第一题:奖券数目
有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利。 虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求。某抽奖活动的奖券号码是5位数(10000-99999),要求其中不要出现带“4”的号码,主办单位请你计算一下,如果任何两张奖券不重号,最多可发出奖券多少张。 请提交该数字(一个整数),不要写任何多余的内容或说明性文字。
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> using namespace std; void solve() { int ans = 0; for (int a = 1; a <= 9; a++) { if (a == 4) continue; for (int b = 0; b <= 9; b++) { if (b == 4) continue; for (int c = 0; c <= 9; c++) { if (c == 4) continue; for (int d = 0; d <= 9; d++) { if (d == 4) continue; for (int e = 0; e <= 9; e++) { if (e == 4) continue; ans++; } } } } } cout << ans << endl; } int main() { solve(); return 0; }
运行结果是:52488.
第二题 星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。 每个炸弹都可以设定多少天之后爆炸。 比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。 有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。 请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19 请严格按照格式书写。不能出现其它文字或符号。
使用Excel:
代码解法:
#include <iostream> using namespace std; int year = 1777, month = 4, day = 30; bool IsEndofMonth(); void AddDay(int days); void IncDay(); bool IsLeapYear(); bool IsLeapYear() { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } bool IsEndofMonth() { switch(month) { case 4: case 6: case 9: case 11: return day == 30; case 2: if (IsLeapYear()) return day == 29; else return day == 28; default: return day == 31; } } void IncDay() //增加一天 { if(IsEndofMonth()) //增加天数,记得要判断是否是年末,月末 { if(month == 12) { day = 1; month = 1; year++; } else { //已经是IsEndMonth day = 1; month++; } } else { day++; } } void AddDay(int days) { for (int i = 1; i <= days; i++) //增加多少天 days - 1 { IncDay(); } } int main() { // AddDay(5343); cin >> year >> month >> day; AddDay(1000); cout << year << "-" << month << "-" << day << endl; return 0; }
第三题
观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞 ------------------- 三 羊 生 瑞 气 其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。 请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
#include <iostream> #include <algorithm> using namespace std; void solve() { int num[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; do { int a = num[0]*1000 + num[1]*100 + num[2]*10 + num[3]; int b = num[4]*1000 + num[5]*100 + num[6]*10 + num[1]; int c = num[4]*10000 + num[5]*1000 + num[2]*100 + num[1]*10 + num[7]; if (a + b == c && b > 1000) { cout << b << endl; } } while (next_permutation(num, num+10)); } int main() { solve(); return 0; }
第四题 格子中输出
StringInGrid函数会在一个指定大小的格子中打印指定的字符串。 要求字符串在水平、垂直两个方向上都居中。 如果字符串太长,就截断。 如果不能恰好居中,可以稍稍偏左或者偏上一点。 下面的程序实现这个逻辑,请填写划线部分缺少的代码。 #include <stdio.h> #include <string.h> void StringInGrid(intwidth, 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】)
注解:printf("%*s", len, "xxx"):相当于是 (len, "str"), 设置输出字符 为len位, 如果str长度不足len位, 前面补空格
printf("%*s%s%*s", (width-2-strlen(s))/2, "", s, (width-2-strlen(s))/2, ""); //填空
第五题:九数组分数 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;}
第六题:加法变乘法
我们都知道:1+2+3+ ... + 49 = 1225 现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如: 1+2+3+...+10*11+12+...+27*28+29+...+49= 2015 就是符合要求的答案。 请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10)。 注意:需要你提交的是一个整数,不要填写任何多余的内容。
原文地址:https://www.cnblogs.com/douzujun/p/8569467.html
时间: 2024-12-15 22:28:22