目录
2070.Fibbonacci Number
- 2070.Fibbonacci Number
- 2071.Max Num
- 2072.单词数
- 2073.无限的路
- 2074.叠筐
Problem Description Your objective for this question is to develop a program which will generate a fibbonacci number. The fibbonacci function is defined as such: f(0) = 0 f(1) = 1 f(n) = f(n-1) + f(n-2) Your program should be able to handle values of n in the range 0 to 50. Input Each test case consists of one integer n in a single line where 0≤n≤50. The input is terminated by -1. Output Print out the answer in a single line for each test case. Sample Input 3 4 5 -1 Sample Output 2 3 5 Hint Note: you can use 64bit integer: __int64
分析:直接利用题目给出的递推式编写代码即可。
#include void Fibbonacci(){ __int64 fib[51]={0,1}; int i,n; for(i=2;i<=50;i++){ fib[i]=fib[i-1]+fib[i-2]; } while(scanf("%d",&n)!=EOF && n!=-1){ if(n>=0 && n<=50){ printf("%I64d\n",fib[n]); } } }2071.Max Num
Problem Description There are some students in a class, Can you help teacher find the highest student . Input There are some cases. The first line contains an integer t, indicate the cases; Each case have an integer n ( 1 ≤ n ≤ 100 ) , followed n students’ height. Output For each case output the highest height, the height to two decimal plases; Sample Input 2 3 170.00 165.00 180.00 4 165.00 182.00 172.00 160.00 Sample Output 180.00 182.00
分析:直接比较,然后输出最大的即可。
#include void MaxNum(){ int t,n; double max,h; while(scanf("%d",&t)!=EOF){ //t个案例 while(t--){ scanf("%d",&n); max=0; //n个学生的身高 while(n--){ scanf("%lf",&h); if(h>max){ max=h; } } printf("%.2lf\n",max); } } }2072.单词数
Problem Description lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。 Input 有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。 Output 每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。 Sample Input you are my friend # Sample Output 4
分析:每输入一个单词用数组word保存,并且与二维数组words中的单词进行比较,若刚输入的单词不在words中,则将其保存到其中,并且单词数量count加一。
#include #include void NumberWords(){ //二维数组st的每一行用来保存一个单词,且每行的单词都不一样 char words[100][1000]; char word[1000]=""; char c; int i,j,count,k; while((c=getchar())!='#'){ word[0]=c; j=count=0; k=1; while(c!='\n'){ //将一个完整的单词保存到s中 while((c=getchar())!=' ' && c!='\n'){ word[k++]=c; } word[k]='\0'; //将刚输入的一个完整的单词与之前输入的单词进行比较 for(i=0;i<j;i++){ if(strcmp(words[i],word)==0){ break; } } //若刚输入的单词是新单词,则将其保存到st中 if(i>=j && strlen(word)){ strcpy(words[j],word); count++; j++; } k=0; } printf("%d\n",count); } }2073.无限的路
Problem Description 甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直线,于是他就在平面直角坐标系中画出如下的图形: 甜甜的好朋友蜜蜜发现上面的图还是有点规则的,于是他问甜甜:在你画的图中,我给你两个点,请你算一算连接两点的折线长度(即沿折线走的路线长度)吧。 Input 第一个数是正整数N(≤100)。代表数据的组数。 每组数据由四个非负整数组成x1,y1,x2,y2;所有的数都不会大于100。 Output 对于每组数据,输出两点(x1,y1),(x2,y2)之间的折线距离。注意输出结果精确到小数点后3位。 Sample Input 5 0 0 0 1 0 0 1 0 2 3 3 1 99 99 9 9 5 5 5 5 Sample Output 1.000 2.414 10.646 54985.047 0.000
分析:找出规律,事先用数组记录结果。
#include #include void InfiniteRoad(){ double A[202]={0}; double s1,s2,dis; int i,N,x1,y1,x2,y2; for(i=1;i<202;i++){ A[i]=sqrt(2*i*i)+sqrt(i*i+(i-1)*(i-1))+A[i-1]; } scanf("%d",&N); while(N--){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1==0){ s1=A[y1] - y1*sqrt(2); }else if(y1 == 0){ s1=A[x1]; }else{ s1=A[x1+y1]-y1*sqrt(2); } if(x2==0){ s2 = A[y2]-y2*sqrt(2); }else if(y2 == 0){ s2=A[x2]; }else{ s2=A[x2+y2]-y2*sqrt(2); } dis=fabs(s2-s1); printf("%.3lf\n",dis); } }2074.叠筐
Problem Description 需要的时候,就把一个个大小差一圈的筐叠上去,使得从上往下看时,边筐花色交错。这个工作现在要让计算机来完成,得看你的了。 Input 输入是一个个的三元组,分别是,外筐尺寸n(n为满足0<n<80的奇整数),中心花色字符,外筐花色字符,后二者都为ASCII可见字符; Output 输出叠在一起的筐图案,中心花色与外筐花色字符从内层起交错相叠,多筐相叠时,最外筐的角总是被打磨掉。叠筐与叠筐之间应有一行间隔。 Sample Input 11 B A 5 @ W Sample Output AAAAAAAAA ABBBBBBBBBA ABAAAAAAABA ABABBBBBABA ABABAAABABA ABABABABABA ABABAAABABA ABABBBBBABA ABAAAAAAABA ABBBBBBBBBA AAAAAAAAA @@@ @WWW@ @W@W@ @WWW@ @@@
分析:注意四个角要用空格代替 。
#include void FoldingBasket(){ char pic[100][100]; int n,i,center,j,count=0; char in,out; while(scanf("%d",&n)!=EOF){ getchar(); in=getchar(); getchar(); out=getchar(); if(count>0) printf("\n"); i=1; center=(n+1)/2; pic[center][center]=in;//中心; while(center>=i+1){ int p=center-i,q=center+i;//顶点; for(j=0;j<2*i+1;j++){ pic[p][p+j]=i&1?out:in;//右; pic[p+j][p]=i&1?out:in;//下; pic[q][q-j]=i&1?out:in;//左; pic[q-j][q]=i&1?out:in;//上; } i++; } //四个角用空格代替 if(n!=1){ pic[1][1]=' ',pic[1][n]=' ',pic[n][1]=' ',pic[n][n]=' '; } for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ printf("%c",pic[i][j]); } printf("\n"); } count++; } }
杭电OJ第11页2075~2079算法题(C语言)