zoukankan      html  css  js  c++  java
  • codeforces 758D

    D. Ability To Convert
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1·162 + 13·161 + 11·160). Alexander lived calmly until he tried to convert the number back to the decimal number system.

    Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k.

    Input

    The first line contains the integer n (2 ≤ n ≤ 109). The second line contains the integer k (0 ≤ k < 1060), it is guaranteed that the number kcontains no more than 60 symbols. All digits in the second line are strictly less than n.

    Alexander guarantees that the answer exists and does not exceed 1018.

    The number k doesn't contain leading zeros.

    Output

    Print the number x (0 ≤ x ≤ 1018) — the answer to the problem.

    Examples
    input
    13
    12
    output
    12
    input
    16
    11311
    output
    475
    input
    20
    999
    output
    3789
    input
    17
    2016
    output
    594
    Note

    In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·130 or 15 = 1·131 + 2·130.

    这题的题意是第一行输入一个进制,第二行输入该进制下的数,输出将该数转化为十进制的最小值。

    ( 有零的地方一直都处理不好- -,后来看了别人的代码才学会。

    思路是用字符串读入,从后往前加,如果大于输入的进制了,就跳出再判断含有0的情况,直到循环到起始。

     1 #include <iostream>
     2 #define ll long long
     3 using namespace std;
     4 string s;
     5 int main()
     6 {
     7     ll b;
     8     cin>>b>>s;
     9     ll ans = 0;
    10     ll p = 1;
    11     ll len = s.length() - 1;
    12     while(len >= 0) {
    13         ll sum = 0;
    14         ll t = 1;
    15         ll i;
    16         for( i = len; i >= 0; i--) {
    17             if(max((s[i] - '0'),1) * t + sum >= b)
    18             break;
    19             sum += (s[i] - '0') * t;
    20             t *= 10;
    21      //       cout<<sum<<endl;
    22         }
    23         while(i < len - 1) {     //每位至少有一个数,所以每次大的循环至少会让最后一位数
    24             if(s[i+1] == '0') {  //加入ans中,因此遍历到倒数第二位之前就行了
    25                 i++;
    26             }
    27             else break;
    28         }
    29         ans += sum * p;
    30     //    cout<<ans<<endl;
    31         p *= b;
    32         len = i;
    33     }
    34     cout << ans << endl;
    35     return 0;
    36 }
    View Code

     

  • 相关阅读:
    Spring源码研究:数据绑定
    Spring
    九、IntelliJ IDEA 编译方式介绍及编译器的设置和选择
    八、IntelliJ IDEA 缓存和索引的介绍及清理方法
    七、IntelliJ IDEA 常见文件类型的图标介绍
    六、修改 IntelliJ IDEA 模板注释中的 user 内容
    五、设置 IntelliJ IDEA 主题和字体的方法
    四、IntelliJ IDEA 之 HelloWorld 项目创建及相关配置文件介绍
    三、详述 IntelliJ IDEA 的使用界面
    二、IntelliJ IDEA 安装目录的核心文件讲解
  • 原文地址:https://www.cnblogs.com/zmin/p/7507129.html
Copyright © 2011-2022 走看看