Chinaunix首页 | 论坛 | 博客
  • 博客访问: 562856
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: C/C++

2015-02-28 10:27:43


题目链接:



这道题需要注意的有一下几点:
1. 首先是原数列中的数字出现的次数要与将原数列*2 之后得到的结果数列中出现的次数是相同的,
    原本打算写一个出现/不出现 只有两种状态的 appear 数组来表示,但是出错了, 重新看题发现,出现的次数必须要相同,
    所以将 appear 状态数组修改成为计数数组
2. 第二点需要注意的就是,在输出结果的时候,无论是满足条件输出 Yes ,还是不满足条件 输出 No,都要加上回车之后再将
  计算出来的乘以 2 倍的数列输出
3. 由于数组的长度为 [1,20] 所以,需要使用大数乘法,在字符串的基础之上进行乘法以及进位的数值计算

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int main ( void )
  5. {
  6.     int appear [10] ;
  7.     char num1[25], num2[25] ;
  8.     int more = 0 ,len , temp , counter = 0 ;
  9. /**
  10. more 用来表示进位的数值,在后面当做 bool 类型来使用,用来表示计算出来的数列是否满足题意
  11. len 用来存放接收到的字符串的长度, temp 用来存放每次将数列上的数值乘以 2 之后的数值
  12. counter 用来存放计算出来的数列的长度
  13. */
  14.     memset (appear , 0 , sizeof(appear)) ;

  15.     scanf("%s", num1) ;
  16.     len = strlen(num1) ;

  17.     for ( int i = len -1 ; i >= 0 ; i-- )
  18.     {
  19.         appear[num1[i] -'0']++ ;

  20.         temp = (int)(num1[i]-'0')*2 + more ; // temp 数值 = 数列当前位置 i 上的数值 *2 + 进位

  21.         num2[counter++] = (temp % 10)+'0' ;  
  22.         // 将 temp 数值与 10 取余,并将 int 数值修改为 char 类型
  23.     
  24.         more = temp / 10 ;
  25.         // more 变量用来存放每次的进位值
  26.          
  27.     }


  28.     if ( more != 0)   // 最后需要检查一下最后的进位是否为 0 , 如果不为零,将其置于 num2 数列的最高位
  29.         num2[counter++] = more + '0';
  30.     
  31.     more = 0 ; // means that all elements appears in the appear array

  32.     for ( int i = 0 ; i < counter ; i++ )
  33.     {
  34.          appear[num2[i]-'0']-- ; // if num2[i] -'0' not appear , appear[..] will < 0
  35.                  
  36.          if ( appear[num2[i]-'0'] < 0 )
  37.              more = 1 ;

  38.     }

  39.     if ( more == 1 )
  40.      printf("No\n") ;
  41.     else
  42.      printf ("Yes\n") ;
  43.           
  44.         for ( int j = counter-1 ; j >= 0 ; j-- )
  45.             printf("%c" , num2[j]) ;
  46.         
  47.     return 0 ;
  48. }
测试点全部通过
阅读(1111) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~