题目
难度等级:6(1-8 难到易)
Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p
In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.
Note: n and p will always be given as strictly positive integers.
digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * kdigPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
复制代码
汉译
有些数字具有有趣的特性。例如:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
给定一个正整数 n,写成 abcd...(a, b, c, d... 是数字)和一个正整数 p
换句话说:
是否存在整数 k 例如: (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
如果是这种情况,我们将返回 k,如果不是,则返回 -1。
注意:n 和 p 将始终作为严格的正整数给出。
digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * kdigPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688
复制代码
代码区
class DigPow{public: static int digPow(int n, int p);};
复制代码
int digPow(int n, int p) { // your code}
复制代码
解答
CPP(c++解法)
#include <cmath>using namespace std;class DigPow{public: static int digPow(int n, int p){ long long sum=0; for(char digit : to_string(n)){ sum+=pow(digit-'0',p++); } return (sum/n)*n==sum ? sum/n : -1; }};
复制代码
#include <string>#include <cmath>
class DigPow{public: static int digPow(int n, int p);};
int DigPow::digPow(int n, int p){ long long s = 0; std::string nstr = std::to_string(n); for (unsigned int i = 0; i < nstr.length(); i++) s += static_cast<long long>(std::pow(static_cast<int>(nstr[i] - '0'), p + i)); if (s % n == 0) return s / n; else return -1;}
复制代码
#include <string>#include <cmath>using namespace std;
class DigPow{public: static int digPow(int n, int p) { string num = to_string(n); int a{0}; for(char ch : num ) { int i = ch - '0'; a += pow(i, p); ++p; } return (( a%n == 0) ? a/n : -1); }};
复制代码
C(c 语言解法)
int digPow(int n, int p) { int numDigits = floor(log10(n))+1; int result = 0; int num = n; for (int i = p + numDigits - 1; i >= p; i--) { result += pow(num%10, i); num/=10; } if (result % n == 0) { return result / n; } return -1;}
复制代码
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>
int digPow(int n, int p) { long long sum = 0; char* s = malloc(20); sprintf(s, "%d", n); for(int i = 0; i < strlen(s); i++) { sum += pow(s[i] - '0', p + i); } return (sum / n) * n == sum ? sum / n : -1;}
复制代码
int digPow(int n, int p) { p += (int)log10(n); unsigned int val = n; unsigned int sum = 0; while(val > 0) { sum += pow(val % 10, p--); val /= 10; } return sum % n == 0 ? sum / n : -1;}
复制代码
D(我的解法)
#include<iostream>#include <string> #include<math.h>using namespace std;
int digPow(int n, int p){ string intStr = to_string(n); long sum = 0; for (int i = 0; i < intStr.length(); ++i, ++p) { sum += pow(intStr[i] - '0', p); } return (sum % n == 0) ? sum / n : -1;}
int main(){ int n , p; cin >> n; cin >> p; cout<<digPow(n, p); system("pause"); return 0;}
复制代码
PS(知识补充)
pow()
使用说明:实现次方运算^
头文件:#include<math.h>
使用方法:pow(a,b)
C++中 int 和 string 的互相转换
一、用 sstream 类
1. int -> string
#include<iostream>#include<sstream> //需要引用的头文件using namespace std;
int main(){ int x = 1234; //需要转换的数字 stringstream sstr; string str;
sstr<<x; str = sstr.str(); //转换后的字符串 cout << str <<endl; return 0;}
复制代码
2. string -> int
#include<iostream>#include<sstream> //需要引用的头文件using namespace std;
int main(){ int x; string str = "4321"; //需要转换的字符串 stringstream sstr(str);
sstr >> x; //转换后的数字 cout << x << endl;}
复制代码
缺点:处理大量数据时候速度慢;stringstream 不会主动释放内存。
二、用 sprintf、sscanf 函数
1. int -> string
#include<iostream>using namespace std;
int main(){ int x = 1234; //需要转换的数字 string str; char ch[5]; //需要定义的字符串数组:容量等于数字长度+1即可
sprintf(ch,"%d", x); str = ch; //转换后的字符串 cout << str << endl;}
复制代码
2. string -> int、float
#include<iostream>using namespace std;
int main(){ char ch[10] = "12.34"; //需要转换的字符串 int x; //转换后的int型 float f; //转换后的float型
sscanf(ch, "%d", &x); //转换到int过程 sscanf(ch, "%f", &f); //转换到float过程
cout << x << endl; cout << f << endl;}
复制代码
三、C 标准库 atoi, atof, atol, atoll(C++11 标准) 函数
可以将字符串转换成 int,double, long, long long 型
1. int -> string
itoa 函数:定义:char *itoa(int value, char *string, int radix);参数:① value:需要转换的 int 型② string:转换后的字符串,为字符串数组③ radix:进制,范围 2-36
2. string -> int、double、long、long long
atoi 函数:定义:int atoi(const char *nptr);double atof(const char *nptr);long atol(const char *nptr);long long atoll(const char *nptr);参数:① nptr:字符串数组首地址
#include<iostream>#include<stdlib.h> //需要引用的头文件using namespace std;
int main(){ int x; char ch[] = "4321"; //需要转换的字符串 x = atoi(ch); //转换后的int数字
cout << x << endl;}
复制代码
C++中 int 与 char 相互转换
一、ASCII 表
了解 int 与 char 相互转换之前,先让我们看一下 ASCII 表。
其中数字字符对应的位置为:48(0) - 57(9)。
二、char 转 int
char 转 int 之前,先将运算式中的每个字符都转换成 ASCII 码值,再进行计算。以下代码为例,其中 i3 的结果符合我们的预期要求。
char c = '0';
int i1 = c; // 48int i2 = c - 0; // 48int i3 = c - '0'; // 0int i4 = c + '0'; // 96
复制代码
三、int 转 char
int 转 char 之前,先将运算式中的每个字符都转换成 ASCII 码值,再进行计算。计算出数值后,再据此转换为字符(数值为该字符对应的 ASCII 码值)。以下代码为例,其中 c4 的结果符合我们的预期要求。
int i = 5;
char c1 = i; // 越界char c2 = i - 0; // 越界char c3 = i - '0'; // 越界char c4 = i + '0'; // 5
复制代码
评论