• Basic remains POJ


    题意 给出B(10以内大于0)进制下 p (1000位以内)和m(9位以内) 求 p%m 在b进制下等于什么

    思路: 可以计算   1e9不会溢出Int所以m在int值以内  先求m  要处理p  每次取一位模刚刚的m即可(b^n 也要不停模)

    坑点 : p可能等于0  这时候要输出0

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<vector>
     4 #include<cmath>
     5 #include<iostream>
     6 using namespace std;
     7 
     8 char s1[1005],s2[600];
     9 vector<int>q;
    10 int main(){
    11     int b,p,m;
    12     while(scanf("%d",&b)==1&&b){
    13         scanf("%s%s",s1,s2);
    14         int len1=strlen(s1);
    15         int len2=strlen(s2);
    16         long long  temp1=0,temp2=0;
    17         q.clear();
    18       //    cout<<s1<<" "<<s2<<endl;    
    19         int zz=1;
    20         for(int i=len2-1;i>=0;i--){
    21             temp2+=zz*(s2[i]-'0');
    22             zz*=b;
    23         }
    24         zz=1;
    25 
    26         for(int i=len1-1;i>=0;i--){
    27             temp1+=(zz*(s1[i]-'0'))%temp2;
    28         //    cout<<temp1<<endl;
    29             temp1%=temp2;
    30             zz*=b;
    31             zz%=temp2;
    32         }
    33         //cout<<temp1<<temp2<<endl;
    34         int ans=temp1%temp2;
    35         //cout<<ans<<endl;
    36         if(temp1==0){
    37             printf("0
    ");
    38             continue;
    39         }
    40         while(ans){
    41             q.push_back(ans%b);
    42             ans/=b;
    43         }
    44         for(int i=q.size()-1;i>=0;i--){
    45             printf("%d",q[i]);
    46         }
    47         printf("
    ");
    48 
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    [面试题]什么是面向对象编程
    面向对象编程的新手理解
    Object of type type is not JSON serializable
    STL---map
    STL---llist
    Div标签使用inline-block有间距
    STL---vector
    KMP算法
    算法06
    算法05
  • 原文地址:https://www.cnblogs.com/ttttttttrx/p/10279641.html
Copyright © 2020-2023  润新知