Chinaunix首页 | 论坛 | 博客
  • 博客访问: 145086
  • 博文数量: 29
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 265
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-04 13:11
文章分类

全部博文(29)

文章存档

2015年(2)

2014年(27)

我的朋友

分类: C/C++

2014-01-10 09:31:34

题目1:八进制转换为十进制

#include

int main(void)

{

         char *p,s[6];

         int n = 0;

         p = s;

         gets(p);

         while(*(p)!='\0') //'0'ascll码值是48*p是字符型

         {

                   n = n*8+*p-'\0';

                   p++;

         }

         printf("%d\n",n);

         return 0;

}

题目2:两个字符串连接程序

#include

int main(void)

{

         char a[10],b[10],c[20];

         int i=0,j=0,k=0;

         gets(a);

         gets(b);

         while(a[i]!='\0')

         {

                   c[k]=a[i];

                   i++;

                   k++;

         }

         while(b[j]!='\0')

         {

                   c[k]=b[j];

                   j++;

                   k++;

         }

         c[k]='\0';     //最后加上字符串结束符

         printf("%s\n",c);

         //puts(c);    

         return 0;

}

题目3:结构体变量传递程序

include "stdio.h"

struct student

{

    int x;

    char c;

}a;

void f1(struct student b);

void f2(struct student* b);

void main(void)

{

    a.x=3;

    a.c='a';

    f1(a);

    printf("%d,%c\n", a.x, a.c);   // 3 ‘a’ 结构体变量作形参时,其成员的值不会变

    f2(&a);

    printf("%d,%c\n", a.x, a.c);   // 20 ‘y’ 结构体变量的地址作形参时,其成员的值会变

}

void f1(struct student b)

{

    b.x=20;

    b.c='y';

}

void f2(struct student* b)

{

    b->x = 20;

    b->c = 'y';

}

题目4:某公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,规则如下: 每位数字都加上5,然后用除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

#include "stdio.h"

int main(void)

{

         int n,i,t;

         int a[4];

         scanf("%d",&n);

         a[0] = n%10;

         a[1] = n%100/10;

         a[2] = n%1000/100;

         a[3] = n/1000;

         for(i=0;i<=3;i++)

         {

                   a[i] = a[i]+5;

                   a[i] = a[i]%10;

         }

         for(i=0;i<=3/2;i++)

         {       

                   t = a[i];

                   a[i] = a[3-i];

                   a[3-i] = t;

         }

 

         for(i=3;i>=0;i--)

         {

                   printf("%d",a[i]);

         }

         return 0;

}

题目5:按位与,或,异或,同或

#include "stdio.h"

int main(void)

{

         int a,b,c,d,e;

         int i,j;

         i = 0xff;

         j = 0xfe;

         a = i&j;

         b = i|j;

         c = i^j;

         d = !i^j;

         e = ~j;    //-255   1111 1110取反后0000 0001

-255的表示方法是0000 0000取反加1,即0000 0001

         printf("%d %d %d %d %d\n",a,b,c,d,e);

         return 0;

}

题目6:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

#include "stdio.h"

int main(void)

{

         int i,j,k,n;

         n=0;

         for(i=1;i<5;i++)

         {

                   for(j=1;j<5;j++)

                   {

                            for(k=1;k<5;k++)

                            {

                                     if((i!=j)&&(i!=k)&&(j!=k))

                                     {

                                               printf("%d%d%d\n",i,j,k);

                                         n++;

                                     }                

                            }

                   }

         }

         printf("%d",n);

         return 0;

}

题目7:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

#include "stdio.h"

#include "math.h"

int main(void)

{

         long int i,x,y;

         for(i=0;i<10000;i++)

         {

                   x = sqrt(i+100);

                   y = sqrt(i+268);

                   if((x*x==i+100)&&(y*y==i+268))

                            printf("%ld\n",i);

         }

         return 0;

}

题目8:输入某年某月某日,判断这一天是这一年的第几天?

#include "stdio.h"

int main(void)

{

         int year,mon,day;

         int leap,sum;

scanf("%d %d %d",&year,&mon,&day);

         switch(mon)

         {

                   case 1:sum=0;break;

                   case 2:sum=31;break;

                   case 3:sum=59;break;

                   case 4:sum=90;break;

                   case 5:sum=120;break;

                   case 6:sum=151;break;

                   case 7:sum=181;break;

                   case 8:sum=212;break;

                   case 9:sum=243;break;

                   case 10:sum=273;break;

                   case 11:sum=304;break;

                   case 12:sum=334;break;

 

                   default :printf("data error");break;

         }

         sum = sum+day;

         if(year%4 == 0)

                   leap=1;                      

         else 

                   leap=0;

         if(leap==1) sum = sum+1;

         printf("It is the %d th day",sum);

         return 0;

}

