1、 简单输出整数 C
#include <stdio.h>
#include <stdlib.h>
void printN(int n);
int main()
{
int n;
scanf("%d",&n);
PrintN(n);
return 0;
}
void PrintN(int n){
for(int i=1; i<n; i++){
printf("%d\n",i);
}
printf("%d",n);
}
!细节问题:注意大小写(PrintN写成printN)
2、 多项式求值
Q:本题要求实现一个函数,计算阶数为n
,系数为a[0]
... a[n]
的多项式f(x)=∑i=0n(a[i]×xi) 在x
点的值。
#include <stdio.h>
#include <math.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=0;
for(int i=0; i<=n; i++){
sum+=(a[i]*pow(x,i));
}
return sum;
}
!小tips:数的n次方可用math.h头文件里的pow(x,i)方法
3、 简单求和
#include <stdio.h>
#define MAXN 10
int Sum ( int List[], int N );
int main ()
{
int List[MAXN], N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%d", &List[i]);
printf("%d\n", Sum(List, N));
return 0;
}
int Sum( int List[], int N){
int sum=0;
for(int i=0; i<N; i++){
sum+=List[i];
}
return sum;
}
4、 求自定类型的平均
#include <stdio.h>
#define MAXN 10
typedef float ElementType;
ElementType Average( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Average(S, N));
return 0;
}
ElementType Average( ElementType S[], int N)
{
ElementType ave=0;
for( int i=0; i<N; i++){
ave+=S[i];
}
return ave/N;
}
!小tips:ElementType是随时在开头可修改的类型,ave也要用ElementType类
5、 自定类型最大值
#include <stdio.h>
#define MAXN 10
typedef float ElementType;
ElementType Max( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Max(S, N));
return 0;
}
ElementType Max( ElementType S[], int N)
{
ElementType max=S[0];
for(int i=1; i<N; i++){
if(S[i]>max)
max = S[i];
}
return max;
}
6、 单链表整型的阶乘和
#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;
}
int FactorialSum(List L)
{
int sum=0,j;
List p=L;
while(p!=NULL)
{
j=1;
for(int i=2;i<=p->Data;i++)
{
j*=i;
}
sum+=j;
p=p->Next;
}
return sum;
}
!小tips:使用了头插法初始化单链表
7、 统计某类完全平方数
#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>
int n[10];
void initialArray(int n[])
{
for(int i=0; i<10; i++)
{
n[i]=0;
}
}
int IsTheNumber( const int N)
{
int num=N,s=sqrt(num);
initialArray(n);
if(s*s!=N)
return 0;
while(num!=0)
{
n[num%10]++;
num/=10;
}
for(int i=0;i<10;i++)
{
if(n[i]>=2)
return 1;
}
return 0;
}
!细节问题:完全平方数的确认sqrt(int)一定要赋值给一个int型,避免精确度不够出错
8、 简单阶乘计算
#include <stdio.h>
int Factorial( const int N );
int main()
{
int N, NF;
scanf("%d", &N);
NF = Factorial(N);
if (NF) printf("%d! = %d\n", N, NF);
else printf("Invalid input\n");
return 0;
}
int Factorial (const int N)
{
int n=1;
if(N>=0)
{
for(int i=2; i<=N; i++)
{
n*=i;
}
return n;
}
else return 0;
}
!细节问题:0!=1
9、 统计各位数字
#include <stdio.h>
int Count_Digit ( const int N, const int D );
int main()
{
int N, D;
scanf("%d %d", &N, &D);
printf("%d\n", Count_Digit(N, D));
return 0;
}
int Count_Digit ( const int N, const int D )
{
int n[10];
for(int i=0; i<10; i++)
n[i]=0;
int num=abs(N);
if(N==0) n[0]=1;
while(num!=0)
{
n[num%10]++;
num/=10;
}
return n[D];
}
!细节问题:测试点0,测试点2;
??? 10、 阶乘计算升级版(未完成)
#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 )
{
int n[4000],w=0,sum;
for(int s=1;s<4000;s++)
{
n[s]=0;
}
n[0]=1;
int h,j=0,p;
if(N<0) printf("Invalid input");
else if(N==0) printf("1");
else
{
for(int i=2;i<=N;i++){
for(j=0,p=0,h=j;j<=w;j++)
{
h=j;
sum=n[j]*i; //sum=594
n[h]=sum%10+p%10; //
p=p/10+sum/10;
sum/=10;
while(n[h]>=10){
n[h]-=10;
p++;
}
}
if(p!=0)
{
//printf("p=%d ",p);
n[++w]=p;
}
}
for(int i=w;i>=0;i--){
printf("%d",n[i]);
}
}
}
!数组版乘法。
为什么会有负数。。