问题
问题2:求广义的斐波那契数列
/*
广义斐波那契数列
*/
#include <stdio.h>
int function(int n){
if (n == 1 || n == 2 || n == 3)
return 1;
else {
int a1 = 1;
int a2 = 1;
int a3 = 1;
int a;
int i;
for (i = 4; i <= n; i++){
a = a1 + a2 + a3;
a1 = a2;
a2 = a3;
a3 = a;
}
return a;
}
}
int main(){
int n;
scanf("%d", &n);
int number = function(n);
printf("%d", number);
return 0;
}
问题3:从1到55,选出能被3整除,且至少有一位上的数是5的那些数,求出个数
#include <stdio.h>
int function(int s){
int total = 0;
while (s){
if (s % 10 == 5)
total++;
s = s / 10;
}
if (total != 0)
return 1;
return 0;
}
int main(){
int i;
int count = 0;
for (i = 1; i <= 55; i++){
if (i % 3 == 0&&function(i)){
count++;
}
}
printf("%d\n", count);
return 0;
}
问题4:查询成绩最高的学生记录
/*
查询成绩最高的学生记录
注意返回学生实体
*/
#include <stdio.h>
struct student{
char name[10];
int grade;
}s[3];
struct student sort(struct student a[3]){
int i, j;
struct student temp;
for (i = 0; i < 3;i++)
for (j = 0; j < 3 - i - 1; j++){
if (a[j].grade<a[j + 1].grade){
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
return a[0];
}
int main(){
int i;
for (i = 0; i < 3; i++)
scanf("%s%d", &s[i].name, &s[i].grade);
struct student temp = sort(s);
printf("%s%d", temp.name, temp.grade);
return 0;
}