题目9:该程序读取输入直到遇到#字符,然后报告空格数目,换行符数目。

#include "stdio.h"

int main(void)

{

         char c;

         int i,j;

         i=0;

         j=0;

         while((c=getchar())!='#')

         {

                   if(c==' ')

                            i++;

                   if(c=='\n')

                            j++;

         }

         printf("the end\n");

         printf("spaces:%d newlines:%d\n",i,j);

         return 0;

}

题目10:指向指针的指针

#include "stdio.h"

int main(void)

{

         char *s[]={"man","woman","baby"};

         char **q;

         int k;

         for(k=0;k<3;k++)

         {

                   q=&(s[k]);

                   printf("%s\n",*q);

         }

         return 0;

}

 

题目11:输入四个同学的分数,最后打印出他们的分数,分数平均值和每一科的平均值

#include

#include

#define LEN 14

#define CSIZE 4

#define SCORES 3

struct name {

         char first[LEN];

         char last[LEN];

};

struct student {

         struct name person;

         float scores[SCORES];

         float mean;

};

void get_scores(struct student al[],int lim);

void find_means(struct student al[],int lim);

void show_class(const struct student ar[], int lim);

void show_ave (const struct student ar[], int lim);

int main()

{

         struct student classes[CSIZE] = {

                   {"File","Snide"},

                   {"Clare","Voyans"},

                   {"Bingo","Higgs"},

                   {"Fawn","Hunter"}

         };

         get_scores(classes,CSIZE);

         find_means(classes,CSIZE);

         show_class(classes,CSIZE);

         show_ave (classes,CSIZE);

         return 0;

}

void get_scores(struct student al[],int lim)

{

         int i,j;

         for(i=0;i

         {

                   printf("please enter %d scores for %s %s:\n",SCORES,al[i].person.first,al[i].person.last);

                   for(j=0;j

                   {

                            while(scanf("%f",&al[i].scores[j]) != 1)// 1时表示输入与类型不符合

                            {

                                     scanf("%*s");//跳至下一空白字符,不把此数据读入

                                     puts("Please use numeric input");

                            }

                   }

         }

}

void find_means(struct student al[],int lim)

{

         int i,j;

         float sum;

         for(i=0;i

         {

                   for(sum=0,j=0;j

                            sum = sum+al[i].scores[j];

                   al[i].mean = sum / SCORES;

         }

}

void show_class(const struct student ar[], int lim)

{

         int i, j;

         char wholename[2*LEN];

         for (i = 0; i < lim; i++)

         {

                   strcpy(wholename, ar[i].person.first);//复制字符串函数

                   strcat(wholename, " ");//字符串连接函数,先去掉结束符,连接后再加上结束符

                   strcat(wholename, ar[i].person.last);

                   printf("%27s: ", wholename); //输出27

                   for (j = 0; j < SCORES; j++)

                            printf("%6.1f ", ar[i].scores[j]);

                   printf(" Average = %5.2f\n", ar[i].mean);

         }

}

void show_ave (const struct student ar[], int lim)

{

         int i, j;

         float total;

         printf("\n%27s: ", "QUIZ AVERAGES");

         for (j = 0; j < SCORES; j++)

         {

                   for (total = 0, i = 0; i < lim; i++)

                            total += ar[i].scores[j];

                   printf("%6.2f ", total / lim);

         }

         putchar('\n');

}

 

题目12:用指向函数的指针数组执行菜单

#include

#define NUM 4

double twice(double x);

double half(double x);

double thrice(double x);

void showmenu(void);

int main()

{

         double (*pf[NUM])(double) = {twice,half,thrice}; //声明指向函数的指针数组

         double val,ans;

         int sel;

         printf("enter a number:");

         while(scanf("%lf",&val)&&val>0)

         {

                   showmenu();

                   while(scanf("%d",&sel)&&sel>=0&&sel<3)

                   {

                            ans = (*pf[sel])(val);

                            printf("answer = %f\n",ans);

                            showmenu();

                   }

                   printf("enter next number:");

         }

         return 0;

}

void showmenu()

{

         printf("enter one of the following choices:\n");

         puts("0) double the value\n");

         puts("1) halve the value\n");

         puts("2) triple the value\n");

}

double twice(double x) {return 2.0 * x;}

double half(double x) {return x / 2.0;};

double thrice(double x) {return 3.0 * x;}

 

 

 

 

 

 

阅读(1517) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~