菜鸟记录:c语言实现PAT甲级1010--Radix

这篇具有很好参考价值的文章主要介绍了菜鸟记录:c语言实现PAT甲级1010--Radix。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

很长时间没做,忙于考研和实习,久违的的拾起了算法。做了很长时间,其实总体思路还是很简单的,但满分不知道为什么就是到不了,又因为网上很多答案包括柳神的都是c++,无法参透,姑且只能这样了。

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:


N1 N2 tag radix

Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set { 0-9, a-z } where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number radix is the radix of N1 if tag is 1, or of N2 if tag is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print Impossible. If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

题目分析:

一方面,N1和N2的数字输入是不方便用int数组的,因该用字符串来分个存储,这样既方便又高效。另一方面,程序的整体流程就是:

  1. 输入、存储。
  2. 判断tag,到这大概能写出main函数,根据result变量确定输出数字还是“impossible”
    int main() {
         int result;
         scanf("%s %s %d%d",&N1,&N2, &tag, &radix);
             if (tag == 1)
             {
                 result = Radix(N1, N2,radix );
             }
             else if(tag==2)
             {
                 result = Radix(N2,N1,radix);
             }
             else
             {
                 result = 0;
             }
    
         if (result != 0)
             printf("%d", result);
         else
             printf("Impossible");
        return 0;
     }  
    
  3. 编写具体的转换函数,结果返回到result。

个人想法:

主题函数很好写,而且不需要在意题目中提到的出现多个可转换的进制输出最小进制,当你第一次遍历得到正确进制数时就可以直接输出。

转换进制的函数Radix(char *tar,char *cha,int radix),tar数组直接按照radix写一个for循环转换为二进制,cha则多加一个for循环进行多个进制的遍历,(这里注意的是,不是只到36就可以的,我相同的程序在只遍历36次时只有19分,遍历过多又会有超时,最高在999999次时达到24分),转换进制的代码就好写了。

 1 int Radix(char *tar,char *cha,int radix) {
 2     double sum1 = 0;
 3     for (int i = strlen(tar)-1; i >=0; i--)
 4     {
 5         double tmp;
 6         tmp = tar[i];
 7         if (tmp > '9')tmp = tmp - 'a' + 10;//a-z
 8         else if (tmp < 'a'&&tmp>='0')tmp -= '0';//0-9
 9         if (tmp >= radix) return 0;
10         sum1 += tmp * pow(radix,strlen(tar)-i-1);
11     }
12     for (int i = 0; i <= 999999;i++) {
13         double sum2 = 0;
14         for (int j = strlen(cha) - 1; j >= 0; j--) {
15             double tmp;
16             tmp = cha[j];
17             if (tmp > '9')tmp =tmp- 'a' + 10;//a-z
18             else if (tmp < 'a' && tmp >= '0')tmp -= '0';//0-9
19             if (tmp >= i) break;
20             sum2 += tmp * pow(i, strlen(cha) - j - 1);
21         }
22 
23         if (sum1 == sum2)return i;
24     }
25     return 0;
26 }

在多次调试时发现需要注意:

  1. 输入N1和N2数组时, scanf("%s %s %d%d",&N1,&N2, &tag, &radix);%s后面必须有空格,这样每个字符才会被分割到数组里面。
  2. 求和变量sum2与sum1不同,需写在for循环内,不然遍历下一次时会sum2因为不会清0而不断累加导致一直报错。
  3. sizeof()求出来整个数组的长度,而strlen()求出有效长度。eg:110存入a[10],sizeof(a)=10.strlen(a)=3
  4. ‘110’在数组里面的位置是0,1,2,机器看起来就是‘011’,反过来了,在求和时要反过来
  5. 输入的数字大于当前进制是不可能的,所以直接退出或break就好(9和19行)

最后得到的整体代码为:

菜鸟记录:c语言实现PAT甲级1010--Radix菜鸟记录:c语言实现PAT甲级1010--Radix
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #define _CRT_SECURE_NO_WARNINGS
 5 char N1[10],  N2[10];
 6 int tag, radix;
 7 int Radix(char* tar, char* cha, int radix);
 8  int main() {
 9      int result;
10      
11      scanf("%s %s %d%d",&N1,&N2, &tag, &radix);
12          if (tag == 1)
13          {
14              result = Radix(N1, N2,radix );
15          }
16          else if(tag==2)
17          {
18              result = Radix(N2,N1,radix);
19          }
20          else
21          {
22              result = 0;
23          }
24 
25      if (result != 0)
26          printf("%d", result);
27      else
28          printf("Impossible");
29     return 0;
30  }
31 int Radix(char *tar,char *cha,int radix) {
32     double sum1 = 0;
33     for (int i = strlen(tar)-1; i >=0; i--)
34     {
35         double tmp;
36         tmp = tar[i];
37         if (tmp > '9')tmp = tmp - 'a' + 10;//a-z
38         else if (tmp < 'a'&&tmp>='0')tmp -= '0';//0-9
39         if (tmp >= radix) return 0;
40         sum1 += tmp * pow(radix,strlen(tar)-i-1);
41     }
42     for (int i = 0; i <= 999999;i++) {
43         double sum2 = 0;
44         for (int j = strlen(cha) - 1; j >= 0; j--) {
45             double tmp;
46             tmp = cha[j];
47             if (tmp > '9')tmp =tmp- 'a' + 10;//a-z
48             else if (tmp < 'a' && tmp >= '0')tmp -= '0';//0-9
49             if (tmp >= i) break;
50             sum2 += tmp * pow(i, strlen(cha) - j - 1);
51         }
52 
53         if (sum1 == sum2)return i;
54     }
55     return 0;
56 }
View Code

结果:

菜鸟记录:c语言实现PAT甲级1010--Radix

 文章来源地址https://www.toymoban.com/news/detail-844205.html

到了这里,关于菜鸟记录:c语言实现PAT甲级1010--Radix的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 菜鸟记录PAT甲级1003--Emergency

    久违的PAT,由于考研408数据结构中有一定需要,同时也是对先前所遗留的竞赛遗憾进行一定弥补 ,再次继续PAT甲级1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    浏览(45)
  • PAT 甲级考试【1003 Emergency】

    题目: As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead

    2024年02月08日
    浏览(49)
  • 1021 Deepest Root (PAT甲级)

    1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)_柳婼的博客-CSDN博客 柳婼的解法在这里,两次dfs,还是挺好玩的。 我的解法比较暴力,就是先用并查集算连通分量(这个其实还是dfs来算会更方便),如果只有一个连通分量,那deepest root一定在仅有一条arc的

    2024年02月15日
    浏览(34)
  • 1114 Family Property (PAT甲级)

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate. Input Specification: Each input file contains one test case. For each case, the fir

    2024年02月06日
    浏览(51)
  • PAT甲级图论相关题目

    PAT甲级图论相关题目: 分数 25 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some o

    2024年01月21日
    浏览(53)
  • 1028 List Sorting (PAT甲级)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, eac

    2024年02月15日
    浏览(41)
  • 1072 Gas Station (PAT甲级)

    A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range. Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendatio

    2024年02月09日
    浏览(42)
  • 1111 Online Map (PAT甲级)

    这道题我读题不仔细导致踩了个大坑,一个测试点过不了卡了好几个小时:第二个dijkstra算法中,题目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我却想当然地认为在fastest path is not unique的时候,判断标准是最短距离…… Input our

    2024年02月07日
    浏览(42)
  • pat甲级 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    浏览(35)
  • 1083 List Grades (PAT甲级)

    Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. Input Specification: Each input file contains one test case. Each case is given in the following format: where  name[i]  and  ID[i

    2024年02月08日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包