2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「ビットあみだくじ」

2
Posted at

勉強会には参加してないけどおもしろそうなことしてたので勝手に参加。

ルール

使用言語

候補には無かったけどAS3を使用。

回答

個人的にビット演算はif文が少ないほうが美しい気がする。

Test189.as
package  
{
	import flash.display.Sprite;
	
	public class Test189 extends Sprite 
	{
		private var count: int;
		
		private function solve(amida: String): String 
		{
			var i: int, j: int, p: int, v: int, t: int;
			var yoko: uint = parseInt(amida.split("-").join(""), 0x10);
			var res: String = "";
			for (i = 0; i < 9; i++) 
			{
				for (j = 0, p = i; j < 4; j++) 
				{
					v = ((yoko >> (j << 3)) & 0xFF) << 1;
					t = (v >> p) & 3;
					while (t == 1) t = (v >> --p) & 1;
					while (t == 2) t = (v >> ++p) & 2;
				}
				res = (8 - p) + res;
			}
			return res;
		}
		
		private function test(amida: String, answer: String): void
		{
			var reply: String = solve(amida);
			if (reply == answer) 
				trace(count + " : OK [" + reply + "]");
			else
				trace(count + " : NG");
			count++;
		}
		
		public function Test189() 
		{
			/*0*/ test( "d6-7b-e1-9e", "740631825" );
			/*1*/ test( "83-4c-20-10", "123805476" );
			/*2*/ test( "fb-f7-7e-df", "274056813" );
			/*3*/ test( "55-33-0f-ff", "123456780" );
			/*4*/ test( "00-00-00-00", "012345678" );
			/*5*/ test( "00-00-00-55", "021436587" );
			/*6*/ test( "40-10-04-01", "021436587" );
			/*7*/ test( "00-00-aa-00", "103254768" );
			/*8*/ test( "80-20-08-02", "103254768" );
			/*9*/ test( "ff-7e-3c-18", "876543210" );
			/*10*/ test( "aa-55-aa-55", "351708264" );
			/*11*/ test( "55-aa-aa-55", "012345678" );
			/*12*/ test( "db-24-db-e7", "812543670" );
			/*13*/ test( "00-01-00-40", "021345687" );
			/*14*/ test( "00-00-80-00", "102345678" );
			/*15*/ test( "01-40-00-00", "021345687" );
			/*16*/ test( "00-00-00-02", "012345768" );
			/*17*/ test( "00-00-02-00", "012345768" );
			/*18*/ test( "00-14-00-00", "012436578" );
			/*19*/ test( "00-00-01-40", "021345687" );
			/*20*/ test( "00-80-01-00", "102345687" );
			/*21*/ test( "c8-00-00-81", "120354687" );
			/*22*/ test( "05-48-08-14", "021435687" );
			/*23*/ test( "24-05-00-f0", "413205687" );
			/*24*/ test( "40-08-14-01", "021536487" );
			/*25*/ test( "18-c8-80-80", "210534678" );
			/*26*/ test( "1c-88-52-00", "120564738" );
			/*27*/ test( "ec-dc-67-62", "213468705" );
			/*28*/ test( "0a-b6-60-e9", "035162784" );
			/*29*/ test( "52-d6-c6-c2", "120345678" );
			/*30*/ test( "47-e7-b0-36", "231047658" );
			/*31*/ test( "0f-85-91-aa", "108263754" );
			/*32*/ test( "76-b6-ed-f3", "601435782" );
			/*33*/ test( "f5-5e-f7-3d", "025847163" );
			/*34*/ test( "dd-e7-fb-f9", "610247538" );
			/*35*/ test( "8f-f4-af-fd", "583246017" );
			/*36*/ test( "bf-fb-cb-f7", "105382674" );
			/*37*/ test( "e5-fd-ff-ff", "512046378" );
			/*38*/ test( "ef-df-ef-fe", "713205648" );
			/*39*/ test( "bf-7f-fd-d7", "826437105" );
			/*40*/ test( "36-ff-df-de", "814527603" );
			/*41*/ test( "6f-dd-ff-ff", "230685147" );
		}
	}
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?