pku1331 multiply(use strtol 数字字符串转数字)

本文详细介绍了strtol函数的功能及参数,包括如何将不同进制的字符串转换为十进制整数,并通过一个具体的编程实例展示了其在解决数学问题中的应用。此外,还提供了与strtol相关的其他字符串转换函数,如strtoul和strtod。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

long int strtol ( const char * str, char ** endptr, int base ) <stdlib.h>

Parameters

str
C string containing the representation of an integral number.
endptr
Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.

Return Value

If the base value is between 2 and 36, the format expected for the integral number is a succession of the valid digits and/or letters needed to represent integers of the specified radix (starting from '0' and up to 'z'/ 'Z' for radix 36). The sequence may optionally be preceded by a plus or minus sign and, if base is 16, an optional "0x" or "0X" prefix.
相关函数
atol Convert string to long integer (function)
strtoul Convert string to unsigned long integer (function)
strtod Convert string to double (function)
  1. /*pku1331
  2.   Name: Multiply
  3.   Date: 24-07-08 19:47
  4.   Description: 题目给出q p r 其中p*q==r 在某种进制中,求最小的进制
  5.   strtol:用stdlib头文件,avg1是数字字符串,avg2是返回的转化后的字符串,avg3是数字字符串的进制
  6.     返回:十进制数 
  7. */
  8. #include<iostream>
  9. using namespace std; 
  10. #include<stdio.h>
  11. #include<stdlib.h>
  12. #define pr printf
  13. int main(){
  14.     char a[20],b[20],c[20];
  15.     int t,m;
  16.     int x,y,z,i;
  17.     scanf("%d",&t);
  18.     while(t--){
  19.         scanf("%s%s%s",a,b,c);   
  20.         m=0;
  21.         i=0;
  22.         while(a[i]!='/0'){if(m<a[i]-'0')m=a[i]-'0';i++;}
  23.            i=0;
  24.         while(b[i]!='/0'){if(m<b[i]-'0')m=b[i]-'0';i++;}
  25.             i=0;
  26.         while(c[i]!='/0'){if(m<c[i]-'0')m=c[i]-'0';i++;}
  27.         while(m++<17){
  28.                x=strtol(a,NULL,m);
  29.                y=strtol(b,NULL,m);
  30.                z=strtol(c,NULL,m);//由m进制转化成10进制 
  31.                 if(z/y==x && z%y==0)break
  32.             
  33.             }
  34.         if(m<17)pr("%d/n",m);else pr("0/n");    
  35.     }
  36. }
  37. /*
  38. 3
  39. 6 9 42
  40. 11 11 121
  41. */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值