1. 随意组合
1.1问题描述:两组数组{1,4,6,7}{2,3,5,8} 从一组数据中分别取数与另一组中的数配对,共配成4对(组中的每个数必被用到),每个配对中的数字组成两位数,求平方和,无论正倒,结果相等:
87^2 + 56^2 + 34^2 + 21^2 = 12302
78^2 + 65^2 + 43^2 + 12^2 = 12302
求这两组数据一共有多少种配对方案具有该特征。
1.2 问题分析:只要将两组数中的数两两配对出所有组合, 将每个组合进行计算即可。 一组数不变, 另外一组数列出所有排列,由于不需要考虑位置, 所以只需要对一组数列出排列就行!
1.3流程图:
1.4源程序及运行结果:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[4]={2,3,5,8};
int b[4]={1,4,6,7};
int x,y,ans=0;
do
{
int a1=a[0]*10+b[0],b1=b[0]*10+a[0];
int a2=a[1]*10+b[1],b2=b[1]*10+a[1];
int a3=a[2]*10+b[2],b3=b[2]*10+a[2];
int a4=a[3]*10+b[3],b4=b[3]*10+a[3];
x=a1*a1+a2*a2+a3*a3+a4*a4;
y=b1*b1+b2*b2+b3*b3+b4*b4;
if(x==y)
ans++;
}while(next_permutation(a,a+4));
printf("%d",ans);
return 0;
}
2. 奇怪的比赛
2.1问题描述:每位选手都有一个起步的分数为10分。每位选手需要回答10个问题(其编号为1到10),越后面越有难度。答对的,当前分数翻倍;答错了则扣掉与题号相同的分数。
某获胜选手最终得分刚好是100分, 算出所有可能的情况。
2.2问题分析:如果把答对的记为1,答错的记为0,则10个题目的回答情况可以用仅含有1和 0的串来表示。自定义函数f,在函数f中进行10次循环,当循环次数为10且分数为100时,输出对应满足条件的数组。
2.3流程图:
2.4源程序及运行结果:
#include<stdio.h>
int s[11];
void f(int k,int x){
if(k>10||x<=0){
return;
}
if(k==10&&x==100){
for(int i=0;i<10;i++){
printf("%d",s[i]);
}
printf("\n");
}
s[k]=1;
f(k+1,x*2);
s[k]=0;
f(k+1,x-k-1);
}
int main(){
f(0,10);
return 0;
}
3.palindrom
Statement of the Problem
We say that a number is a palindrom if it is the sane when read from left to right or
from right to left. For example, the number 75457 is a palindrom.
Of course, the property depends on the basis in which is number is represented. The
number 17 is not a palindrom in base 10, but its representation in base 2 (10001) is a
palindrom.
The objective of this problem is to verify if a set of given numbers are palindroms in
any basis from 2 to 16.
Input Format
Several integer numbers comprise the input. Each number 0 < n < 50000 is given in
decimal basis in a separate line. The input ends with a zero.
Output Format11
Your program must print the message Number i is palindrom in basis where I is the
given number, followed by the basis where the representation of the number is a
palindrom. If the number is not a palindrom in any basis between 2 and 16, your
program must print the message Number i is not palindrom.
3.1 问题描述:如果一个数字从左到右读时是相同的,我们说它是回文 从右向左。比如75457这个数字是回文。
当然,属性取决于表示数字的基础。数字17不是以十进制为基数的回文,但它在二进制(10001)中的表示是回文。
这个问题的目的是验证一组给定的数字是否是从二进制到十六进制任一进制的回文。
输入格式:几个整数组成输入。每个数字0 < n < 50000都以十进制为基础,在单独的一行中给出。输入以零结尾。
输出格式:你的程序必须打印信息,数字i是回文,其中i是给定的数字,后面是表示数字的回文。如果数字不是二进制到十六进制之间的回文,您的程序必须打印消息“数字i不是回文”。
3.2问题分析:将n转化为2-16进制的数存到c数组中,判断c中正数第几位和倒数第几位的数是否相等,若相等,输出此时对应的进制数,即表明它是以几进制为基础的回文数 .
3.3流程图:
3.4源程序及运行结果:
#include<stdio.h>
int s[11];
void f(int k,int x){
if(k>10||x<=0){
return;
}
if(k==10&&x==100){
for(int i=0;i<10;i++){
printf("%d",s[i]);
}
printf("\n");
}
s[k]=1;
f(k+1,x*2);
s[k]=0;
f(k+1,x-k-1);
}
int main(){
f(0,10);
return 0;
}
4. 电话号码
4.1问题描述:记录电话号码(2-abc,3-def,4-ghi,5-jkl,6-mno,7-pqrs,8-tuv,9-wxyz),电话号 码只有11位。输入: 第一行输入一个正整数T(0<T<=100),表示测试数据的组数,每组测试数据只 有一行,输入一串字符(字符长度为11)。
输出: 每组输出占一行,输出数字的电话号码。
4.2问题分析:本题主要考察对递归的运用,但也不算是纯粹的递归,只是通过递归将所有可能出现的结果得到,再通过循环判断得出符合题目要求的结果,最终以字符串的形式表示出来。
4.3流程图:
4.4源程序及运行结果:
#include<stdio.h>
#include<string.h>
int main(){
int n,i,j;
char a[12];
scanf("%d",&n);
while(n--){
scanf("%s",a);
for(i=0;i<11;i++){
if(a[i]=='a'||a[i]=='b'||a[i]=='c'){
a[i]='2';
}
if(a[i]=='d'||a[i]=='e'||a[i]=='f'){
a[i]='3';}
if(a[i]=='g'||a[i]=='h'||a[i]=='i'){
a[i]='4';}
if(a[i]=='j'||a[i]=='k'||a[i]=='l'){
a[i]='5';}
if(a[i]=='m'||a[i]=='n'||a[i]=='o'){
a[i]='6';}
if(a[i]=='p'||a[i]=='q'||a[i]=='r'||a[i]=='s'){
a[i]='7';}
if(a[i]=='t'||a[i]=='u'||a[i]=='v'){
a[i]='8';}
if(a[i]=='w'||a[i]=='x'||a[i]=='y'||a[i]=='z'){
a[i]='9';}}
for(i=0;i<11;i++){
printf("%c",a[i]);
}
printf("\n");
}}