目录
语法
float型 相关
负数求余
负数求余:-123%10 = -3 结果仍未负数
6-2 多项式求值
函数接口定义:
double f( int n, double a[], double x );
其中n
是多项式的阶数,a[]
中存储系数,x
是给定点。函数须返回多项式f(x)
的值。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
double f( int n, double a[], double x );
int main()
{
int n, i;
double a[MAXN], x;
scanf("%d %lf", &n, &x);
for ( i=0; i<=n; i++ )
scanf("%lf", &a[i]);
printf("%.1f\n", f(n, a, x));
return 0;
}
/* 你的代码将被嵌在这里 */
代码实现
double f( int n, double a[], double x ){
double sum,temp=1;
sum=a[0];
for(int i=1;i<=n;i++){
temp*=x;
sum+=a[i]*temp;
}
return sum;
}
6-6 求单链表结点的阶乘和
函数接口定义:
int FactorialSum( List L );
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
int Data; /* 存储结点数据 */
PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */
int FactorialSum( List L );
int main()
{
int N, i;
List L, p;
scanf("%d", &N);
L = NULL;
for ( i=0; i<N; i++ ) {
p = (List)malloc(sizeof(struct Node));
scanf("%d", &p->Data);
p->Next = L; L = p;
}
printf("%d\n", FactorialSum(L));
return 0;
}
/* 你的代码将被嵌在这里 */
注:
需要考虑到0、1的阶乘,链表为空的情况。
代码实现:
这样做有点暴力求解的意思,感觉还能再优化一下
int FactorialSum( List L ){
int res=0;
PtrToNode p=L;
while(p!=NULL){
int temp=1;
for(int i=1;i<= p->Data;i++)
temp*=i;
res+=temp;
p=p->Next;
}
return res;
}
6-7 统计某类完全平方数
函数接口定义:
int IsTheNumber ( const int N );
其中N
是用户传入的参数。如果N
满足条件,则该函数必须返回1,否则返回0。
裁判测试程序样例:
#include <stdio.h>
#include <math.h>
int IsTheNumber ( const int N );
int main()
{
int n1, n2, i, cnt;
scanf("%d %d", &n1, &n2);
cnt = 0;
for ( i=n1; i<=n2; i++ ) {
if ( IsTheNumber(i) )
cnt++;
}
printf("cnt = %d\n", cnt);
return 0;
}
/* 你的代码将被嵌在这里 */
/*分析:
* 统计两数之间共有多少个该类完全平方数
* 如何判断是完全平方数
* 如何判断至少有两位数字相同
*/
代码实现一:
注:在裁判测试程序样例中引入了#include <math.h>,所以可以使用double sqrt(double x)
函数进行开方,注意大多数情况下开方的不是double型,需要强制类型转换,例如本题。与sqrt()对应的是double pow(double x, double y)
int IsTheNumber ( const int N ){
int i=(int)sqrt((int)N);
if(i*i==N){
int s=N;
int a[10]={0};
while(s!=0){
a[s%10]++;
if(a[s%10]==2) return 1;
s=s/10;
}
}
return 0;
}
代码实现二:
注:传入的参数为const int 型,故函数中不能对N进行修改值的操作,需要再定义一个int s进行判断是否含有相同的数字
int IsTheNumber ( const int N ){
if(N<100) return 0; //小于100且两位相同的一定不是完全平方数
//判断是完全平方数
for(int i=1;i<N/2;i++){
if(i*i==N){
//判断至少有两位数字相同
int a[10]={0};
int s=N;
while(s!=0){
int k=s%10;
a[k]++;
if(a[k]==2) return 1;
s/=10;
}
}
}
return 0;
}
6-8 简单阶乘计算
注:
1.本题虽然说是非负数的阶乘,但是在测试点仍然存在负数。
2.看题目时也要考虑裁判测试程序样例中的逻辑,由其逻辑可得负数的阶乘返回0
3.0的阶乘是1
6-10 阶乘计算升级版!
本题要求实现一个打印非负整数阶乘的函数。N
是用户传入的参数,其值不超过1000。如果N
是非负整数,则该函数必须在一行中打印出N
!的值,否则打印“Invalid input”。
裁判测试程序样例:
#include <stdio.h>
void Print_Factorial ( const int N );
int main()
{
int N;
scanf("%d", &N);
Print_Factorial(N);
return 0;
}
/* 你的代码将被嵌在这里 */
代码实现:
void Print_Factorial ( const int N ){
if(N<0){
printf("Invalid input");
return;
}
int res[3000]={0};
int k=0,len=1;//k为进位值,len为数组长度
res[0]=1;
for(int i=2;i<=N;i++){//0、1的阶乘均为1,不需要再计算
for(int j=0;j<len;j++){
int temp=res[j]*i+k;
res[j]=temp%10;
k=temp/10;
}
while(k!=0){//如果还有进位,则需要延长数组长度
res[len]=k%10;
len++;
k=k/10;
}
}
for(int i=--len;i>=0;i--)
printf("%d",res[i]);
}
7-1 厘米换算英尺英寸
代码实现
#include<stdio.h>
void PrintN ( int N );
int main ()
{
int m;
scanf("%d", &m);
int foot,inch;
foot=m/30.48;
inch=(m/30.48-foot)*12;
printf("%d %d",foot,inch);
return 0;
}