E. The shortest problem

本文详细解析了一个C++编程问题,题目要求编写一个程序,输入一个整数n,计算其各个位上的数字之和,并将这个和转换为一个新整数x,最后判断x是否能被11整除。通过使用循环和条件判断,实现了解题逻辑。文章提供了一个简洁高效的解决方案,并附带了代码示例。

http://www.bnuoj.com/v3/contest_show.php?cid=7612#info

http://www.bnuoj.com/v3/contest_show.php?cid=7612#problem/

题解:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-7-solutions-by-uestc/

水题:

#include <iostream>
#include<stdio.h>
using namespace std;
int sum,x;
void get_sum(int n)
{
    while(n)
    {
        sum += n%10;
        n /= 10;
    }
}
void get_x()
{
    int times = 1;
    int s = sum;
    while(s)
    {
        s /= 10;
        times *= 10;
    }
    x = (x * times)+sum;
    x = x%11;
}
int main()
{
    int n,t;
    int cas = 1;
    while(cin>>n>>t && (n!=-1||t!=-1))
    {
        sum = 0;
        x = 0;
        x = n%11;
        get_sum(n);
        for(int i = 1;i <= t;i ++){
                get_x();
                get_sum(sum);
        }
        if(x == 0)
            printf("Case #%d: Yes\n",cas++);
        else if(x)
            printf("Case #%d: No\n",cas++);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zhangjialu2015/p/5268121.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值