Pages

Monday, August 15, 2011

d018. 字串讀取練習


#include
#include
#include
using namespace std;

const int SIZE = 3000;

int main()
{
char input[SIZE];
while(cin.getline(input,SIZE))
{
double sum=0;
char* pch;
pch = strtok(input," :");
while(pch !=NULL)
{
bool IsOdd;
if((int)atoi(pch)%2==1)
IsOdd=1;
else IsOdd=0;

pch = strtok(NULL," :");

if(IsOdd)
sum += atof(pch);
else
sum -= atof(pch);

pch = strtok(NULL," :");
}

cout< }
return 0;
}

Sunday, August 14, 2011

d098: Stringstream運用練習(C++)


#include
#include
#include
using namespace std;

bool IsAllInt(string str);


int main()
{
string input;

while(getline(cin,input))
{
int sum=0;
stringstream stream(input);

while(stream)
{
string word;
stream>>word;
if(IsAllInt(word))
{
sum += atoi(word.c_str());
}
}
cout< }
return 0;
}

bool IsAllInt(string str)
{
for(int i=0;i {
if(!isdigit(str[i]))
return 0;
}
return 1;
}

d583. 幼稚的企鵝


#include

using namespace std;

int main()
{
int n;

while(cin >> n)
{
for(int i=1; i<=n; i++)
{
int temp;
cin >> temp;
cout << i << " ";
}
cout << endl;
}
}

Saturday, August 13, 2011

d635. 幸運777?luck


#include
using namespace std;
int main() {
int input;
while(cin>>input)
{
if(input<0)
{
cout<<"-1"< break;
}
cout< }

return 0;
}

d649. 數字三角形


#include
#include
#include //格式控制
#include
using namespace std;
int main() {
int input;
while(cin>>input)
{
string str;
for(int i=1;i<=input;i++)
{
str +='+';
cout.setf(ios::right);
cout << setw(input) << setfill('_') << str< }
cout< }

return 0;
}

d059. 數學函數練習


#include
#include
#include //格式控制
using namespace std;
int main() {
double a,b;
cin>>a>>b;
cout << pow(a,b) << endl ; //a 的 b 次方

double c;
cin>>c;
cout << fixed << setprecision(3) << sqrt(c) << endl ;

int d;
cin>>d;
cout<
int e,f;
cin>>e>>f;
cout<<(rand()%(f-e))+e << endl ;

return 0;
}

d532. 文文的求婚 (三)


#include
using namespace std;

int main()
{
int a,b,count=0;
cin>>a>>b;
for(int year=a;year<=b;year++)
{
if((year%4==0 && year%100!=0) || (year%400==0))
count++;
}
cout<
return 0;
}

d559. 班號


#include
using namespace std;

int main()
{
int input;
while(cin>>input)
{
cout<<"'C' can use printf(\"%d\",n); to show integer like "< }

return 0;
}

d827. 買鉛筆


#include
using namespace std;

int main()
{
int input,dozens,singles,output;
cin>>input;

dozens = input/12;
singles = input%12;

output = dozens*50+singles*5;
cout< return 0;
}

d122. Oh! My Zero!!


#include
using namespace std;

int main()
{
long input;

while(cin>>input)
{
long output = 0;
while(input>4)
{
input/=5;
output +=input;
}
cout< }
return 0;
}

d489. 伏林的三角地


#include
//海龍公式
using namespace std;

int main()
{

int a,b,c;
cin >> a >> b >> c;

long long int ans = (long long int)(a+b+c)*(b+c-a)*(a+c-b)*(a+b-c);

cout << ans/16 << endl;
return 0;
}

Friday, August 12, 2011

d277. 矩形对角线


#include
using namespace std;

int main()
{
int n;
while(cin >> n)
{
if(n%2==0)
cout << n << endl;
else
cout << n-1 << endl;
}
}

d461. 班際籃球賽


#include
using namespace std;

int main()
{
int a;
cin>>a;
cout<<(a-1);

return 0;
}

d124. 3的倍数


#include
#include
using namespace std;

int main()
{
string str="0";
int input;

while(getline(cin,str))
{
int sum=0;
for(int i=0;i {
sum += str[i];
sum = sum%3;
}
if(sum==0)
cout<<"yes"< else
cout<<"no"<
}

return 0;
}

Thursday, August 11, 2011

d086. 態度之重要的證明


#include
using namespace std;
#include

int main()
{

string str;
while(cin>>str)
{
int size=0,points=0,allchar=1;
size=str.size();
if(size==1 && str[0]=='0')
{
break;
}
for(int i=0;i {
if(!isalpha(str[i]))
{
allchar=0;
break;
}
char a;
a=str[i];
a=tolower(a);
points += a-'a'+1;
}
if(allchar)
cout< else
cout<<"Fail"< str.clear();

}



return 0;
}

Wednesday, August 10, 2011

d460. 山六九之旅


#include
using namespace std;

int main (){
int n ;
while(cin >> n)
{
cout << (n>=0)*0+(n>=6)*590+(n>=12)*200+(n>=18)*100-(n>=60)*491<< endl ;
}
}

a104. 排序


#include
using namespace std;
#include

int main()
{
int size;
while(cin>>size)
{
int arr[100000];
for(int i=0;i {
cin>>arr[i];
}
sort(arr,arr+size);

for(int i=0;i {
cout< }
cout< }

return 0;
}

d507. 三角形的判斷


#include
using namespace std;

int main()
{
int a,b,c;
while(cin>>a>>b>>c)
{
int sides[3]={a,b,c};
sort(sides,sides+3);
a=sides[0];
b=sides[1];
c=sides[2];
if( c*c > (a*a+b*b) )
cout<<"obtuse triangle";
else if(c*c < (a*a+b*b) )
cout<<"acute triangle";
else cout<<"right triangle";

}
system("pause");
return 0;
}

d511. 小明的作業


#include

using namespace std;

int main()
{
int a,b,c;
int point=0;
while(cin>>a>>b>>c)
{
int max=0,other_two=0;
max = (a>b)? a:b;
max = (max>c)? max:c;
other_two = a+b+c-max;

if(other_two>max)
point++;
}
cout<
//system("pause");
return 0;
}

Monday, August 8, 2011

d491. 我也愛偶數 (swap 版)


#include
using namespace std;
#include


int main()
{
int a,b;
while(cin>>a>>b)
{
if(a>b)
{
int temp=a;
a=b;
b=temp;
}
int sum=0;
for(int i=a;i<=b;i++)
{
if(i%2==0) //別種寫法?
sum += i;
}
cout< }

return 0;
}

d490. 我也愛偶數 d485 我愛偶數


#include
using namespace std;
#include


int main()
{
int a,b;
while(cin>>a>>b)
{
int sum=0;
for(int i=a;i<=b;i++)
{
if(i%2==0) //別種寫法?
sum += i;
}
cout< }

return 0;
}

a040. 阿姆斯壯數


#include
using namespace std;
#include
#include

bool IsArm(int thenum);

int main()
{
int a,b;
while(cin>>a>>b)
{
int flag_hasArm=0;
for(int num=a;num<=b;num++)
{
if(IsArm(num))
{
cout< flag_hasArm=1;
}
}
if(!flag_hasArm)
cout<<"none";
cout< }


return 0;
}


bool IsArm(int thenum)
{

int a[100]={0},sum=0,size=0,temp=thenum;
for(int i=0;temp!=0;i++)
{
a[i] = temp % 10;
temp /= 10;
size++;
}

for(int j=0;j {
sum += pow((float)a[j],size);
}

return ((sum==thenum)? 1:0);
}


/*
bool IsArm(int thenum)
{
char buffer[100];
//string str="0";
itoa(thenum,buffer,10);
string str(buffer);
int sum=0,length=str.length();
for(int i=0;i {
sum += pow( (float)(str[i]-'0'),length);
}

return ((sum==thenum)? 1:0);
}
*/

Sunday, August 7, 2011

a034. 二進位制轉換


#include
using namespace std;
#include

int main()
{
string output;
int input;
while(cin>>input)
{
if (input==0)
cout<<"0";
while(input>0)
{
if(input%2==1)
output +="1";
else
output +="0";
input/=2;
}

for(int i=output.length()-1;i>=0;i--)
{
cout< }
cout< output.clear();
}

return 0;
}

a054. 電話客服中心


#include
using namespace std;

#include

bool CheckIsReal(int thesum,int thei);

int main()
{

string ID;


while (cin>>ID)
{
int sum=0;

for(int index=0,mul=8;mul>=1;index++,mul--)
{
sum += ( (ID[index]-(int)'0') * mul );
}
sum += (ID[8]-'0'); //最後一位數 千萬要注意'8'跟數字8是不同的!!

for(int i=0;i<=25;i++)
{
if (CheckIsReal(sum,i))
cout<<(char)(i+'A');
}
cout< }


return 0;
}

bool CheckIsReal(int thesum,int thei)
{
int v[]=
{ 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21,
22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33};
int temp=0;
thesum += v[thei] / 10 ;
thesum += (v[thei]%10)*9;
if (thesum%10==0)
return 1;
else
return 0;
}

a020. 身分證檢驗



/*學習事項
1.使用string時,千萬注意數字與符號的數字是不同的 eg. '0' != 0
2.用cout除錯其實很好用
*/
#include
using namespace std;

#include

int AlphabetToInt(char letter);

int main()
{

string ID;

while (cin>>ID)
{
int sum=0, ch_num=0;
//deal with alphabet
sum += ( AlphabetToInt(ID[0]) / 10 );
sum += ( AlphabetToInt(ID[0]) % 10 )*9;
//cout<<( AlphabetToInt(ID[0]) / 10 )< //cout<<( AlphabetToInt(ID[0]) % 10 )*9< //cout << (int)'1'<
for(int index=1,mul=8;mul>=1;index++,mul--)
{
sum += ( (ID[index]-(int)'0') * mul );
//cout<< ID[index] << " " << (ID[index]-(int)'0') << " " < //cout< }
//cout< sum += (ID[9]-'0'); //最後一位數 千萬要注意'8'跟數字8是不同的!!
//cout< //cout< cout<<( (sum%10==0)? "real":"fake" )< }

//system("pause");
return 0;
}

int AlphabetToInt (char letter)
{
int v[]=
{ 10, 11, 12, 13, 14, 15, 16, 17, 34, 18, 19, 20, 21,
22, 35, 23, 24, 25, 26, 27, 28, 29, 32, 30, 31, 33};
int i = letter - 'A';
return v[i];
}

Tuesday, August 2, 2011

a058. MOD3


#include
using namespace std;

int main()
{
int many,num;
int remainder0=0,remainder1=0,remainder2=0;

cin>>many;
for(int i=0;i {
int remainder=0;
cin>>num;
remainder = num % 3;

switch ( remainder )
{
case 0 :
remainder0++;
break;
case 1 :
remainder1++;
break;
case 2 :
remainder2++;
break;
default :
break;
}
}
cout< return 0;

}

Monday, August 1, 2011

a065. 提款卡密碼


#include
using namespace std;
#include

int main()
{
string word;
while(cin>>word)
{
for(int i=0;i<6;i++)
{
int num;
num=abs( (int)word[i]-(int)word[i+1] ) ;
cout< }
cout< }


system("pause");
return 0;

}