喊了這麼多年說想玩ACM ,今天終於開始了,希望最少可以一週一題(夠懶吧),不然最少3天一題試看看囉!!
首先第一題,最簡單的100 ,但真不知自己為什麼那麼笨.......SUBMISSIONS 了快50次,才發現不是程式的問題......找了好久...所以....我要加強呀
玩ACM方向也許是對的,但不知自己還有多少時間、精力、腦力可以玩就是了.....如果有人要玩,記得....加油...不一定要問別人,因為別人一定會,要自己弄懂比較難啦.....(這也是訓練自己邏輯、解題的能力)
以下有兩種都是可以的....
########### method 1 ###########
[先輸出]
#include <iostream>
using namespace std;
int getCrycleLength(int nCalc)
{
int nLength;
nLength=1;
while(nCalc!=1)
{
if (nCalc%2!=0)
{
//奇數
nCalc = nCalc*3+1;
}
else
{
//偶數
nCalc = nCalc/2;
}
++nLength;
}
return nLength;
}
int main(void)
{
int nBegin,nEnd,nMaxLength,n,nCrycleLength;
while(cin>>nBegin>>nEnd)
{
cout<<nBegin<<" "<<nEnd<<" ";
nMaxLength=0;
if (nBegin>nEnd)
{
swap(nBegin,nEnd);
}
for(n=nBegin ; n<=nEnd ; n++)
{
nCrycleLength=getCrycleLength(n);
if(nCrycleLength>nMaxLength) {nMaxLength=nCrycleLength;}
}
cout <<nMaxLength<< endl;
}
return 0 ;
}
########### method 2 ###########
[後輸出]
#include <iostream>
using namespace std;
int getCrycleLength(int nCalc)
{
int nLength;
nLength=1;
while(nCalc!=1)
{
if (nCalc%2!=0)
{
//奇數
nCalc = nCalc*3+1;
}
else
{
//偶數
nCalc = nCalc/2;
}
++nLength;
}
return nLength;
}
int main(void)
{
int nBegin,nEnd,OraBegin,OraEnd,nMaxLength,n,nCrycleLength;
while(cin>>nBegin>>nEnd)
{
nMaxLength=0;
/* remember original order of entries for output */
OraBegin=nBegin;
OraEnd=nEnd;
/* 新手最常死在這 因為等等要交換.就會忘了最後輸出要是原來的順序*/
if (nBegin>nEnd)
{
swap(nBegin,nEnd);
}
for(n=nBegin ; n<=nEnd ; n++)
{
nCrycleLength=getCrycleLength(n);
if(nCrycleLength>nMaxLength) {nMaxLength=nCrycleLength;}
}
cout<<OraBegin<<" "<<OraEnd<<" "<<nMaxLength<< endl;
}
return 0 ;
}
###################################################
留言列表