C程序设计基础期中测试上机

内容纲要

1039. DisplayBinary

Input an integer, and output its binary counterpart. For example, binary for -10 is -1010B.

输入格式
An integer in range [LONGLONG_MIN, LONGLONG_MAX].

输出格式
Binary counterpart ending with B .

样例

Input
-10
Output
-1010B
Input
0
Output
0B
Input
10
Output
1010B

题解:

没啥难度,开个超过65位的字符数组,首位当符号位,开一个i记录下标,然后从末尾往前取余插入,输出时先判断符号位,然后将数组从i+1的位置输出到末尾,最后加个B即可。

代码:

#include <stdio.h>
#include <math.h> 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
    long long n;
    unsigned long long n1;
    int i = 1000; 
    scanf("%lld", &n);
    if(n==0){
        printf("0B\n");
        return 0;
    }
    char a[1001] = {'\0'};
    if(n<0){
        a[0] = '-';
        n1 = -1*n;
    }
    else
        n1 = n;
    int temp;
    while(n1){
        temp = n1%2;
        n1 /= 2;
        a[i] = temp+48;
        i--;
    }
    if(a[0]=='-')
        printf("-");
    for(int j = i+1; j < 1001; j++)
        printf("%c",a[j]);
    printf("B\n");
    return 0;
}

1040. Permutation

On a standard telephone keypad, the digits are mapped onto the alphabet (minus the letters Q and Z) as shown in this diagram:

In order to make their phone numbers more memorable, service providers like to find numbers that spell out some word appropriate to their business that makes that phone number easier to remember. Such words that help you remember some other data are called mnemonics.
Write a function ListMnemonics that generates all possible letter combinations that correspond to a given number, represented as a string of digits. For example, if you call ListMnemonics (“723”) your program should generate the 27 possible letter combinations corresponding to that prefix, as follows:

If the argument passed to ListMnemonics contains a 0 or a 1, that position in the output should simple be displayed as the digit, since there are no letters that correspond to it. For example, if you used the function to generate mnemonics for the area code 415, you program should generate the following nine strings:

输入格式
A string with three digits

输出格式
All mnemonics with alphabetical order, one mnemonic in one line

Input
723
Output
PAD

PAE
PAF
PBD
PBE
PBF
PCD
PCE
PCF
RAD
RAE
RAF
RBD
RBE
RBF
RCD
RCE
RCF
SAD
SAE
SAF
SBD
SBE
SBF
SCD
SCE
SCF|

Input
000
Output
000
Input
415
Output
G1J

G1K
G1L
H1J
H1K
H1L
I1J
I1K
I1L |

题解

没想到更好做法,我这算是暴力破解了。因为思路是一边写一边产生的,里面有很多可以优化的地方,但是就暂时不改动,当作反面教材给大家看看吧。
1) 开一个长数组,计入A-Y去除Q的所有字母,然后就可以通过指令读入的数字减二再乘以三找到数组对应的下标。
2) 手动排列组合,三种情况,两种情况开二维数组,用0,1,2代替次序。
3) 每一个字母则可以通过步骤1中的数组,及步骤1中的下标加上步骤2中排列组合的0,1,2呼出。

代码

#include <stdio.h>
#include <math.h>

