zoukankan      html  css  js  c++  java
  • 7-1 币值转换 (20 分)

    实验代码:

    include<stdio.h>

    int main ()
    {
    int n, initial_n;
    scanf("%d", &n);
    initial_n = n; // 保留初始值

    char num[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
    char unit[10] = {0, 0, 'S', 'B', 'Q', 'W', 'S', 'B', 'Q', 'Y'};//舍弃前两位 
    char result[17]={0}; // 9 位数最多有 17 位输出 
    
    int i, last_i = n % 10; 
    int j = 0;
    int count_n = 0;
    while (n > 0) {
        i = n % 10;
        n /= 10;
        count_n ++;
        if (i == 0 && (count_n % 4) > 1) { // 从十位开始统计(个位0永远不输出) 
            if (last_i != 0) {   // 如果前一位不等于 0,那就输出这个 0 
                result[j++]  = num[i];    
            } 
        }
        if (count_n == 5 && i == 0 && initial_n < 100000000) {
            result[j++] =  unit[count_n]; // 万 w 是一定要输出的    
        }
        if (count_n > 1 && i != 0) {    // 非 0 不输出单位 
            result[j++] = unit[count_n];
        } 
        if (i != 0) {               // 处理非 0 数的输出 
            result[j++] = num[i];
        }
        last_i = i; //保留 i 的前一位的值 用于处理 0 
    }
    
    if (initial_n == 0) {       // 处理特殊值 0 
        result[j++]  = num[i];
    } 
    
    for (j=j-1; j>=0; j--) {
        printf("%c", result[j]);
    }
    printf("
    ");
    
    return 0;
    

    }

    1. 这题我开始以为看起来很简单,我就先开始从小地方着手,先输入一个整数,用小写英文字母a-j顺序代表大写数字0-9
      就是如何把数字相应的换成字母,但是发现操作起来,并不顺利。因为写出来的代码,根本不能数字转换成字母,于是我上网查询

    这是上网提问后的结果,但是把这个小地方整合到这个大题目中并不是那么容易。这题有很多不明白的地方 还是看不懂。

  • 相关阅读:
    C语言:线程同步之信号量(sem_init,sem_post,sem_wait)
    SVN使用 -work
    atomic c++ y原子变量 替换锁代码
    原子变量
    perf
    内存泄露检测工具对比
    valgrind ----直接使用参数
    堆内存分析---特别棒
    调不尽的内存泄漏,用不完的 Valgrind(转)
    静态库动态库制作方法
  • 原文地址:https://www.cnblogs.com/liualiu/p/10405331.html
Copyright © 2011-2022 走看看