zoukankan      html  css  js  c++  java
  • PAT Basic Level 1006

    AC代码


    1
    #include <stdio.h> 2 int main () 3 { 4 int x; 5 scanf("%d",&x); 6 int mask = 1; 7 int temp = x; 8 while(temp != 0 ) //计算输入数据的位数,算出用于分割位数的mask大小 9 { 10 temp = temp/10; 11 mask = mask * 10; 12 } 13 mask = mask/10; 14 while (mask > 0) 15 { 16 if(mask == 100) //如果mask == 100说明输入的数据是三位数 17 { 18 temp = x/mask; //得到百位的数据,输出B 19 while(temp > 0) 20 { 21 printf("B"); 22 temp--; 23 } 24 x = x%mask; //每次处理完毕一位数字,将那位数字去掉 25 mask = mask/10; //mask同时减小 26 }else 27 if(mask == 10) //mask == 10,数据为两位数 28 { 29 temp = x/mask; 30 while(temp > 0) 31 { 32 printf("S"); 33 temp--; 34 } 35 x = x % mask; 36 mask = mask/10; 37 }else 38 if(mask == 1) //mask == 1,数据为一位数 39 { 40 temp = x/mask; 41 int i = 1; 42 while(temp>0) 43 { 44 45 printf("%d",i); 46 i++; 47 temp--; 48 } 49 x = x % mask; 50 mask = mask/10; 51 } 52 53 } 54 return 0 ; 55 }

     反思:AC代码相比网上他人代码显得复杂。其实不必反复对原数据操作,可将数据每位放入数组中,然后再进行相应操作更简明。

  • 相关阅读:
    Python生成器表达式
    Python列表解析
    Python迭代器(Iterator)
    Python set 集合
    python eval 函数妙用
    Python字典 (dict)
    Python序列之元组 (tuple)
    Python序列之列表 (list)
    递归和反射
    常用标准库
  • 原文地址:https://www.cnblogs.com/Ponytai1/p/5975442.html
Copyright © 2011-2022 走看看