1. 随意组合
小明被绑架到X星球的巫师W那里。当时,W正在玩弄两组数据 (2 3 5 8) 和
(1 4 6 7),他命令小明从一组数据中分别取数与另一组中的数配对,共配成4对
(组中的每个数必被用到)。
小明的配法是:{(8,7),(5,6),(3,4),(2,1)}
巫师凝视片刻,突然说这个配法太棒了!
因为:
每个配对中的数字组成两位数,求平方和,无论正倒,居然相等:
87^2 + 56^2 + 34^2 + 21^2 = 12302
78^2 + 65^2 + 43^2 + 12^2 = 12302
小明想了想说:“这有什么奇怪呢,我们地球人都知道,随便配配也可以
啊!”
{(8,6),(5,4),(3,1),(2,7)}
86^2 + 54^2 + 31^2 + 27^2 = 12002
68^2 + 45^2 + 13^2 + 72^2 = 12002
巫师顿时凌乱了。。。。。
请你计算一下,包括上边给出的两种配法,巫师的两组数据一共有多少种配
对方案具有该特征。
配对方案计数时,不考虑配对的出现次序。
就是说:{(8,7),(5,6),(3,4),(2,1)}与{(5,6),(8,7),(3,4),(2,1)}是同一种
方案。
运行代码:
include <stdio.h>
int main(){
int m[]={2,3,5,8};
int n[]={1,4,6,7};
int a=0,b=0,c=0,d=0,num=0,x,y;
//for循环嵌套,四个位置将四个数遍历一次;
for(a=0;a<4;a++){
for(b=0;b<4;b++){
if(a==b)
b++;
for(c=0;c<4;c++){
while(c==a||c==b){
c++;
}
for(d=0;d<4;d++){
while(d==a||d==b||d==c){
d++;
}
x=(m[0]*10+n[a])*(m[0]*10+n[a])+(m[1]*10+n[b])*(m[1]*10+n[b])+(m[2]*10+n[c])*(m[2]*10+n[c])+(m[3]*10+n[d])*(m[3]*10+n[d]);
y=(n[a]*10+m[0])*(n[a]*10+m[0])+(n[b]*10+m[1])*(n[b]*10+m[1])+(n[c]*10+m[2])*(n[c]*10+m[2])+(n[d]*10+m[3])*(n[d]*10+m[3]);
if(x==y){
num++;
}
}
}
}
}
printf("%d",num);
}
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.
回文数
运行代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
int n=1,m=1,k=0,j=0,x,y;
int num[150];
while(n!=0){
scanf("%d",&n);
if(n==0)exit(0);
for(int i=2;i<=16;i++){
k=0;
m=1;
x=n;
while(m!=0){
m=x/i;
num[k]=x%i;
k++;
x=m;
}
y=k;
k=k-1;
for(j=0;j<y/2;j++){
if(num[j]==num[k]){
k--;
}
}
if((y-k-1)==y/2&&n>i){
printf("Number %d is palindrom in basis ",n);
for(int s=y-1;s>=0;s--){
printf("%d",num[s]);
}
printf("\n");
}else if(i==2){
printf("Number %d is not a palindrom\n",n);
break;
}
}
}
}
4. 电话号码
辉子最近接到了一个棘手的任务,他们公司有一个电话簿,但是这是一个奇
怪的电话簿,因为它不是用数字记录电话号码,而是用数字键上所对应的字母来
记录电话号码(
2-abc,3-def,4-ghi,5-jkl,6-mno,7-pqrs,8-tuv,9-wxyz),电话号
码只有11位。现在你的任务就是帮辉子写一个程序来把这些字母的电话号码转化
成数字的电话号码。
输入:
第一行输入一个正整数T(0<T<=100),表示测试数据的组数,每组测试数据只
有一行,输入一串字符(字符长度为11)。
输出:
每组输出占一行,输出数字的电话号码。
运行代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
//判断用户输入是否正确;
int t=2,j=0,i=0;
printf("请输入一百以内的数:\n");
scanf("%d",&t);
if(t<0||t>100){
printf("输入的数据有误");
exit(0);
}
char num[1100];
for(i=0;i<t*11+1;i++){
scanf("%c",&num[i]);
}
for(j=0;j<t;j++){
for(i=11*j;i<11*(j+1)+1;i++){
if(num[i]=='a'||num[i]=='b'||num[i]=='c'){
printf("2");
}else if(num[i]=='d'||num[i]=='e'||num[i]=='f'){
printf("3");
}else if(num[i]=='g'||num[i]=='h'||num[i]=='i'){
printf("4");
}else if(num[i]=='j'||num[i]=='k'||num[i]=='l'){
printf("5");
}else if(num[i]=='n'||num[i]=='m'||num[i]=='o'){
printf("6");
}else if(num[i]=='p'||num[i]=='q'||num[i]=='r'||num[i]=='s'){
printf("7");
}else if(num[i]=='t'||num[i]=='u'||num[i]=='v'){
printf("8");
}else if(num[i]=='w'||num[i]=='x'||num[i]=='y'||num[i]=='z'){
printf("9");
}
}
printf("\n");
}
}