Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173640
  • 博文数量: 35
  • 博客积分: 287
  • 博客等级: 二等列兵
  • 技术积分: 353
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-02 21:01
个人简介

君子务本,本立则道生。

文章分类
文章存档

2014年(4)

2013年(12)

2012年(19)

分类: C/C++

2013-03-17 22:19:46


  1. 丢手帕问题:

  2. //筛选法解决
  3. #include<stdio.h>
  4. #define n 10
  5. #define m 3

  6. void main()
  7. {
  8.     int a[n+1]={0};
  9.     int k=n;
  10.     int count;
  11.     int i=0;
  12.     
  13.     while(k>0){
  14.         count=m;
  15.         while(count>0){
  16.             i=(i+1)%(n+1);
  17.             if(i==0)
  18.                 i=1;
  19.             if(a[i]==0)
  20.                 count--;
  21.         }
  22.         a[i]=1;
  23.         printf("%-4d",i);
  24.         k--;
  25.     }
  26.     printf("\nThe last one is %d\n",i);
  27.    
  28. }

  29. //循环链表法解决
  30. #include<stdio.h>
  31. #include<stdlib.h>
  32. #define n 10
  33. #define m 3

  34. struct node{
  35.     int num;
  36.     struct node *next;
  37. };

  38. void creat_link(struct node *head)
  39. {
  40.     struct node *p,*q;
  41.     int i;
  42.     p=head;
  43.     p->num=1;
  44.     for(i=2;i<=n;i++){
  45.         q=(struct node *)malloc(sizeof(struct node));
  46.         q->num=i;
  47.         q->next=head;
  48.         p->next=q;
  49.         p=q;
  50.     }

  51. }

  52. void main()
  53. {
  54.     struct node *head,*p,*q,*temp;
  55.     int count;

  56.     head=(struct node *)malloc(sizeof(struct node));
  57.     creat_link(head);

  58.     p=head;
  59.     while(p!=p->next){
  60.             count=1;
  61.             while(count<m-1){
  62.                 p=p->next;
  63.                 count++;
  64.                 }
  65.                 temp=p->next;
  66.                 printf("%-4d",temp->num);

  67.                 p->next=p->next->next;
  68.                 p=p->next;
  69.                 free(temp);

  70.     }
  71.     printf("\nThe last one is %-4d\n",p->num);
  72.     
  73. }





  74. 2、联合体的应用之十六进制高低倒转
  75. #include<stdio.h>
  76. int main()
  77. {
  78.     union body{
  79.         struct BYTE{
  80.             unsigned char l,h;
  81.         }byte;
  82.         unsigned int word;
  83.     }a,b;
  84.     printf("Enter data?\n");
  85.     scanf("%x",&a.word);
  86.     b.byte.l=a.byte.h;
  87.     b.byte.h=a.byte.l;
  88.     printf("%x->%x\n",a.word,b.word);
  89.     return 0;
  90. }



  91. 链表倒置,采用的是头插法
  92. #include<stdio.h>
  93. #include<stdlib.h>
  94. struct node{
  95.     int num;
  96.     struct node *next;
  97. };

  98. void turn(struct node *head);

  99. int main()
  100. {
  101.     struct node *head,*p,*q;
  102.     int x;
  103.     scanf("%d",&x);
  104.     head=(struct node *)malloc(sizeof(struct node));
  105.     head->next=NULL;
  106.     q=head;
  107.     while(x!=-1){
  108.         p=(struct node *)malloc(sizeof(struct node));
  109.         p->num=x;
  110.         p->next=NULL;
  111.         q->next=p;
  112.         q=q->next;
  113.         scanf("%d",&x);
  114.     }

  115.     turn(head);
  116.     printf("After turning\n");
  117.     p=head->next;
  118.     while(p!=NULL){
  119.         printf("%d ",p->num);
  120.         p=p->next;
  121.     }
  122.     printf("\n");
  123.     getch();
  124.     return 0;
  125. }

  126. void turn(struct node *head)
  127. {
  128.     struct node *p,*q;
  129.     p=head->next ;
  130.     head->next=NULL;
  131.     while(p!=NULL){
  132.           q=p->next;
  133.           p->next=head->next;
  134.           head->next=p;
  135.           p=q;
  136.     }
  137. }


  138. 统计输入的数字串里各数字的个数
  139. #include<stdio.h>

  140. int main()
  141. {
  142.     char str[50];
  143.     int count[10]={0};
  144.     int i=0;
  145.     printf("Enter numbers");
  146.     gets(str);
  147.     while(str[i]!='\0'){
  148.         count[str[i]-'0']++;
  149.         i++;    
  150.     }
  151.     for(i=0;i<10;i++)
  152.         printf("%d:%d\n",i,count[i]);
  153.   return 0;    
  154.   
  155. }


  156. 统计输出输入的学生成绩的最高分和最低分对应的学生的学号
  157. #include<stdio.h>
  158. #define N 100
  159. struct stu{
  160.     int num;
  161.     float score;
  162. };

  163. int main()
  164. {
  165.     struct stu info[N];
  166.     int i;
  167.     struct stu max,min;
  168.     for(i=1;i<=N;i++)
  169.         scanf("%d%f",&info[i].num,&info[i].score);
  170.     max=min=info[1];
  171.     for(i=2;i<=N;i++)
  172.         if(info[i].score>max.score)
  173.             max=info[i];
  174.         else if(info[i].score<min.score)
  175.             min=info[i];
  176.     printf("Max:\n");
  177.     printf("num score\n");
  178.     for(i=1;i<=N;i++)
  179.         if(max.score==info[i].score)
  180.             printf("%d:%d\n",i,min.score);
  181.     printf("Min:\n");
  182.     printf("num score\n");
  183.     for(i=1;i<=N;i++)
  184.         if(min.score==info[i].score)
  185.             printf("%d:%d\n",i,min.score);

  186.         return 0;
  187.     
  188. }




  189. 从键盘输入5个数,输出最小公倍数

  190. #include<stdio.h>
  191. int find(int a,int b);
  192. int main()
  193. {
  194.     int a[5],i,x;
  195.     printf("Enter 5 numbers\n");
  196.     for(i=0;i<5;i++)
  197.         scanf("%d",&a[i]);
  198.     x=a[0];
  199.     for(i=1;i<5;i++)
  200.         x=find(a[i],x);
  201.     printf("%d\n",x);
  202.     return 0;
  203. }

  204. int find(int a,int b)
  205. {
  206.     int d;
  207.     if(a>b){
  208.         d=a;
  209.         a=b;
  210.         b=d;
  211.     }
  212.     for(d=b;;d+=b)
  213.         if(d%a==0)
  214.             return d;
  215. }


  216. 链表操作:输入一串数,再输入一个待删除的数字,删除这个待删除的数字
  217. #include<stdio.h>
  218. #include<stdlib.h>

  219. struct node{
  220.     int num;
  221.     struct node *next;
  222. };


  223. void del(struct node *head,int x);

  224. int main()
  225. {
  226.     struct node *head;
  227.     struct node *p,*q;
  228.     int x;
  229.     head=(struct node *)malloc(sizeof(struct node));
  230.     head->next=NULL;
  231.     q=head;
  232.     printf("Enter numbers and -1 is end\n");


  233.     while(1){
  234.         scanf("%d",&x);
  235.         if(x==-1)
  236.             break;
  237.         p=(struct node *)malloc(sizeof(struct node));
  238.         p->num=x;
  239.         p->next=NULL;
  240.         
  241.         q->next=p;
  242.         q=q->next;
  243.     }

  244.     printf("\nEnter the number you want to delete:");
  245.     scanf("%d",&x);
  246.     

  247.     del(head,x);
  248.     printf("After deleting:");

  249.     p=head->next;
  250.     while(p){
  251.         printf("%-4d",p->num);
  252.         p=p->next;
  253.     }

  254.     getch();
  255.     return 0;
  256. }

  257. void del(struct node *head,int x)
  258. {
  259.     struct node *p,*temp;
  260.     
  261.     p=head;
  262.     while(p->next){
  263.         if(p->next->num==x){
  264.             temp=p->next;
  265.             p->next=p->next->next;
  266.             free(temp);        
  267.         }
  268.         else
  269.             p=p->next;
  270.     }

  271.     return ;
  272. }


  273. 移位操作,对输入的一个十进制数,输出它的二进制表示
  274. #include<stdio.h>
  275. void main()
  276. {
  277.     int i;
  278.     unsigned int number,temp;
  279.     scanf("%u",&number);
  280.     printf("%x\n",number);
  281.     for(i=15;i>=0;i--){
  282.        temp=(number&0x8000);
  283.        temp>>=15;
  284.        printf("%1x",temp);
  285.        number<<=1;
  286.     }
  287. }


  288. 9、输出ASCII码表

  289. #include<stdio.h>
  290. void mian()
  291. {
  292.     int i=32;
  293.     printf("ASCII code Decimal hexadecimal");
  294.     printf(" ASCII code Decimal hexidecima\n");
  295.     while(i<=79){
  296.         printf("%6c %6d% 0x04x",i,i,i);
  297.         if((i+48)<127)
  298.              printf("%6c %6d% 0x04x",i,i,i);
  299.         i++;
  300.         printf("\n");
  301.     }
  302.     return 0;
  303. }


  304. 10、输入年份和这年的第一天是星期几,再输入月份,输出这个月份的日历
  305. #include<stdio.h>
  306. void main()
  307. {
  308.     int year,month,week_day,month_day,i,j,days,first_day;
  309.     printf("Enter the year and Jan,first of the year.\n");
  310.     scanf("%d %d",&year,&week_day);
  311.     printf("Enter the month you want to show.\n");
  312.     scanf("%d",&month);
  313.     days=0;
  314.     for(i=1;i<month;i++)
  315.         switch(i){
  316.             case 1:
  317.             case 3:
  318.             case 5:
  319.             case 7:
  320.             case 8:
  321.             case 10:
  322.             case 12:days+=31;break;
  323.             case 4:
  324.             case 6:
  325.             case 9:
  326.             case 11:days+=30;break;
  327.             case 2: if((year%4==0&&year%100!=0)||(year%400==0)) days+=29;else days+=28;break ;
  328.             }
  329.     switch(month){
  330.             case 1:
  331.             case 3:
  332.             case 5:
  333.             case 7:
  334.             case 8:
  335.             case 10:
  336.             case 12:month_day=31;break;
  337.             case 4:
  338.             case 6:
  339.             case 9:
  340.             case 11:month_day=30;break;
  341.             case 2: if((year%4==0&&year%100!=0)||(year%400==0))
  342.                          month_day=29;
  343.                     else month_day=28;
  344.                     break;
  345.             }

  346.     first_day=(days%7+week_day)%7;

  347.     printf("month Mon. Tue. Wen. Tur. Fri. Sta Sun.\n");
  348.     printf("%-5d",month);

  349.     if(first_day==0){
  350.         first_day=7;
  351.         printf("%42d\n",1);
  352.         printf(" ");
  353.     }
  354.     else
  355.         printf("%*d",6*first_day,1);
  356.     for(i=1;i<month_day;i++){
  357.         printf("%6d",i+1);
  358.         if(((first_day+i)%7)==0){
  359.             printf("\n");
  360.             printf(" ");
  361.         }
  362.     }

  363.     printf("\n");
  364.     return ;
  365. }


  366. 11、冒泡排序

  367. void bubble_sort()
  368. {
  369.     int bub[5]={3,4,1,2,7};
  370.     int i,j;
  371.     int temp;
  372.     int change;
  373.     int n=5;
  374.     for(i=1;i<n;i++){
  375.         change=0;
  376.         for(j=1;j<=n-i;j++)
  377.            if(bub[j]>bub[j-1]){
  378.                 temp=bub[j];
  379.                 bub[j]=bub[j-1];
  380.                 bub[j-1]=temp;
  381.                 change=1;
  382.            }
  383.          if(change==0)
  384.             break;
  385.      }
  386.     for(i=0;i<n;i++)
  387.         printf("%d\t",bub[i]);
  388.     return ;
  389. }


  390. 12、用数组筛选法找出200以内的素数,这个比较classic

  391. void prime()
  392. {
  393.     int prime[201]={0};
  394.     int i;
  395.     int k;
  396.     int d;
  397.     for(d=2;d<=sqrt(200);d++)
  398.        if(prime[d]==0)
  399.             for(i=2*d;i<=200;i=i+d)
  400.                 prime[i]=1;
  401.     k=0;
  402.     for(i=2;i<=200;i++)
  403.         if(prime[i]==0){
  404.             k++;
  405.             printf("%d\t",i);
  406.             if(k%5==0)
  407.                 printf("\n");
  408.         }
  409. }


  410. 13、删除s1中的s2中所含字符
  411. void main()
  412. {
  413.     char s1[30],s2[30];
  414.     char *p1,*p2;
  415.     int i,j,k=0;
  416.     p1=s1,p2=s2;

  417.     printf("Enter string s1 and s2...\n");
  418.     scanf("%s%s",s1,s2);
  419.     for(i=0;*(p1+i)!='\0';i++){
  420.         for(j=0;*(p2+j)!='\0'&&*(p1+i)!=*(p2+j);j++);
  421.             if(*(p2+j)=='\0'){
  422.                 *(p1+k)=*(p1+i);
  423.                 k++;
  424.         }
  425.     }
  426.     for(i=0;i<k;i++)
  427.         printf("%1c",*(p1+i));
  428.     return ;

  429. }


  430. 14、输入起始地址,输出内容
  431. void main()
  432. {
  433.     long int b_addr,e_addr,i;
  434.     int j;
  435.     char *point;
  436.     printf("Enter begain and end addr...\n");
  437.     scanf("%lx %lx",&b_addr,&e_addr);
  438.     for(i=b_addr;i<e_addr;i+=16){
  439.         printf("%lx: ",i);
  440.         point=(char *)i;
  441.         for(j=0;j<16;j++){
  442.             if(j==8)
  443.                 printf(" ");
  444.             printf(" %02x",*point);
  445.             point ++;

  446.         }
  447.         printf("\n");
  448.     }
  449. }





  450. 对输入的字符加上偶校验位

  451. #include<stdio.h>

  452. struct bit{
  453.     unsigned bit0:1,bit1:1,bit2:1,bit3:1,bit4:1,bit5:1,bit6:1,bit7:1;
  454. }*sp;


  455. void main()
  456. {
  457.      char ch[]="abcde";
  458.      char *dp;
  459.      int bit_sum;
  460.      int parity;
  461.      dp=ch;
  462.      while(*dp!='\0'){
  463.           sp=(struct bit*)dp;
  464.           bit_sum=sp->bit0+sp->bit1+sp->bit2+sp->bit3+sp->bit4+sp->bit5+sp->bit6;
  465.           parity=bit_sum%2;
  466.           printf("%c:%d%d%d%d%d%d%d\n",*dp,parity,sp->bit6,sp->bit5,sp->bit4,sp->bit3,sp->bit2,sp->bit1,sp->bit0);
  467.           dp++;
  468.      }
  469. }



  470. 质数分解:

  471. 例如:45=3*3*5

  472. #include<stdio.h>

  473. void main()
  474. {
  475.     int a[100];
  476.     int x,t,i;
  477.     printf("x=");
  478.     scanf("%d",&x);
  479.      printf("%d=",x);
  480.     if(x<1){
  481.         printf("x must greater than 0");
  482.         return ;
  483.     }
  484.     else if(x==1){
  485.         printf("x=1");
  486.         return ;
  487.     }
  488.     else{
  489.         t=2,i=-1;
  490.         while(x!=1){
  491.               if(x%t==0){
  492.                     x=x/t;
  493.                     a[++i]=t;
  494.                     t=2;
  495.                 }
  496.                 else
  497.                     t++;
  498.         }
  499.     }

  500.     for(t=0;t<=i;t++){
  501.         printf("%d",a[t]);
  502.         if(t<i)
  503.             printf("*");
  504.     }
  505.     
  506. }

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