首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C/CPP每日一题:Playing with digits

C/CPP每日一题:Playing with digits

作者头像
CtrlX
发布2022-09-23 15:33:59
3360
发布2022-09-23 15:33:59
举报
文章被收录于专栏:C++核心编程C++核心编程

题目

难度等级: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

  • we want to find a positive integer k, if it exists, such that the sum of the digits of n taken to the successive powers of p is equal to k * n.

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 * 1
digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
digPow(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,如果它存在的话,使得 n 的数字之和对 p 的连续幂等于 k n。

换句话说:

是否存在整数 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 * 1
digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
digPow(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

(没run起来,一直报错,随后再补)
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表。

0
0

其中数字字符对应的位置为:48(0) - 57(9)。

二、char转int

char转int之前,先将运算式中的每个字符都转换成ASCII码值,再进行计算。 以下代码为例,其中i3的结果符合我们的预期要求。

char c = '0';

int i1 = c;                    // 48
int i2 = c - 0;                // 48
int i3 = c - '0';              // 0
int 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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
    • 汉译
      • 代码区
      • 解答
        • CPP(c++解法)
          • C(c语言解法)
            • D(我的解法)
              • pow()
              • C++中int和string的互相转换
              • C++中int与char相互转换
          • PS(知识补充)
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档