• hihoCoder编程练习赛70


    题目1 : 数位翻转

    时间限制:20000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    给定一个数 n,你可以进行若干次操作,每次操作可以翻转 n 的二进制表示下的某一位,即将 0 变成 1,1 变成 0

    现在小 Hi 想知道,至少需要多少次操作,才能将 n 变成 n-1

    输入

    一个正整数 n

    1 ≤ n ≤ 109

    输出

    输出最少的操作次数

    样例输入
    10
    样例输出
    2
     1 // 2018-07-29
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #define ll long long 
     7 
     8 using namespace std;
     9 
    10 int main()
    11 {
    12     ll n;
    13     while(cin>>n){
    14         ll ans = n^(n-1);
    15         int cnt = 0;
    16         while(ans){
    17             if(ans&1LL){
    18                 cnt++;
    19             }
    20             ans>>=1;
    21         }
    22         cout<<cnt<<endl;
    23     }
    24 
    25 
    26     return 0;
    27 }
    View Code

    题目2 : 最短公共子序列

    时间限制:20000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    给定一个 01 串 A,你需要找一个和它等长的01串 B,使得 A 和 B 的最长公共子序列最短

    为了方便,你不需要输出 B,你只需要输出 A 和 B 的最长公共子序列的长度

    输入

    第一行一个 01 串 A

    1 ≤ |A| ≤ 105

    输出

    输出最短的长度

    样例输入
    000111
    样例输出
    3
     1 // 2018-07-29
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 string str;
    10 
    11 int main()
    12 {
    13     cin>>str;
    14     int len = str.length();
    15     int zero = 0, one = 0;
    16     for(int i = 0; i < len; i++)
    17       if(str[i] == '0')zero++;
    18       else one++;
    19     cout<<min(zero, one)<<endl;
    20 
    21     return 0;
    22 }
    View Code

    题目3 : 拼三角形

    时间限制:20000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    给定 n 根木棍,第 i 根长度为 ai

    现在你想用他们拼成尽量多的面积大于 0 的三角形,要求每根木棍只能被用一次,且不能折断

    请你求出最多能拼出几个

    输入

    第一行一个正整数 n

    第二行 n 个正整数 a1 … an

    1 ≤ n ≤ 15

    1 ≤ ai ≤ 109

    输出

    输出最多能拼出几个三角形

    样例输入
    6
    2 2 3 4 5 6
    样例输出
    2
     1 // 2018-07-29
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #define ll long long 
     7 
     8 using namespace std;
     9 
    10 ll arr[20];
    11 int n, ans;
    12 bool vis[20];
    13 
    14 void dfs(int step){
    15     for(int i = 1; i <= n; i++){
    16         for(int j = i+1; j <= n; j++){
    17             for(int k = j+1; k <= n; k++){
    18                 if(!vis[i] && !vis[j] && !vis[k] && arr[i]+arr[j]>arr[k]){
    19                     vis[i] = vis[j] = vis[k] = true;
    20                     ans = max(ans, step);
    21                     dfs(step+1);
    22                     vis[i] = vis[j] = vis[k] = false;
    23                 }
    24             }
    25         }
    26     }
    27     return;
    28 }
    29 
    30 int main()
    31 {
    32     cin>>n;
    33     for(int i = 1; i <= n; i++)
    34       cin>>arr[i];
    35     ans = 0;
    36     sort(arr+1, arr+1+n);
    37     memset(vis, 0, sizeof(vis));
    38     dfs(1);
    39     cout<<ans<<endl;
    40 
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    MySQL四种分区类型
    CentOS下升级MySQL 5.0.* 到5.5
    CentOS5.5使用yum来安装LAMP
    mysql-bin 常见操作
    引爆你的Javascript代码进化
    python读写excel的简单方法demo
    python时间戳数字转为字符串格式表达
    Djang——CSRF verification failed. Request aborted
    Apache部署django
    Qt设置windows系统时间
  • 原文地址:https://www.cnblogs.com/Penn000/p/9385921.html
Copyright © 2020-2023  润新知