题目来源:http://lx.lanqiao.cn/problem.page?gpid=T51
给定n个十六进制正整数,输出它们对应的八进制数。
输入格式
输入的第一行为一个正整数n (1<=n<=10)。
接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
输出n行,每行为输入对应的八进制正整数。
【注意】
输入的十六进制数不会有前导0,比如012A。
输出的八进制数也不能有前导0。
2
39
123ABC
71
4435274
我的答案
import java.math.BigInteger;
import java.util.Scanner;
class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[] nums = new String[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.next();
}
for (int i = 0; i < n; i++) {
BigInteger bigInteger = new BigInteger(nums[i],16);
System.out.println(bigInteger.toString(8));
}
}
}
在这个位置为什么用的是BigInteger,而不是Integer。
主要是Integer能保存的位数有点小。
C9DAB2B36C这个16进制就不可以变成Integer。
更何况别人给的测试数据是这样的
F9F0DD6DDD0A59E241DC1ED720287896286F5CC3ADDF6C1ADF6ED35F477B0022981E5E1FBFE1BFB8E26B5BA93253275BF6A44B3FA1051CDFE3B3F5D2725A9A580FD5B04525B3182FCD2B3FDA124ACA3C901406A2B55CD8B95D48D13E379F1CCBCDFC39FEE4ACC5523AA0BDEF57E63A1F81CBABA9F45CAAED48D06BFB3D16836042BED57CAC84761BFEB59A0C81304908BB781E4BBDF230D2E977374B97BD0B6B7D38B736428826A0F2729BE2290256DC304E875C9D4B3FB2125AE3D0CD3130D6114989517ACA97DAA2485181EB31C07D2C6A5BCC587E048A6D2BEACD6FE206F225C708461B41FDB5AD087C5DC4FCAEEC3A3437A42E51B065D6E4332F71B109D3317681AB0FCBF31C9F1C23BA46B4F983AF9214D13AC3DDF6C03F3E9854C4D47741A5576812BE0B5CB8BF647B930687EC881DF76191F9C468C1736EEF1E59635EB6CBD2C73B00068C8FFEDEDEC2826D114DC1F8824924FA079056E25A5DDBAECEB90A18C51F919A83AE980E25BF06DD486427DFDEE6F708AA642625CE4C4298AFDD7AC48AA81B0C5608C2D801543EDEEAB5479C1342E4E4AEC719F1A46E33C1D761EFDF22116051E18FA714C72FAE0756835527748E4DF2E728E3DAAF94E9B3F37489C0D4E12D40469296D35673027F5E4D70D54B668A7C00F96CCB23F2562A468B650A1719186285BE1893B861E8B2E3E49EEA5F37799FE4DFC738F99E140F8E31D1B21E9882867563C9BC24A39A2195266D87B20348ABD0FA9A6BF0E8A0935B9B82334378B148132AD85C83433D68803026469BE4C2DD5F738CF0F4321AAAADAC4E2D90253F28E9B65B74FA6040AEE5A23CFDD120058FE454FC8E7FEBF9ADE4F11FEE01E16604A5892179064FFEDD3CE4BD2CE0D46D8121A422D282170E43B276F92D9F10CA1FBB3DD1F530C3F07A3E028301211FB2E6E13B22153D92E0C1EC36F3825910CF7A0A53D68041221E30A66AAECF8DF4C6EC13FEA8EE900BE3B891230E28B2C78A49D377B55555038BC2CE5CD848A002A4C873F3958EA8233E5FCAC9202C12FB6B3DE307886208536A325FC7FE4003C6EF3C244ACADD2208D3A226924D240EAEEEB02FBE98BBAC37FD92434806D157F35A379E6261D1D08DE2F253A5976EF61405CA32CA3AB1A3E85DAA0509C68BC90DEC72E3C1D728A000DBAD0A7C1F8D88A741925532C6A66B646116B82C7AA02476608B5CD128783EC9FEFA617EC1EE555B5D6A94BBC24F008FE7A581342137683B595FD0A92E83F02D7C20D54F677CFA753C41CEAFD32C349B1CBE1FE87546FCB28F35EE4B1674A0FBCAAEA861DA7C63EF224008B1E258354FFED963A3E2F45D37F78FF30D6669BA8A864E9E1700C5DFDD75C68D4E3A7E408C6CAFBB7BB3182E59320BF4A2E10218E758703EBE1C63ACDDED9D13FF4F167FDC74CA28834E7FA4C81552841D323B10785427CAB082F36CB7119A5A79CA4DAB5FF77B12C94BCA78199A3E4A704BDE72E6952A7E3B905186A10DF4774B2199370CC26309E993A1942912D8913B2D5545117747130A6BC0FE9107998D5BAA0EF2F694DA7D5D81D8F790E296FE19828747DDBA828D00ED8D41D247935ADEC550C3E8D6A53A618EEC0CF43878246F9CF6FDEC3826387B667626A5E2D164000F6FD4B0CD7F5EAC42268DB7F985D85D7BC4F75C5CBAA66F89506175AFA7704FB03B787C427F8DE0631C481E8C5FC9E79221A9DFC4E42C58F654E626286E146B68C1290EEE20A789DDDC40268859DB433045A438268CD06BE372398B2D5F892CA714B5CEB4A840324A474D36A85317FBE0FA5B80F2943C677BEB8112965BD46C566A1EB104530BA7692EA31496F7B530B9AD75E793BAE515E43974D1BC861B6D40AB682FBD993BE1F2A667713F75AD2EEC944C3F9D8C87E798DF0513488F6AD4678A3B9C82909772F51FA23EBBD16B5C2D7580112A1B19D1EF08136CF4D5F211F868979B2A733441359580174FC31E416A9F12B3C2706B1A0BF9B00FFD21B6210B11DC59EC947BE86D11E10DE2E980B8BC988E26ABA269AC6BD4CD2BF0471FFCB95377922449DE5393850A00A57B47800D374D961DFEB51489A83A8EF0947147E7E248955831D555EFE2A60969DDB14993CDB530D61BB70A6EC05CAB57909DA206FBB2C89D3444EB2AB7653CDCC6968AC757A103F37376EA0A17F43C0F2A6A4229ACBAFAD6D5CCFD6045572564FCF3F1D9D83C202F5FB4C1507A79FDDFAC29DF2A7569599B84F4644DEB6E83D20FBDFD862E08793FD2337705263BA0DA097F6F59E515E45C68FD003266D07BA7426A1B3F145F8ABF2BC2BF51622DDC41EAC05FF6446CE1B1C837789DBBA87EA6866E5549AA68C1989D0159E0499B731097EED287DEF9F91961ABA8CE15A98B9137F0977656F5F1E5281D0DB225BA14B4BA442BB71AC1BA6E27B34656C00D4B1F65380EF19C2469290DC541B01CD57E4D3753654F173706003C57D17ED4342725F785CD4D4754D5412A8FB0E841C79EC865D2210685A6AE7C8FC3FAB4B8B467CCC9FEAF434E9ED0B5F79F143CCE135DF27E01D5413D00DB6D2FC33FF0D032E245156EAB0D80E6B43D3F06EF6B09DEC23D79B14BEDBC36063353A330E3AB16D434DE52A4F502C18F4D3E06FE999AF7E2BB01DA5C05EC66BB3E93481D1B70252D13EED3BD999F05A33478C85E3DE5213341215DFE68D6D8A1C1989E6DB52E651A73BC0AA737D0F71B8A429AF4F12574FE7BA854F8BC9B4A6C5AEE5D24F5A699404E8A272D4B886F4B939E0B3F0D5969AB7259A77E3F69EA48DD6E89D9632DCC84FDD944774D0274A5113AA03135E0264131A761FAEFC547A58DF2E231811A75BA752D61740394A4A2195FC814A2E189CFEEC4F38F61415E377863177BA5C2E81C6D050856A97F7B6E3D2449FEFC0D41ABEA0E355DECC5734B06F4FF3EB3CF56A30B23070F3D4B0860E54E47CFB8F0E93F4525C34008B0110F64DBCAA729713A0702CC50C5975A859FA7A614D3E441E48E75BC572EF787D26790DA5AD3E24C6CBD16A7DC5C3E400A79B430E03C27993465A0C7C140F80F288DCBDBC07E707B4E0EEC6BDABD2BC435200FBC02B73227026FECAC659805C4AE4BEF7E125458C5A249EE543EC4388E7C95B03C38180DDBF24D09147D8A508CC1172435D4DD1A80CCDD5E1CB9719FD51FC33F8045E5F66C33982A5D3CECBC1CBDFEC7F0CD6C3C86F1F1B4FE0DAB9B848734F25BFB727F96088BC79C437EEF2675B67016B9928289A0467E2C19A09C602169170C09E9B72576BF4DB6656F1CF3E5DFDDCD6A611869F183E3948F49C3DA9A96753D0AE124A152535E7DEB7AF448EDE53123B04E4FF914C73341FBBEFF6DC42F542147F86E173DE61370E2EE14BD8EDD2FE76DFCC14FE25F5CF4ED2F1DC9B97E955F34CF50E42338F3CD0EC2B8B7225B8AABE6A46889BB1AED7FB316BCDDF287C32B9CF0479F2A917002318ED6CC843575011F156A484D9BD9D0A5F8BB44F897D98F894E396760249F4443C0E04E8D566D5E69C9220922DC11055C53C91566BC30A9962C925B427537B830C0AD5040D70719E3576A2ACA001B85B4552A66A3DAAE3829F83220C20DD829D7EF145B725219A32AB5CD58055DE86BF5A19FC11232BD5D7023E8BED1BB92687090F6118433290992D7A85891323ABAA4AAACB20E5275C189821EABFED288C2D6C727A36F5D51EDA7FC5A748460D22A8F1B6BECB3A058DFFCB4628E7EE401E90F46B2365D6A53A5F596824F033141A40EAB0D15A700AA5AFE070461792BACFBAA6A78F2FF2A97495406877009BB5B6F5C9C48E480E172AD6B3E2AE23AD8537A00648580562D8CAE7DD97BCA592AD94D9B110A2504290C7894100DA57C04541EF20FC25D67664DED20E2D874578AC99BC9B8B0517B7B969B67D3F484C0F8A8361EECE4D6F418AA3010415C51C496CCCDBB99F6FEB175BA6DBAE063128A9465210BBF2AEDB527F84A22A85BBD542C94DAF89137EEC15C996CB141CFE230ED4B74405784545B0E47A08F14EF6204F406846EBE2037B350A002F17D0DF02E524D6BB19D1C4D0ADAADD9E46E16F344591B4DCDAEAFBA984351C3521780AD4066BA387D61C1B5908E141772EF38C782D4C0C4E25A310C18A2DB94D730008825CF6834A8ED9E9A73D4E68CEBCEB46EA3D0B04682321DC809CF04BEF7CBC29656616A7ECA0D7D5861765245900539B8F195B038B4521BA7C1E2327D37260EEFF84B8733B86C30402225492705D03CF243DCA006962C66C88EED70475278E6B298E49EA24CEB3B3B91812D87F00D6BF03D4CCEE0ADBD8F910122EBADBAA159187553333E0FB8E4AE6C147B225C4A652DB70EB3CB2840CEF2A072B94058ABEC89487FC4AECEF8CEB7E299BD9063C4F4D3D4298D74B6D848F2A8B5545B71F76CA31DC9A3E5ABDE3C1D4C2A175895FF1A227EC11A6B54A80B5577F8E417906BA8D169283938DD0D8520C18B692FA9A41868180513DE1EC93E9002B6BD5560A8800980EA3C84A4ED37D398956EACE446440D4E78AFC44A272FAB830E1BA0FF643618580F7C4B76295C4DF5B1061F5738E403C024D6F4C2D2E1FE6BF6106679E5EF8AFBEC1E0E0D0EFFC5ABCBB213B089F14EC2BD0BB09BF8BBD57912B2572E01F40B7B87648FF77A2509E1CA3116F17E679D22496C85300650F31BD4DEAC02B6943C675C4D27D2D22D63837522224D8D1B982E46C6E98BCA82D15567789B5187C2FF035CA362F2D74A89FF167A1DCA48C37C6D900F3F1075AFEAEF06A236C7E8A6403D13C43D4A2E918809EAB1078FF2532813BD7EBB8DA2E2AFB8969AD19C3E056191E009C876A58444CEA5886149F4F5586486D4F4A9A3EE7AC104965E042F9870CF6945EEF81D6873A773ED1E14DBC4B831177F66773E0A1A28D05FE73B2FFD72E8A580BF7ED79E1B7FBCE93145032751FF67F27704E9304AFF6D8AECFEF2553449C4B3BC795A9A489B5253E41E6639B737BEA6A100C9A023B750B45C2B355EC8586FE10E1C8BDAE8149C8E9A9CFEAB6F3DE2EE0FB8A82804C90479E15DFF96EC3CE1BF067AEA2EEF8E3F820DF0D86B5983A32A9A4746525D09D8BD6AD333E8C6B69E02849CBFE0CE99747DF4028EA455BE3B1B0A7B95B645FB9688B8A3647BA2ADEB9E509F645A9453BDB6569BA0743121CBF1B801C5B6A095D4B2A4E545976B826732F43C9F23FB8DF3FA7DFC28384CAA3EC320FBF1FEC7BBBB5284EBD231EEB1C84E8397485001CBC1D46CF3E34C1FDD72BF546ADCADD68985DE1D1F05B149ECB9B0D2AAE57BBF435116E26E71FCAD85636450E356E1524A4612871080C3E4847E8DF704DF521AD50E88598D940789C074D6BDB8C0BEA83A6B3B4B8E0958218965013BAFB5D6D103C3E1F6FFF47160BC5BD86781C57968C3BA4A1390712D1D9687FEE7FBC743F16ABA5CDED4FF20CB742621D1C980C7122DC79956E55051BC5E27FF9B91B59D8BA423E7ACCFC217E651E407F98BF187C2BE596F52F140936E4434B2E4DD55427F0D96CE9B0DB902746A92D76A4B08D77E4041FDCFB88BAF00A927081441C193FD44E31B2C3A8D98D310A18C5CD1E64D4812B3DDF077D1FB40BF13B6087EFCB34D5003DF357166CA7798A4FAD68C24F71471740B97CFE8A5F3D98C453C1500C1432C8371A318BBDAEB3677BA4C04CC6108D8B4967AE25CC47FAE769B29FF5505EDD917F81EA7A6B155FCB87E17D7CDCAAA4B13321D9B34C8AC554C3630D0D9A73E24257320F55BEF734CF8238F368DDCC00E571880DDCBC3E0FE81110479145D45BADD25DF29BED9EC77E88ECF7E3CB8272F444136BE48824F92811408245EC75E692AA50544DC62B0431582EA6382AD0062A17A6BE474634A660E8E9E110BF0B52569FD5D1C17F616C144FD20F2BE2634BADA7278F9FEF04B29F161253D35479F16A89108AF69FB41433A450723C6A56BC03513DC2352EA32D6D2BD9E064BBA67AACCD980DD2B75E5CB7780586A324A9F46B1F31C038896D514C9421BC5D0F6F3CA4CD68E90626377740B62627326A296CD2A3B8DBEF21798BA31CA33D59772E4F0E3B6074F9562D1C028A5B8B4F26D66E499A90E893FC008131B8C3313670CDE168BF8A82D7EE76FA7A34E651CC19A0A08500F82DF0B67A1E4422A737450E5AEDFEDF60C507C71D65188B0B240225C02CF0B507B0E78F4E456C0683A85CE2D0EC09100C1E39D771CDED460EE5BC78D54DE5EE2FC589DFB9C960F6FDCAA3279647B26D1239C08795005F64C2E660DF62624CF6F2FB282CD2C2127D554B61AC30E8DEEC0D7359F2BB5CDC921DD7F74282A50811684B13ECE38BF1E0FB8E3C0BEB0F3109945A73656E05FE5EADCD9D88887B9752BAC2D273078F6DE0BAD475CFD3B6A085A4776EA6625CF3CAE9F5EB5BF01909FAD6135B9BDE7C2370C65A1A50A6AAF94CF5F408FD66A891857EF88493A3D9C25B7408CF536FBF0457240A99D3CBD8D23473C3326912826D98292B20EEBB68EAC5D980BF9C12E7F8F111C41B2C69443190A2151A12C09C88D99ADB507B9C0CD9C7BDCC8DE4D705FEF98C4DCB8578142EBDB7A35882088F678E3CB0845FC74E5FC0764CECEE46EBD691351B96A5DE42E03567143F3761237C4A155AC0F9E3CC3F1962E9210823B9F04059A199B8D74066AC885A952B8E47E873215BB038788DE296AE1333EC133FBB3DC8978CFF48C2A5953A86D72FA6E5113D9C51840DCCF718C424B0BDF53DB4FE28A7A2BAF87FF87BDAF8AB59096BD59FE3686225A9492C4F6370EC9F5FA3542379BDBD3577E932895F94B2312D4716A9F56A29F91D24FAE59795AC6C9A3B0D402AA44AE57097B7C4F5991F5A985A59A744B8E9D6A6D5E99DE36F2B9C3F6880FC4A43419E8636FC3DF9C245E744FD4E988EBCF4A7ADD9A06992BD894CD39113AA1560A184E5E8642F7DDF71B455668009579F814672FD61CDE9C5B8A0D06507E5E75699C0BBD1C9FB3B0E3AE0A45AB309B5F4EC4971A4A9FD5FB8A994EEF92F3D4212E5B665A0F9E489C2357146A65D3904BE964FBEFA7DE07B2A09B4F5A43DD3181AE69A494C63CDEE7E82378DCBB9EC2F7050AA9F6022D005E8710FFCAA58678D8DE27
其他的进制转化根据这个代码稍加修改就可以了。