资源描述
CP5
/*4、编写程序输入一位学生的生日(年:y0、月:m0、日:d0);
并输入当前的日期(年:y1、 月:m1、日:d1);
输出改学生的实足年龄。 (P116) */
main( )
{
int y0, m0, d0, y1, m1, d1, age;
printf("Please input current day:\n");
scanf("%d,%d,%d",&y1,&m1,&d1);
printf("Please input birthday:\n");
scanf("%d,%d,%d",&y0,&m0,&d0);
age = y1-y0;
if ( m1<m0 )
age--;
else if(m1==m0&&d1<d0) age--;
printf("age=%d\n",age);
}/*7、某商店位促销推出如下让利销售方案,其中 M 位购买金额,n 为让利
百分比。
M<100, N=0; 100<=M<200, N=1.5% 200<=M<300, N=2.5%
300<=M<400,
N=3.5% 400<=M<500, N=4.5% 500<=M<600, N=5.5% M>600,
N=6%; 编写程序,对输入的购买金额,输出顾客购买金额、实际支出金
额和返还金额。*/
main( )
{
int m,k;
float n;
printf ("Please input 购买金额 m :\n");
scanf ("%d",&m);
k = m/100;
switch (k)
{
case 0: n=0;
break;
case 1: n=0.015;
break;
case 2: n=0.025;
break;
case 3: n=0.035;
break;
case 4: n=0.045;
break;
case 5: n=0.055;
break;
default: n=0.06;
break;
}
printf("%5d,%7.2f,%7.2f",m,m-n*m,m*n);/*输出购买金额、实
际支付金额和返还金额 */
}
===========================================
CP6
5./*计算6个学生5门课成绩,每人的平均成绩。*/
#include<stdio.h>
void main()
{
int i,j,xuesheng;
double fen,mean,zongfen;
for(i = 1; i <= 6; i ++)
{
zongfen = 0;
for(j = 1; j <= 5; j ++)
{
scanf("%lf",&fen);
zongfen = zongfen + fen;
}
mean = zongfen / 5.0;
printf("xuesheng%d = %lf\n",i,mean);
}
}
//8.所有大于1010的四位偶数,该偶数各位数字两两不相同。
#include<stdio.h>
void main()
{
int t,a,b,c,d;
for(t=1010;t<9999;t++)
{
if(t%2==0)
{
a=(t/1000)%10;
b=(t/100)%10;
c=(t/10)%10;
d=t%10;
if((a!=b) && (a!=c) && (a!=d) && (b!=c) && (b!=d) && (c!=d))
printf("%d,",t);
}
}
}
===========================================
cp8
/*1、编写一个判断一个整数是否是素数的函数,使用该函数编写验证 1000
以内的哥德巴赫猜想是成 立。
(每个不小于 6 的偶数都是两个素数之和)*/
#include <stdio.h>
main( )
{
int i, j;
for ( i=6; i<=1000; i+=2 )
for ( j=3; j<=i/2; j+=2 )
if ( flag(j) && flag(i-j) )
{
printf("%d=%d+%d\n", i, j, i-j);
break;
}
}
flag (n) /* 函数 flag 的功能是判断整数 n 是否为素数 */
int n;
{
int i;
if ( n%2 == 0)
return(0);
for ( i=3; i<n/2; i++ )
if ( n%i==0 )
return(0);
return(1);
}
/* 3、编写一个求水仙花数的函数,求 100 到 999 之间的全部水仙花数。
所
谓水仙花数是指一个三位数, 其各位数字立方的和等于该数。例如:153
就是一个水仙花数: 153 = 1*1*1 + 5*5*5 + 3*3*3 参考程序:*/
main( )
{
int i=0,j,k,a,b,c,s;
for (a=1; a<=9; a++)
for (b=0; b<=9; b++)
for (c=0; c<=9; c++)
{
j=100*a+10*b+c;
if ( ex(j) )
printf ("%d=%d*%d*%d+%d*%
d*%d+%d*%d*%d\n", j,a,a,a,b,b,b,c,c,c);
}
}
int ex (int m)
{
int sum=0, z, k;
z=m;
while (z>0)
{
k= z%10;
sum += k*k*k;
z /= 10;
}
return ( m==sum );
}
/*4、请编写一个函数,输出整数m的全部素数因子。例如:m=120 时
,因子为: 2,2,2,3,5 参考答案:*/
main ( )
{
int m;
printf ("\nEnter m=");
scanf ("%d", &m);
primedec (m);
}
primedec(m)
int m;
{
int k=2;
while (k<=m)
if (m%k == 0 )
{
printf ("%d, " , k);
m = m/k;
}
else k++;
}
==========================================
cp9
/*1、编写一函数,其功能是交换两个变量 x、y 的值。编程序实现对数组
a[100],b[100]调用 此函数,交换 a、b 中具有相同下标的数组元素的值,
且输出交换后的 a、b 数组。
/*p279_1.c*/
//this is a wrong program
#include "stdlib.h"
void swap(int *pa,int *pb)
{
int t;
t=*pa;*pa=*pb;*pb=t;
}
main()
{
int a[100],b[100],i;
for(i=0;i<100;i++) /*产生数组a和b */
{
a[i]=random(100);
b[i]=random(100);
}
printf("before swap A:\n"); /*输出交换前数组a */
for(i=0;i<100;i++)
printf("%3d",a[i]);
printf("\n");
printf("before swap B:\n"); /*输出交换前数组b */
for(i=0;i<100;i++)
printf("%3d",b[i]);
printf("\n");
for(i=0;i<100;i++) /*a、b数组元素交换 */
swap(&a[i],&b[i]);
printf("after swap A:\n"); /*输出交换后数组a */
for(i=0;i<100;i++)
printf("%3d",a[i]);
printf("\n");
printf("after swap B:\n");/*输出交换后数组b */
for(i=0;i<100;i++)
printf("%3d",b[i]);
printf("\n");
}
//this is a wrong program
#include<stdio.h>
void swap(int *x,int *y)
{
int temp;
temp=*x;*x=*y;*y=temp;
}
void main()
{
int a[100],b[100],i;
i=0;
printf("enter y to reset the array or other to quit
reset:");
if(getch()=='y')
{
for(i=0;i<100;i++){printf("a[%d]:",i);
scanf("%d",a+i);}
for(i=0;i<100;i++)
{
printf("b[%d]:",i);
scanf("%d",a+i);
}
}
printf("a\tb\n");
for(i=0;i<100;i++)
printf("%d\t%d\n",a[i],b[i]);
while(i<100)
{
swap(a+i,b+i);
i++;
}
printf("\n\na\tb\n");
for(i=0;i<100;i++)
printf("%d\t%d\n",a[i],b[i]);
}
//7、将空格分开的字符串称为单词。输入多行字符串,直到输入"stop"单词
时才停止。最后输 出单词的数量。
//解 1:
#include "string.h"
main()
{
char s[200];
long n=0;
int i,m;
gets(s);
while(strcmp("stop",s)!=0)
{
m=strlen(s);
for(i=0;i<m;i++)
{
if(s[i]!=' '&&s[i+1]==' ')n++;
if(s[i]!=' '&&s[i+1]=='\0')n++;
if(s[i]==' '&&s[i+1]==' ')continue;
if(s[i]==' '&&s[i+1]!=' ')continue;
}
gets(s);
}
printf("n=%ld\n",n);
}
//解 2.
#include "string.h"
main()
{
char s[80],*p=s;
int n=0;
printf("Input a string:\n");
gets(s);
while(strcmp(s,"stop")!=0)
{
p=s;
while(*p!='\0')
{
if(*p==32||*p=='\0')n++;
p++;
}
if(strlen(s)>0)n++;
printf("Input a string:\n");
gets(s);
}
printf("words number:%d\n",n);
}
/*8、将输入的两行字符串连接后,将串中的空格全部移到串首后输出。 /*
P280_8A */
#include "string.h"
main()
{
char s1[80],s2[40],*p1=s1,*p2=s2;
printf("Input 2 string:\n");
gets(s1);
gets(s2);
while(*p1!='\0')p1++;/*指针指向s1的末尾*/
while(*p2!='\0')/*连接s2*/
*p1++=*p2++;
*p1='\0';
while(p1!=s1) /*将空格移到字符串前面*/
{
if(*p1!=32)
{
p1--;
continue;
}
else
{
p2=p1;
while(p2!=s1) /*将空格之前的所有字符向后移动一个字
符位置*/
{
*p2=*(p2-1);
p2--;
}
*p2=' ';
p1--;
}
}
puts(s1);
}
/*11、设一个以符号'.'结束的英文句子长度小于 80 字节。请编写程序读
入改句子,并检查其 是否为回文(即正读和反读都是一样的,不考虑空
格和标点符号)。例如:
读入:madam i'm adam. 输出:yes
读入:abcdba 输出:no /* P280_11.c */
#include "string.h"
main()
{
char a[20],*ps,*pe;
int n;
gets(a);
n=strlen(a);
ps=a;pe=ps+n;
while(ps<=pe)
{
if(*ps<'A'||*ps>'Z'&&*ps<'a'||*ps>'z')ps++;
if(*pe<'A'||*pe>'Z'&&*pe<'a'||*pe>'z')pe--;
if(*ps!=*pe)
break;
ps++;pe--;
}
if(ps>pe)printf("%s:yes!\n",a);
else
printf("%s:No!\n",a);
}
===========================================
cp10
//1.错的
#include<iostream.h>
struct std //定义学生结构体,包含学好number,成绩
score,名次mc
{
int number;
int score;
int mc;
};
#define N 100
int main()
{
int i,j,max,num;
static int t=1;
struct std stduent[N],temp;
cin>>max;
for(i=0;i<max;i++)
{
cin>>stduent[i].number;
cin>>stduent[i].score;
}
for(i=0;i<max-1;i++) //冒泡法排序,从高分到低分
{
for(j=0;j<max-i;j++)
if(stduent[j].score<stduent[j+1].score)
{
temp=stduent[j];
stduent[j]=stduent[j+1];
stduent[j+1]=temp;
}
}
for(i=0;i<max-1;i++) //通过前面的冒泡法排序,已知成
绩从高到低排序,就可以确定学生的名次,成绩相同,名次相同,否侧加一
{
if(stduent[i].score>stduent[i+1].score)
{
stduent[i].mc=t++;
stduent[i+1].mc=t;
}
else if(stduent[i].score=stduent[i+1].score)
{
stduent[i].mc=t;
stduent[max-1].mc=t;
}
}
cout<<" 成绩 "<<" 学号 "<<" 名次
"<<endl;
for(i=0;i<max;i++)
cout<<" "<<stduent[i].score<<"
"<< stduent[i].number<<" "<<stduent[i].mc<<endl;
cout<<"************************************************************
******************"<<endl;
for(i=1;i<=stduent[max-1].mc;i++) //输出相同名次的人数和学号
,同名次的学号输出在同一行中,一行最多输出10个学号
{
num=0;
for(j=i-1;j<=max-1;j++)
{
if(i==stduent[j].mc)
{
num++;
cout<<stduent[j].number<<" ";
}
if(0==num%10) cout<<endl;
}
cout<<"第"<<i<<"名有:"<<num<<"个"<<endl;
}
return 0;
}
展开阅读全文