int main()
{
    char a[30] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', '0','0','0','1','1','1'}; 
    char ch;
    char cmd[3];
    int i = 0;
    int count_Z_O = 0;
    while((ch = getchar())!='\n'){
        if(ch == '0' || ch == '1')
            count_Z_O++;
        cmd[i]=ch;
        i++;
    }
    if(cmd[0]=='0'&&cmd[1]=='0'&&cmd[2]=='0'){
        printf("000\n");
        return 0;
    }
    if(cmd[0]=='1'&&cmd[1]=='1'&&cmd[2]=='1'){
        printf("111\n");
        return 0;
    }
    if(count_Z_O==1){
        int arr1[9][2]={{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};
        for(int j = 0; j < 9; j++){
            int index_A = (cmd[0]-48-2)*3;
            int index_B = (cmd[1]-48-2)*3;
            int index_C = (cmd[2]-48-2)*3;
            if(cmd[0]=='0'||cmd[0]=='1')
                printf("%c%c%c\n",cmd[0],a[index_B+arr1[j][0]],a[index_C+arr1[j][1]]);
            if(cmd[1]=='0'||cmd[1]=='1')
                printf("%c%c%c\n",a[index_A+arr1[j][0]],cmd[1],a[index_C+arr1[j][1]]);
            if(cmd[2]=='0'||cmd[2]=='1')
                printf("%c%c%c\n",a[index_A+arr1[j][0]],a[index_B+arr1[j][1]],cmd[2]);
        }
        return 0;
    }
    if(count_Z_O==2){
        int index_A = (cmd[0]-48-2)*3;
        int index_B = (cmd[1]-48-2)*3;
        int index_C = (cmd[2]-48-2)*3;
        if(cmd[0]!='0'&&cmd[0]!='1')
            printf("%c%c%c\n%c%c%c\n%c%c%c",a[index_A],cmd[1],cmd[2],a[index_A+1],cmd[1],cmd[2],a[index_A+2],cmd[1],cmd[2]);
        if(cmd[1]!='0'&&cmd[1]!='1')
            printf("%c%c%c\n%c%c%c\n%c%c%c",cmd[0],a[index_B],cmd[2],cmd[0],a[index_B+1],cmd[2],cmd[0],a[index_B+2],cmd[2]);
        if(cmd[2]!='0'&&cmd[2]!='1')
            printf("%c%c%c\n%c%c%c\n%c%c%c",cmd[0],cmd[1],a[index_C],cmd[0],cmd[1],a[index_C+1],cmd[0],cmd[1],a[index_C+2]);
        return 0;
    }
    if(count_Z_O==3){
        printf("%c%c%c\n",cmd[0],cmd[1],cmd[2]);
        return 0;
    }
    int arr[27][3]={{0,0,0},{0,0,1},{0,0,2},{0,1,0},{0,1,1},{0,1,2},{0,2,0},{0,2,1},{0,2,2},{1,0,0},{1,0,1},{1,0,2},{1,1,0},{1,1,1},{1,1,2},{1,2,0},{1,2,1},{1,2,2},{2,0,0},{2,0,1},{2,0,2},{2,1,0},{2,1,1},{2,1,2},{2,2,0},{2,2,1},{2,2,2}};
    char array[27][3];
    i = 0;
    for(int j = 0; j < 27; j++){
        int index_A = (cmd[0]-48-2)*3;
        int index_B = (cmd[1]-48-2)*3;
        int index_C = (cmd[2]-48-2)*3;
        printf("%c%c%c\n", a[index_A+arr[j][0]],a[index_B+arr[j][1]],a[index_C+arr[j][2]]);
    }

    return 0;
}

1041. Compute

Write a RECURSIVE function to compute the following formula:
sum(n)= 1 - \frac{1}{2} + \frac{1}{3}...-(-1)^n* \frac{1}{n}

/***************************************************************/
/*                                                             */
/*  DON'T MODIFY main function ANYWAY!                         */
/*                                                             */
/***************************************************************/
#include <stdio.h>
double sum(int n)
{

 // TODO: your function definition

}
int main()
{
    int n;
    scanf("%d",&n);
    printf("%f\n",sum(n));
    return 0;
}

题解

这题应该不用说了吧。

代码

#include <stdio.h>

double sum(int n){
    if(n==1) return 1;
    else return sum(n-1) + (n%2?1.0:-1.0)/n;
}

int main(){
    int n;
    scanf("%d",&n);
    printf("%f\n",sum(n));
    return 0;
}

1042. LCM

In arithmetic, the least common multiple (LCM) of two integers a and b, usually denoted by LCM (a, b), is the smallest positive integer that is divisible by both a and b. For example, the LCM of 4 and 6 is 12.

Write a program to compute the least common multiple of integers ,.

输入格式
Input in one line.

Input in another line.

输出格式
Output the least common multiple of integers in one line.

题解

单次循环,每次判定两个数;
先求前两个数的最小公倍数,为二数乘积再除以最大公约数,公约数很好判,从最小的数倒序递减1,判断是否能同时被两个数整除即可。
然后用这个最小公倍数和下一个数去作公倍数运算。
记得开unsigned long long。

代码

#include <stdio.h>

unsigned long long LCM(int n, int a[]){
    if(n==1)
        return a[0];
    else{
        unsigned long long temp = a[0];
        for(int i = 1; i <n; i++){
            int min = temp>a[i]?a[i]:temp;
            int sFac = 1;
            for(int j = min; j > 1; j--){
                if(a[i] % j == 0 && temp % j == 0){
                    sFac = j;break;
                }
            }
            temp = temp * a[i] / sFac;
        }
        return temp;
    }
}

int main()
{
    int a[101]={0};
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n; i++)
        scanf("%d",&a[i]);
    printf("%lld\n",LCM(n,a));    
    return 0;
}

留下评论