算法笔记入门篇(进制转换)

两种基本转换

(1)二进制转换为十进制

#include<stdio.h>
#include<iostream>
using namespace std;
//二进制转换为十进制
int main(){
        int x = 1111;
        int sum = 0;
        int u = 1;//
        while(x){
            //从后往前 依次取余得最后一位 并与因数(变化)相乘
            sum += (x%10) * u;
            u *=2;
            x /=10;
        }
      cout << sum <<endl;
    return 0;
}

(2) 十进制转换为 n 进制

#include<stdio.h>
#include<iostream>
using namespace std;
//除基取余法
int main(){
    int x;
    while(cin >> x){
        int a[40];//存储结果
        int i = 0;
        do{
            a[i] = x % 2;
            x /= 2;
            i++;
        }while(x!=0);//保证x=0时也会执行

        //反向输出
       for(int j = i-1;j>=0;j--){//i-1  因为do while 循环中i多加了1
            cout << a[j];
        }
        printf("\n");
    }
    return 0;
}

B1037 在霍格沃茨找零钱 (20分)

如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易。”现在,给定哈利应付的价钱 P 和他实付的钱 A,你的任务是写一个程序来计算他应该被找的零钱。

输入格式:

输入在 1 行中分别给出 P 和 A,格式为 Galleon.Sickle.Knut,其间用 1 个空格分隔。这里 Galleon 是 [0, 10​7​​] 区间内的整数,Sickle 是 [0, 17) 区间内的整数,Knut 是 [0, 29) 区间内的整数。
输出格式:

在一行中用与输入同样的格式输出哈利应该被找的零钱。如果他没带够钱,那么输出的应该是负数。
输入样例 1:

10.16.27 14.1.28

输出样例 1:

3.2.1

输入样例 2:

14.1.28 10.16.27

输出样例 2:

-3.2.1

#include<stdio.h>
const int A =17 * 29;//0.0 Tips:数值使用取为常数const
const int  B =29;
int a1,a2,b1,b2,c1,c2;
int a,b,c;
int main(){
    while(scanf("%d.%d.%d %d.%d.%d",&a1,&b1,&c1,&a2,&b2,&c2)!=EOF){
        int sum1 = 0,sum2 =0;int sum = 0;
        sum1 = a1* A + b1 * B + c1;
        sum2 = a2* A + b2 * B + c2;

            sum = sum2-sum1;//0.1 主要思路:转换为最小单位 求差值
            if(sum < 0){//0.3 负数的处理
               printf("-");
                sum = -sum;//取绝对值
            }
            //0.2 注意:
            // 分别得到三个值 类似于10进制 取每一位的操作
            a = sum / A;
            b = sum % A /B;
            c = sum % B;
            printf("%d.%d.%d\n",a,b,c);

    }

    return 0;

}

A1058 A+B in Hogwarts (20分)

compute A+B where A and B are given in the standard form of Galleon.Sickle.Knut (Galleon is an integer in [0,10​^7​​], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).

Input Specification:

Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.
Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input.
Sample Input:

3.2.1 10.16.27

Sample Output:
14.1.28

#include<stdio.h>
int main(){
    方法一 一个测试点未过
    long long a1,a2,b1,b2,c1,c2;
    scanf("%lld.%lld.%lld %lld.%lld.%lld",&a1,&b1,&c1,&a2,&b2,&c2);
    long long  a=0;
    long long b=0;
    long long c=0;
    a = a1+a2;
    if(c1 + c2 >= 29){

        c = (c1+c2) % 29;
        b += (c1+c2)/29;
    }else{
        c = c1 + c2;
    }
    if(b1+b2 >= 17){
        b += (b1+b2)%17;//注意1:过程中进位要累加
        a += (b1+b2)/17;
    }
    else{
        b += b1+b2;
    }
    printf("%lld.%lld.%lld",a,b,c);


    return 0;
}

方法二
凡是需要用到长整型的题目,全部改为用long long;与B1037思路类似

#include<cstdio>
#include<cstring>
int main(){
	long long  a,b,c,x,y,z;
	scanf("%lld.%lld.%lld %lld.%lld.%lld",&a,&b,&c,&x,&y,&z);
	long long sum = a*29*17 + b*29 + c + x*29*17 + y*29 + z;
	printf("%lld.%lld.%lld\n",sum/(29*17),sum%(17*29)/29,sum%(17*29)%29);
	return 0;
}


A1027 Colors in Mars (20分)

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

Input Specification:

Each input file contains one test case which occupies a line containing the three decimal color values.

