//第四屆藍(lán)橋杯軟件類國賽真題-C-C-1_猜年齡/*題目標(biāo)題: 猜年齡 美國數(shù)學(xué)家維納(N.Wiener)智力早熟,11歲就上了大學(xué)。 他曾在1935~1936年應(yīng)邀來中國清華大學(xué)講學(xué)。 一次,他參加某個重要會議,年輕的臉孔引人注目。 于是有人詢問他的年齡,他回答說: “我年齡的立方是個4位數(shù)。我年齡的4次方是個6位數(shù)。 這10個數(shù)字正好包含了從0到9這10個數(shù)字,每個都恰好出現(xiàn)1次。” 請你推算一下,他當(dāng)時到底有多年輕。 通過瀏覽器,直接提交他那時的年齡數(shù)字。 注意:不要提交解答過程,或其它的說明文字。*//*【解題思路】解法一:暴力枚舉 答案:18*/#include<iostream>#include<cmath>#include<cstring>using namespace std;int book[10];//標(biāo)記0~9這10個數(shù)字出現(xiàn)的次數(shù),下標(biāo)表示0~9這10個數(shù)字,值表示出現(xiàn)的次數(shù) /* * @簡介:檢測年齡age是否滿足題意條件 * @參數(shù):年齡age * @返回:若滿足條件返回true,否則返回false */ bool isSolution(int age){ int PRoduct1 = (int)pow(age,3); int product2 = (int)pow(age,4); //檢測是否滿足 年齡的立方是個4位數(shù)、年齡的4次方是個6位數(shù) if(product1/1000 == 0) return false; if(product2/10000 == 0) return false; do{ book[product1%10]++; product1 /= 10; }while(product1); do{ book[product2%10]++; product2 /= 10; }while(product2); //檢測是否滿足 這10個數(shù)字正好包含了從0到9這10個數(shù)字,每個都恰好出現(xiàn)1次 for(int i=0;i<10;i++) { if(book[i] != 1 ) return false; } return true;}int main(){ for(int i=10;i<100;i++) { memset(book,0,sizeof(book)); if(isSolution(i)) cout<<"他那時的年齡數(shù)字為:"<<i<<endl; } return 0;}
新聞熱點(diǎn)
疑難解答