杭电oj2007c语言答案,杭电OJ第11页2070~2074算法题(C语言)

本文解析了四道编程题,包括斐波那契数列、求最大值、统计单词数和计算路径长度等问题,提供了完整的C语言代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

2070.Fibbonacci Number

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

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

07b5cf83363a441e5d00327e1eb8a0a8.png

分析:找出规律,事先用数组记录结果。

#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

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++;

}

}

标签:11,OJ,pic,C语言,单词,while,y1,Input,y2

来源: https://blog.youkuaiyun.com/weixin_43004044/article/details/112728047

### HDU OJ 1019 C语言解决方案 针对HDU OJ编号为1019的问题,此题目涉及动态规划算法的应用。对于输入部分,程序需先读取一个整数C来确定测试数据的数量。随后,每一组测试案例由两部分组成:首先是两个整数n和m,代表矩阵的行数与列数;接着是具体的矩阵元素值,这些数值范围限定在-100到100之间[^3]。 下面展示了一个可能用于解决这个问题的C语言实现方式: ```c #include <stdio.h> #include <stdlib.h> int main() { int cases; scanf("%d", &cases); while (cases--) { int n, m; scanf("%d %d", &n, &m); int matrix[n][m]; // Read the input values into a two-dimensional array. for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ scanf("%d", &matrix[i][j]); } } // Initialize DP table with appropriate dimensions and fill it according to problem requirements. int dp[m]; for(int col = 0; col < m; ++col) { dp[col] = matrix[0][col]; } // Update dynamic programming table based on given conditions. for(int row = 1; row < n; ++row){ int new_dp[m]; for(int col = 0; col < m; ++col){ if(col == 0){ // First column special case handling. new_dp[col] = dp[col] + abs(matrix[row][col]-matrix[row-1][col]); }else{ new_dp[col] = fmin(dp[col],new_dp[col-1]) + abs(matrix[row][col]-matrix[row-1][col]); } } // Copy updated results back to original dp[]. for(int k = 0; k<m ;++k){ dp[k]=new_dp[k]; } } // Output result after processing all rows of current test case. printf("%d\n", dp[m-1]); } return 0; } ``` 上述代码实现了对给定二维数组的操作,并通过构建一维DP表来进行状态转移计算。注意,在处理第一列时进行了特别考虑以确保逻辑正确性。最后输出的是经过所有变换后的最终结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值