牛客-计算机考研复试1

KY15 abc

abc_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=O83Ahttps://www.nowcoder.com/practice/912b15e237ef44148e44018d7b8750b6?tpId=40&tqId=21346&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan&difficulty=1&judgeStatus=&tags=/question-ranking

描述

设a、b、c均是0到9之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。

输入描述:

题目没有任何输入。

输出描述:

请输出所有满足题目条件的a、b、c的值。 a、b、c之间用空格隔开。 每个输出占一行。

穷举法即可解决。记得首位不为0.

#include <iostream>
using namespace std;

int main() {
    int i,j,k;
    for(i=1;i<=9;i++){
        for(j=1;j<=9;j++){
            for(k=0;k<=9;k++){
                if(i*100+j*110+k*12==532){
                    cout<<i<<' '<<j<<' '<<k<<endl;
                }
            }
        }
    }
}

KY45 skew数

skew数_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=O83Ahttps://www.nowcoder.com/practice/5928127cc6604129923346e955e75984?tpId=40&tags=&title=&difficulty=1&judgeStatus=0&rp=1&sourceUrl=

描述

在 skew binary 表示中,第 k 位的值 x[k] 表示 x[k]×(2^(k+1)-1)。每个位上的可能数字是 0 或 1,最后面一个非零位可以是 2,例如,10120(skew) = 1×(2^5-1) + 0×(2^4-1) + 1×(2^3-1) + 2×(2^2-1) + 0×(2^1-1) = 31 + 0 + 7 + 6 + 0 = 44。前十个 skew 数是 0、1、2、10、11、12、20、100、101、以及 102。

输入描述:

输入包括多组数据,每组数据包含一个 skew 数。

输出描述:

对应每一组数据,输出相应的十进制形式。结果不超过 2^31-1。

示例1

输入:

10120
200000000000000000000000000000
10
1000000000000000000000000000000
11
100
11111000001110000101101102000

输出:

44
2147483646
3
2147483647
4
7
1041110737

循环+字符串。

通过s.size()-i来控制是在第几位。

#include <cmath>
#include <iostream>
using namespace std;

int main() {
    string s;
    while(cin>>s){
        int sum=0;
        for(int i=0;i<s.size();i++){
            sum+=(s[i]-'0')*(pow(2,s.size()-i)-1);
        }
        cout<<sum<<endl;
    }
}

KY59 神奇的口袋

神奇的口袋_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=O83Ahttps://www.nowcoder.com/practice/9aaea0b82623466a8b29a9f1a00b5d35?tpId=40&tags=&title=&difficulty=1&judgeStatus=0&rp=1&sourceUrl=

描述

有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40。John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an。John可以从这些物品中选择一些,如果选出的物体的总体积是40,那么利用这个神奇的口袋,John就可以得到这些物品。现在的问题是,John有多少种不同的选择物品的方式。

输入描述:

输入的第一行是正整数n (1 <= n <= 20),表示不同的物品的数目。接下来的n行,每行有一个1到40之间的正整数,分别给出a1,a2……an的值。

输出描述:

输出不同的选择物品的方式的数目。

示例1

输入:

3
20
20
20

输出:

3

动态规划问题。

常考问题。

初始化+递归+全局变量。

经典套路为,计算有多少种可能,首先将n种可能变为全局变量。然后进行初始化。这里的问题就是,这个物品要不要拿出,如果拿出小于40,就拿出,不然就跳过。大于那就不拿,考虑下一个。等于那就可能性加一。

#include <iostream>
#include <vector>
using namespace std;
const int maxn=21;
int a[maxn];
int n,count;
void dfs(int now,int i){
    for(int j=i;j<n;j++){
        int sum=now+a[j];
        if(sum>40){
            dfs(now,j+1);
        }else if(sum<40){
            dfs(sum,j+1);
        }else {
            count++;
        }
    }
}
int main() {
    while(cin>>n){
        count=0;
        int i;
        for(i=0;i<n;i++){
            cin>>a[i];
        }
        dfs(0,0);  
        cout<<count; 
    }
    
}

KY72 Digital Roots

Digital Roots_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=O83Ahttps://www.nowcoder.com/practice/cef727d0af33479c9fb4a9c120702414?tpId=40&tags=&title=&difficulty=1&judgeStatus=0&rp=1&sourceUrl=

描述

    The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.     For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

输入描述:

    The input file will contain a list of positive integers, one per line. The integer may consist of a large number of digits. (1 <= input value <= 10^9)

输出描述:

    For each integer in the input, output its digital root on a separate line of the output.

示例1

输入:

24
39

输出:

6
3

本题就是将各个位置的数加起来得到一个个位数,如果不是个位数将得到的和的各个位置的数加起来。

#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin>>n) {
        int sum=n;
        while (sum/10!=0) {
            int ans=0,t=sum;
            while(t!=0){
                ans+=t%10;
                t=t/10;
            }
            sum=ans;
        }
        cout<<sum<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值