Output Specification:

For each test case you should output the Mars RGB value in the following format: first output #, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a 0 to its left.
Sample Input:

15 43 71

Sample Output:

#123456

#include<stdio.h>
int main(){
    char ch[13]{
        '0','1','2','3','4','5','6','7','8','9','A','B','C'

    };//不同字符的对应关系
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    //1.首先 根据数的范围可得 在两位以内(没注意到导致绕弯)


    printf("#");
     printf("%c%c",ch[a/13],ch[a%13]);
      printf("%c%c",ch[b/13],ch[b%13]);
       printf("%c%c",ch[c/13],ch[c%13]);

    printf("\n");


    return 0;
}

code up练习部分

问题 A: 又一版 A+B

输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。
注意输入的两个数相加后的结果可能会超过int和long的范围。

输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。

(注意范围 long long类型即可 )

#include<stdio.h>
int main(){
    long long  A,B;//
    long long  m;
    long long D;
    while(scanf("%lld %lld %lld",&m,&A,&B)!=EOF){
        //测试格式  m=0时结束
       if(m==0){
            break;
       }
        D = A +B;
        int a[40];
        int i=0;
        do{
            a[i] = D % m;
            D /= m;
            i++;

        }while(D!=0);
        for(int j = i-1;j>=0;j--){
            printf("%d",a[j]);
        }
        printf("\n");
    }
    return 0;
}

重点题目:

问题 B: 数制转换

求任意两个不同进制非负整数的转换(2进制~16进制),所给整数在long所能表达的范围之内。
不同进制的表示符号为(0,1,…,9,a,b,…,f)或者(0,1,…,9,A,B,…,F)。

输入

输入只有一行,包含三个整数a,n,b。a表示其后的n 是a进制整数,b表示欲将a进制整数n转换成b进制整数。a,b是十进制整数,2 =< a,b <= 16。

输出

可能有多组测试数据,对于每组数据,输出包含一行,该行有一个整数为转换后的b进制数。输出时字母符号全部用大写表示,即(0,1,…,9,A,B,…,F)。

#include<stdio.h>
#include<string.h>
int main(){
    int a,b;
    char n[100];
    while(scanf("%d %s %d",&a,n,&b)!=EOF){
        int len = strlen(n);
        int ch;

        int sum = 0;
        for(int i =0;i<len;i++){
            if(n[i]>='0' && n[i]<='9'){
                ch = n[i] - '0';
            }
            else if(n[i]>='a' && n[i]<='f'){
                 ch = n[i] - 'a' + 10;
            }
            else if(n[i]>='A' && n[i]<='F'){
                 ch = n[i] - 'A' + 10;
            }
            //注意A-F a-f 情况的处理
            sum = sum * a +ch;//1.a进制转为十进制

        }
        //printf("sum=%d",sum);

        //2.转为b进制
        int p;
        char x[100];
        int j= 0;
        do{
            p = sum % b;
            if(p<10){
                x[j] = p + '0';
            }
            else{
                x[j] = p -10+'A';
            }

            sum /= b;
            j++;

        }while(sum!=0);

        for(int i=j-1;i>=0;i--)
		{
			printf("%c",x[i]);
		}
		printf("\n");

    }
    return 0;

}

问题 C: 进制转换

题目描述

将一个长度最多为30位数字的十进制非负整数转换为二进制数输出。

输入

多组数据,每行为一个长度不超过30位的十进制非负整数。
(注意是10进制数字的个数可能有30个,而非30bits的整数)

输出

每行输出对应的二进制数。

思路:使用字符数组
注意:这里的除基取余要用大数除法

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
//除基取余法
int main(){
    char x[40];

    while(scanf("%s",x)!=EOF){

        int num[40];
        int a[100];//存储结果
        int sum=1;//while循环执行初始化
       int i = 0;
        int len= strlen(x);

        for(int k = 0;k<len;k++){
            num[k] = x[k] - '0';//字符串类型转化为整数类型
        }

       while(sum){
            sum = 0;//当商为0 停止循环
            for(int j = 0;j<len;j++){
                int t = num[j]/2;//商不为0 时 继续执行
                sum += t;

                //两种情况的判断
                if(j==len-1){//最后一位直接取余
                    a[i++] = num[j] % 2;

                }
                else{//把后面的位合并 作为下一次计算的基数
                    num[j+1] = (num[j] % 2) * 10 + num[j+1];
                }
                num[j] = t;//记录所得的商

            }
       }
        //反向输出
       for(int j = i-1;j>=0;j--){
            cout << a[j];
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值