CF 460B Little Dima and Equation

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccutsoft20144264/article/details/49515275

题目链接:http://codeforces.com/problemset/problem/460/B

题意:

输入a,b,c

0 < x < 109

x = b·s(x)a + c

s(x)为x每位数字的总和

求符合条件的x的个数,并输出它们


思路:

由x的取值范围可知,s(x)的取值范围为[1,81]

由此只需要遍历1~81,求得x

再将x转化为string型

算出x每位数字的总和再与s(x)比较是否相等即可


code:

#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<iostream>
#include<sstream>
#define maxn 1000000000
using namespace std;
long long poww(int x,int y)
{
long long sum=1,i;
for(i=1;i<=y;i++)
sum*=x;
return sum;
}
int main()
{
int cnt=0,a,b,c,i,j,sum;
long long num;
vector<string> ve;
string str;
stringstream ss;
scanf("%d %d %d",&a,&b,&c);
for(i=1;i<=81;i++)
{
str.clear();
ss.clear();
num=b*poww(i,a)+c;
if(num<0||num>maxn)
continue;
ss<<num;
ss>>str;
for(j=0,sum=0;j<str.length();j++)
sum+=str[j]-'0';
if(sum==i)
{
ve.push_back(str);
cnt++;
}
}
cout<<cnt<<endl;
for(vector<string>::iterator it=ve.begin();it!=ve.end();it++)
{
cout<<*it;
if(it!=ve.end()-1)
cout<<" ";
}
puts("");
return 0;
}


(这次终于体会到为什么会有不少人吐槽CSDN的编辑器了。。)


代码中用到了stringstream对象进行类型转换
它的头文件为#include<sstream>
个人感觉比于sprintf,sscanf什么的好用多了
它的用法有很多
在此不做介绍了
贴一个网址:http://www.cppblog.com/Sandywin/archive/2007/07/13/27984.html
里面对stringstream的用法有很详细的介绍

猜你喜欢

转载自blog.csdn.net/ccutsoft20144264/article/details/49515275
今日推荐