acm_hdu ACM Steps Section2(1.2.1-1.2.8)

1.2.1

#include <stdio.h>
#include <stdlib.h>

#define up 6
#define down 4
#define stop 5

int main(int argc, const char * argv[])
{
int n = 0;
while (scanf("%d",&n) != EOF)
{
if (n == 0)
exit(0);

int current = 0;
int next = 0;
int sum = 0;

for (int i = 0;i < n;i++)
{
scanf("%d",&next);
if (next > current)
{
sum += (next - current)*up;
sum += stop;
current = next;
}
else if (next < current)
{
sum += (current - next)*down;
sum += stop;
current = next;
}
else
sum += stop;//stop at the same floor will also cost 5 mins! - -
}
printf("%d\n",sum);
}
return 0;
}

1.2.2

#include <stdio.h>
#include <stdlib.h>

bool leapTest (int year)
{
if ((year%4==0 && year%100!=0) or year%400==0)
return true;
else
return false;
}

int leapCompute (int start, int Nth)
{
//if year Y is a leap year,the 1st leap year is year Y
if (leapTest(start))
Nth--;
//else year Y is not a leap year, forward to make year Y multiple of 4
else
{ int res = start%4;
start-=res;
}

while (Nth > 0)
{
start+=4;
if (leapTest(start))
Nth--;
}
return start;
}

int main(int argc, const char * argv[])
{
int n = 0;
while (scanf("%d",&n) != EOF)
{
int start = 0;
int Nth = 0;
for (int i = 0;i < n;i++)
{
scanf("%d",&start);
scanf("%d",&Nth);
int end = leapCompute(start, Nth);
printf("%d\n", end);
}
}
return 0;
}

1.2.3

#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[])
{
char c = 0;
int sum = 0;
int n = 1;
int tmp = 0;
while (scanf("%c",&c) != EOF)
{
if ('#' == c)
exit(0);

if ('\n' == c)
{
printf("%d\n",sum);
sum = 0;
n = 1;
continue;
}

if (' ' == c)//space
tmp = 0;
else
tmp = (int)c - 64;//A-Z:65-90
sum += n*tmp;
n++;
}
return 0;
}

1.2.4

#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[])
{

double sum = 0;
int count = 0;
bool err_flag = false;
double gpa = 0;
char c = 0;
while (scanf("%c",&c) != EOF)
{
if ('\n' == c)
{
if (err_flag)
printf("Unknown letter grade in input\n");
else
printf("%4.2f\n",gpa);
sum = 0;
count = 0;
err_flag = false;
gpa = 0;
continue;
}

if (' ' == c)
continue;

if (c=='A')
sum += 4;
else if (c=='B')
sum += 3;
else if (c=='C')
sum += 2;
else if (c=='D')
sum += 1;
else if (c=='F')
sum += 0;
else
{
err_flag = true;//other characters:"Unknown letter grade in input"
continue;
}

count++;
gpa = sum/count;
//printf("count=%d\n",count);
//printf("sum=%f\n",sum);
//printf("gpa=%f\n",gpa);
}
return 0;
}

1.2.5

#include <stdio.h>
#include <stdlib.h>

bool triangleTest(int *a, int *b, int *c)
{
int tmp = 0;
if (*b > *a) //make a >= b
{
tmp = *b;
*b = *a;
*a = tmp;
}

if (*c > *b) //make b >= c
{
tmp = *c;
*c = *b;
*b =tmp;
}

if (*b > *a) //make a >= b, now (a,b,c) is (max,mid,min)
{
tmp = *b;
*b = *a;
*a = tmp;
}

if ((*b+*c) > *a)
return true;
else
return false;
}

void triangleClassify(int a,int b,int c) //(a,b,c) is (max,mid,min)
{
if (a*a == b*b + c*c)
printf("good\n");
else if (b == c or a==b)
printf("perfect\n");
else
printf("just a triangle\n");
}

int main(int argc, const char * argv[])
{
int n = 0;
while (scanf("%d",&n) != EOF)
{
for (int i = 0;i < n;i++)
{
int a,b,c = 0;
scanf("%d%d%d",&a,&b,&c);
if (triangleTest(&a,&b,&c))
triangleClassify(a,b,c);
else
continue;
}
}
return 0;
}

1.2.6

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int lowestBit (int number)
{
int count = 0;
while ((number%2) == 0)
{
//printf("number=%d\n",number);
number = number >> 1;
count++;
}
return pow(2,count);
}

int main(int argc, const char * argv[])
{
int number = 0;
while (scanf("%d",&number) != EOF)
{
if (number == 0)
exit(0);
int res = lowestBit(number);
printf("%d\n", res);
}
return 0;
}

1.2.7

 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define ID_card_len 18

void fun (char *buf)
{
int areaFlag = 0;
areaFlag = (buf[0] - 0x30)*10 + buf[1] - 0x30;

char *p = buf;
int i = 0;
char year[4+1];
for (i=0,p=&buf[6]; i<4; i++,p++)
{
year[i] = *p;
}
year[4] = '\0';

char month[2+1];
month[0] = buf[10];
month[1] = buf[11];
month[2] = '\0';

char day[2+1];
day[0] = buf[12];
day[1] = buf[13];
day[2] = '\0';

char area[21];
switch (areaFlag)
{
case 33:
strcpy(area,"Zhejiang\0");
break;
case 11:
strcpy(area,"Beijing\0");
break;
case 71:
strcpy(area,"Taiwan\0");
break;
case 81:
strcpy(area,"Hong kong\0");
break;
case 82:
strcpy(area,"Macao\0");
break;
case 54:
strcpy(area,"Tibet\0");
break;
case 21:
strcpy(area,"Liaoning\0");
break;
case 31:
strcpy(area,"Shanghai\0");
break;
default:
break;
}
printf("He/She is from %s,and his/her birthday is on %s,%s,%s based on the table.\n",
area,month,day,year);
}

int main(int argc, const char * argv[])
{
int n = 0;
while (scanf("%d",&n) != EOF)
{
getchar();
char buf[ID_card_len+2]; //contain '\n',and ad2d '\0'
for (int i=0;i<n;i++)
{
fgets(buf,ID_card_len+2,stdin);
//printf("%s\n", buf);
fun (buf);
}
}
return 0;
}

1.2.8

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int test (char c)
{
int res = 0;
if (c>='a' && c<='z')
res = 1;
if (c>='A' && c<='Z')
res = 2;
return res;
}

void fun (char *c)
{
int res = 0;
if (*c=='a' or *c=='e' or *c=='i' or *c=='o' or *c=='u' or
*c=='A' or *c=='E' or *c=='I' or *c=='O' or *c=='U')
{
res = test(*c);//vowel to uppercase
if (res == 1)
*c = *c - 0x20;
}
else
{
res = test(*c);//other characters tp lowercase
if (res == 2)
*c = *c + 0x20;
}
}

int main()
{
int n = 0;
char str[1024];
scanf("%d ",&n);//%d后面的空格不要丢了,要不然enter会被gets函数读入。
//控制串的空白字符使在输入流中跳过一个或多个空白字符.
while (n--)
{
gets(str);
char *p = str;
int len = strlen(str);
while (len--)
{
fun(p);
p++;
}
str[len] = '\0';
printf("%s\n", str);
}
return 0;
}

posted @ 2015-05-17 16:35  周鶏  阅读(199)  评论(0编辑  收藏  举报