001 : //======================================== 002 : // program.c --- ACM ICPC 2012 Domestic B 003 : // Created by M.Miyazaki [2013.01.13] 004 : //======================================== 005 : 006 : #include <stdio.h> 007 : #include <stdlib.h> 008 : #include <string.h> 009 : 010 : /** 011 : * 数値リストの最大要素数 012 : */ 013 : #define MAX_ELEMENTS 21 014 : 015 : /** 016 : * 最大桁数 017 : */ 018 : #define MAX_LENGTH 6 019 : 020 : /* 021 : * 関数のプロトタイプ宣言 022 : */ 023 : int main(int argumentCount, char * argumentValues[]); 024 : 025 : /** 026 : * メインプログラム. 027 : * 028 : * @param argumentCount 引数の数 029 : * @param argumentValues[] 引数の値 030 : * @return 終了時の値(正常終了ならEXIT_SUCCESS) 031 : */ 032 : int main(int argumentCount, char * argumentValues[]) 033 : { 034 : int aValue; // 入力された数値 035 : int numberOfDigit; // 数値および桁数 036 : 037 : scanf("%d %d", &aValue, &numberOfDigit); 038 : 039 : while (aValue != 0 || numberOfDigit != 0) 040 : { 041 : int valueArray[MAX_ELEMENTS]; // 数値リスト 042 : int count = 0; // 数値リスト用のカウンタ 043 : int maxNumber, minNumber; // 桁の入れ替えから求まる最大値と最小値 044 : int indexOfDuplication; // 重複要素のインデックス 045 : 046 : for (count = 0; count < MAX_ELEMENTS; count++) 047 : { 048 : valueArray[count] = -1; 049 : } 050 : count = 0; 051 : valueArray[count] = aValue; 052 : 053 : while (1) 054 : { 055 : int index; 056 : int index1, index2; // 注目している2要素を指すインデックス 057 : char formatString[10]; 058 : char valueString[MAX_LENGTH + 1]; 059 : 060 : sprintf(formatString, "%%0%dd", numberOfDigit); 061 : 062 : sprintf(valueString, formatString, aValue); 063 : for (index = 0; index < strlen(valueString) - 1; index++) 064 : { 065 : if (valueString[index] < valueString[index + 1]) 066 : { 067 : char temporaryCharacter; 068 : temporaryCharacter = valueString[index]; 069 : valueString[index] = valueString[index + 1]; 070 : valueString[index + 1] = temporaryCharacter; 071 : index = -1; 072 : } 073 : } 074 : maxNumber = atoi(valueString); 075 : 076 : sprintf(valueString, formatString, aValue); 077 : for (index = 0; index < strlen(valueString) - 1; index++) 078 : { 079 : if (valueString[index] > valueString[index + 1]) 080 : { 081 : char temporaryCharacter; 082 : temporaryCharacter = valueString[index]; 083 : valueString[index] = valueString[index + 1]; 084 : valueString[index + 1] = temporaryCharacter; 085 : index = -1; 086 : } 087 : } 088 : minNumber = atoi(valueString); 089 : 090 : aValue = maxNumber - minNumber; 091 : count++; 092 : valueArray[count] = aValue; 093 : 094 : indexOfDuplication = -1; 095 : 096 : for (index1 = 0; index1 < count; index1++) 097 : { 098 : for (index2 = index1 + 1; index2 <= count; index2++) 099 : { 100 : if (valueArray[index1] == valueArray[index2]) 101 : { 102 : // 重複要素が見つかったため、ループから脱出する。 103 : indexOfDuplication = index1; 104 : index1 = MAX_ELEMENTS; 105 : index2 = MAX_ELEMENTS; 106 : } 107 : } 108 : } 109 : 110 : if (indexOfDuplication != -1) 111 : { 112 : printf("%d ", indexOfDuplication); 113 : printf("%d ", valueArray[indexOfDuplication]); 114 : printf("%d\n", count - indexOfDuplication); 115 : break; 116 : } 117 : } 118 : 119 : scanf("%d %d", &aValue, &numberOfDigit); 120 : } 121 : 122 : return EXIT_SUCCESS; 123 : } 124 :
This document was generated by NanigashiBiyori on 2013/01/14 at 00:06:27.