中南林业科技大学第十一届程序设计大赛校赛题解

本文是中南林业科技大学第十一届程序设计大赛的题解,涵盖多种编程题目,包括译码、修复栅栏、有趣的二进制等。通过解题思路和示例,帮助参赛者理解并解决各类编程问题,涉及字符串处理、数学计算、数组操作等技巧。
摘要由CSDN通过智能技术生成

原题链接

A-译码

Problem Description:

现在定义一种编码规则:对于长度为3的字符串(均由小写字母组成),首先按照字典序进行排序,即aaa,aab,aac,…,zzz,将这些字符串按照顺序依次从00001至17575编码(前缀0不可省略),即aaa=00000,aab=00001,aac=00002,…,zzz=17575。现在给出一串数字,请你通过计算输出这串数字对应的原字符串。(输入保证该数字长度为5的倍数)
例如输入000021757511222,每五位编号对应于一个字符串
编号00002对应字符串 aac
编号17575对应字符串 zzz
编号11222对应字符串 qpq
故输出为 aaczzzqpq

Input:

输入第一行包含一个整数T,代表测试案例个数。(0 < T <= 10)

Output:

输出数字串对应的原字符串。

Sample Input:
2
10
0000000001
15
000021757511222

Sample Output:

aaaaab
aaczzzqpq

题解:
先初始化一下,把所有的字符串和对应的编码处理出来,然后输入字符串,每五位对应一个数字,每一个数字又对应我们之前处理好的字符串,此时我们之间输出即可。

#include<bits/stdc++.h>
char str[105],a[20000][5];
void init(){
    int p=0;
    for(int i='a';i<='z';i++)
        for(int j='a';j<='z';j++)
            for(int k='a';k<='z';k++)
                a[p][0]=i,a[p][1]=j,a[p][2]=k,a[p++][3]='\0';
}
int main()
{
    init();
    int n,T;
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%s",&n,str),n/=5;
        for(int i=0,t;t=0,i<n;i++){
            for(int j=0;j<5;t=t*10+str[i*5+j]-'0',j++){}
            printf("%s",a[t]);
        }
        puts("");
    }
    return 0;
}

B-Fence Repair

Description:

Founded in 1958, CSUFT has developed into a multidisciplinary university comprising a wide range of disciplines in science, engineering, agriculture, arts, law, economics, management and education, and has been authorized to offer a comprehensive range of programs including bachelor, master, doctor’s programs and post-doctoral programs. Since its foundation, the university has turned out over 160,000 talents. CSUFT is located in the national historical and cultural city : Changsha.
The above is the history of our school. The next is the description of this problem ^_^.
Giving n numbers,Cyl want to find the answer how many pairs of these numbers meet the condition of Ai *2 = Aj (i < j). Please you tell him.
Please note that there are many groups of samples.

Input:

Line 1: One integer N, the number of numbers. (1 < N <= 100)
Lines 2…..N+1: Each line contains a single integer ai. (0< ai < 100)

Output:

Line 1: One integer.

Sample Input:

3
1
2
2

Sample Output:

2

题意:
就是求有多少对数字可以满足 a [ i ] * 2 == a [ j ] (i < j)这个条件

#include<bits/stdc++.h>
int main()
{
    int n,a[100];
    while(scanf("%d",&n)!=EOF){
        int cnt=0;
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++) if(a[i]*2==a[j]) cnt++;
        printf("%d\n",cnt);
    }
    return 0;
}

C-有趣的二进制

Description:

小新在学C语言的时候,邝老师告诉他double类型的数据在表示小数的时候,小数点后的有效位是有限的,但是没有告诉他这是为什么,后来他发现0.1的二进制是一个无限循环小数0.000110011001100110011001100•••,如果只取27位小数,再转换成十进制的话就变成了0.09999999403953552,小新开心的解决了这个问题。与此同时,小新又有了一个新的问题:一个数在64位二进制补码表示下,一共有多少个1。因为小数有无解的情况,所以我们保证输入的都是整数。

Input:

有多组数据,每一行为一个数字n。

Output:

输出这个数字在二进制补码下1的个数。

Sample Input:

15

Sample Output:

4

正数的补码等于其本身,负数的补码等于其取反加一

方法零:字符串模拟求补码过程

方法一:
把数字n每次和1进行与运算,并且在每次运算后,n都右移一位
比如求对于数n,他的二进制为 11101:
11101&1 == 1 cnt+=1 n右移一位
01110&1 == 0 cnt+=0 n右移一位
00111&1 == 1 cnt+=1 n右移一位
……..

需要注意的是:如果负数,我们不断的移位操作,符号位是不会变的,会导致死循环。所以需要将其转化为无符号整数
下面代码是先定义一个无符号数,当做有符号输入
其实我们也可以定义有符号数,输入后再进行类型转换

#include<bits/stdc++.h>
int main(){
    unsigned long long n;
    while(scanf("%lld",&n)!=EOF){
        int cnt=0;
        while(n){
            cnt+=n&1;
            n>>=1;
        }
        printf("%d\n
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值