国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

菜鳥(niǎo)記錄:c語(yǔ)言實(shí)現(xiàn)PAT甲級(jí)1010--Radix

這篇具有很好參考價(jià)值的文章主要介紹了菜鳥(niǎo)記錄:c語(yǔ)言實(shí)現(xiàn)PAT甲級(jí)1010--Radix。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

很長(zhǎng)時(shí)間沒(méi)做,忙于考研和實(shí)習(xí),久違的的拾起了算法。做了很長(zhǎng)時(shí)間,其實(shí)總體思路還是很簡(jiǎn)單的,但滿(mǎn)分不知道為什么就是到不了,又因?yàn)榫W(wǎng)上很多答案包括柳神的都是c++,無(wú)法參透,姑且只能這樣了。

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的數(shù)字輸入是不方便用int數(shù)組的,因該用字符串來(lái)分個(gè)存儲(chǔ),這樣既方便又高效。另一方面,程序的整體流程就是:

  1. 輸入、存儲(chǔ)。
  2. 判斷tag,到這大概能寫(xiě)出main函數(shù),根據(jù)result變量確定輸出數(shù)字還是“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. 編寫(xiě)具體的轉(zhuǎn)換函數(shù),結(jié)果返回到result。

個(gè)人想法:

主題函數(shù)很好寫(xiě),而且不需要在意題目中提到的出現(xiàn)多個(gè)可轉(zhuǎn)換的進(jìn)制輸出最小進(jìn)制,當(dāng)你第一次遍歷得到正確進(jìn)制數(shù)時(shí)就可以直接輸出。

轉(zhuǎn)換進(jìn)制的函數(shù)Radix(char *tar,char *cha,int radix),tar數(shù)組直接按照radix寫(xiě)一個(gè)for循環(huán)轉(zhuǎn)換為二進(jìn)制,cha則多加一個(gè)for循環(huán)進(jìn)行多個(gè)進(jìn)制的遍歷,(這里注意的是,不是只到36就可以的,我相同的程序在只遍歷36次時(shí)只有19分,遍歷過(guò)多又會(huì)有超時(shí),最高在999999次時(shí)達(dá)到24分),轉(zhuǎn)換進(jìn)制的代碼就好寫(xiě)了。

 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 }

在多次調(diào)試時(shí)發(fā)現(xiàn)需要注意:

  1. 輸入N1和N2數(shù)組時(shí),?scanf("%s %s %d%d",&N1,&N2, &tag, &radix);%s后面必須有空格,這樣每個(gè)字符才會(huì)被分割到數(shù)組里面。
  2. 求和變量sum2與sum1不同,需寫(xiě)在for循環(huán)內(nèi),不然遍歷下一次時(shí)會(huì)sum2因?yàn)椴粫?huì)清0而不斷累加導(dǎo)致一直報(bào)錯(cuò)。
  3. sizeof()求出來(lái)整個(gè)數(shù)組的長(zhǎng)度,而strlen()求出有效長(zhǎng)度。eg:110存入a[10],sizeof(a)=10.strlen(a)=3
  4. ‘110’在數(shù)組里面的位置是0,1,2,機(jī)器看起來(lái)就是‘011’,反過(guò)來(lái)了,在求和時(shí)要反過(guò)來(lái)
  5. 輸入的數(shù)字大于當(dāng)前進(jìn)制是不可能的,所以直接退出或break就好(9和19行)

最后得到的整體代碼為:

菜鳥(niǎo)記錄:c語(yǔ)言實(shí)現(xiàn)PAT甲級(jí)1010--Radix菜鳥(niǎo)記錄:c語(yǔ)言實(shí)現(xiàn)PAT甲級(jí)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

結(jié)果:

菜鳥(niǎo)記錄:c語(yǔ)言實(shí)現(xiàn)PAT甲級(jí)1010--Radix

?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-844205.html

到了這里,關(guān)于菜鳥(niǎo)記錄:c語(yǔ)言實(shí)現(xiàn)PAT甲級(jí)1010--Radix的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 菜鳥(niǎo)記錄PAT甲級(jí)1003--Emergency

    菜鳥(niǎo)記錄PAT甲級(jí)1003--Emergency

    久違的PAT,由于考研408數(shù)據(jù)結(jié)構(gòu)中有一定需要,同時(shí)也是對(duì)先前所遺留的競(jìng)賽遺憾進(jìn)行一定彌補(bǔ) ,再次繼續(xù)PAT甲級(jí)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日
    瀏覽(97)
  • PAT 甲級(jí)考試【1003 Emergency】

    PAT 甲級(jí)考試【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日
    瀏覽(93)
  • PAT甲級(jí)圖論相關(guān)題目

    PAT甲級(jí)圖論相關(guān)題目

    PAT甲級(jí)圖論相關(guān)題目: 分?jǐn)?shù) 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日
    瀏覽(23)
  • 1083 List Grades (PAT甲級(jí))

    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日
    瀏覽(19)
  • pat甲級(jí) 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日
    瀏覽(23)
  • 1028 List Sorting (PAT甲級(jí))

    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日
    瀏覽(16)
  • 1021 Deepest Root (PAT甲級(jí))

    1021. Deepest Root (25)-PAT甲級(jí)真題(圖的遍歷,dfs,連通分量的個(gè)數(shù))_柳婼的博客-CSDN博客 柳婼的解法在這里,兩次dfs,還是挺好玩的。 我的解法比較暴力,就是先用并查集算連通分量(這個(gè)其實(shí)還是dfs來(lái)算會(huì)更方便),如果只有一個(gè)連通分量,那deepest root一定在僅有一條arc的

    2024年02月15日
    瀏覽(16)
  • 1114 Family Property (PAT甲級(jí))

    This time, you are supposed to help us collect the data for family-owned property. Given each person\\\'s family members, and the estate(房產(chǎn))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日
    瀏覽(19)
  • 1072 Gas Station (PAT甲級(jí))

    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日
    瀏覽(22)
  • 1111 Online Map (PAT甲級(jí))

    這道題我讀題不仔細(xì)導(dǎo)致踩了個(gè)大坑,一個(gè)測(cè)試點(diǎn)過(guò)不了卡了好幾個(gè)小時(shí):第二個(gè)dijkstra算法中,題目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我卻想當(dāng)然地認(rèn)為在fastest path is not unique的時(shí)候,判斷標(biāo)準(zhǔn)是最短距離…… Input our

    2024年02月07日
    瀏覽(16)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包