zoukankan      html  css  js  c++  java
  • codeforces 962C Make a Square

    题目链接:http://codeforces.com/contest/962/problem/C

    题意:给你一个数字n (n<=2e9)没有前导0。现在需要你删除一些数字,也可以不删除,使得剩下的数是平方数。删除后得到的数字不存在前导0。输出删除最少的位数,不存在输出-1.

    分析:看到n的范围。一共最多也就10位,直接暴力枚举每一种删除情况,然后再判断每种情况是否为平方数,取min就可以了。假设数字一共有cnt位,那么用1到2^cnt-1来代表每种删除情况。当前位为1代表保留,为0代表删除,然后标记位数,求和得到删除后的数,判平方数就可以了。删除位数取min。

    AC代码:

     1 #include<bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int b[15];
     6 long long qw[15];
     7 long long n,d;
     8 int cnt;
     9 int p=100;
    10 int main(){
    11     ios_base::sync_with_stdio(0);
    12     cin.tie(0);
    13     qw[0]=1;
    14     for(int i=1;i<=10;i++){
    15         qw[i]=qw[i-1]*10;
    16     }
    17     cin>>n;
    18     d=n;
    19     cnt=0;
    20     while(d){
    21         b[cnt]=d%10;
    22         d/=10;
    23         cnt++;
    24     }
    25     int up=pow(2,cnt);
    26     for(int i=1;i<up;i++){
    27         int ans=0;
    28         long long sum=0;
    29         for(int j=0;j<cnt;j++){
    30             if(1&(i>>j)){
    31                 sum+=b[j]*qw[ans];
    32                 ans++;
    33             }
    34         }
    35         long long sb=sqrt(sum);
    36         if(sb*sb==sum&&sum>=qw[ans-1]){
    37             p=min(p,cnt-ans);
    38         }
    39     }
    40     if(p==100){
    41         cout<<-1<<endl;
    42     }
    43     else cout<<p<<endl;
    44 return 0;
    45 }
    View Code
  • 相关阅读:
    DP2_第K优解
    DP1
    算法第一讲_动态规划
    CodeForces 487A Fight the Monster
    HDU 2899 Strange fuction
    lower_bound() 与 upper_bound()
    jQuery qrcode生成二维码
    本地代码上传至git仓库
    图片懒加载
    react-native学习(一)————使用react-native-tab-navigator创建底部导航
  • 原文地址:https://www.cnblogs.com/ls961006/p/8799250.html
Copyright © 2011-2022 走看看