c 指针 及其位运算循环移动拔河比赛问题代码

week_2_day1_7.7 周一
//用字符数组 来实现 字母大小写转换
#include<stdio.h>
void desc( char *a ,int n)
{
    char  *i = a;
    int x = 0 ;
    for ( x = 0; x < n ;x ++ )
    {
                    if ( *i  >= 97 )
                        *i-=32;
        i++;
    }
}
int main(void)
{
    void desc(char  *num, int n);//必须在函数内部声明
   char num[100], a;
    int i = 0, k = 0;
    while((a = getchar ())!= '\n'){
        num[i] = a;
        i++;
    }
    desc(num,i);
    for (k = 0; k < i;  k ++) {
        printf("%c",num[k]);
    }
    return 0;
}
//大数字加法
#include <stdio.h>
int main(int argc, const char * argv[])
{

    int a[1001] = {},b[1001] = {} , c[1001] = {} ;
     int i , j= 0  , k1 = 0 ,k2 = 0 ,count1 = 0 , count2 = 0;
    char ch  , ch1 = '0';
    for (i = 1; (ch = getchar()) != ' ' ; i ++) {
        j = ch - ch1;
        a[i] = j;
    }//第一个大数字
     k1 =i - 1;
    for (i = 1; (ch = getchar()) != '\n' ; i ++) {
        j = ch - ch1;
        b[i] = j;
    }//第二个大数字
    k2 = i - 1 ;
    if (k1 > k2) {
        count1 = k1;
        count2 = k2;
    }
    else{ count1  = k2;
        count2 = k1;
    }
    for ( i = count1 ; i > 0 ; i --,k1 --, k2 -- ) {//加法
        if (k2 <= 0)
            c[i] += a[k1];//判断是否 那个比较小的数字 是否已经加完
        else if ( k1 <= 0)
            c[i] += a[k2];
        else{
            c[i] += (a[k1] + b[k2]);
            if (c[i] > 9)
            {
                c[i] %= 10;
                c[i-1] = 1;//是否进位
            }
        }
    }
    if (c[0] == 1)
        printf("%d",c[0]);
    for ( i = 1 ; i <= count1 ; i ++ )
        printf("%d",c[i]);
    printf("\n");
    return 0;
}
//n!的位数  暂时不是太懂

#include <stdio.h>
#include <math.h>
int factorial(int n)
    {
        long a[10000];
        int i,j,c,m=0,w,l = 0;
        
        a[0]=1;
        for(i=1;i<=n;i++)
        {
            c=0;
            for(j=0;j<=m;j++)
            {  
                a[j]=a[j]*i+c;
                c=a[j]/10000;
                a[j]=a[j]%10000;
            }
            if(c>0) {m++;a[m]=c;}
        }
        w=m*4+log10(a[m])+1;
       // printf("\n%ld",a[m]);
       // for(i=m-1;i>=0;i--)
       //     printf("%4.4ld\n",a[i]);
        return w;
    }

int main(int argc, const char * argv[])
{
    
    int x;
    scanf("%d",&x);
    x=factorial(x);
    printf("%d",x);
    return 0;
}
//拔河比赛分配方案
//就是用随机函数确定那个人 在那一队,然后比较那一队和总体重相差小,就输出他。
#include<stdio.h>
#include <stdlib.h>
#include <sys/_types/_mode_t.h>
int swap(int *a, int j);
int  swap(int *a , int j)
{
    int min  = 0, b ,s = 0 , sum = 0;
    int c[100] = {};
    for ( int i = 0; i < j ; i ++ ) {
        sum += a[i];
    }//总人数的 体重
    printf("总体重:%d\n",sum);
    for ( int h = 0; h < j;  h ++)
    {
        c[h] = -1;
    }
    for ( int i = 0;  i < 10000;  i ++)
    {
            for ( int n = 0;  n < j/2;  n ++)
            {
              AA:
                b = abs(arc4random()%(j));
                for ( int h = 0; h < n;  h ++)
                {
                    if (c[h] == b)
                    {
                        goto AA;
                    }
                }
                c[n] = b;
                s += a[b];
            }
        
            for ( int h = 0; h < j;  h ++)
            {
                c[h] = -1;
            }//重置 保存的 已经有用的数据
        
            if (abs(sum/2 - min) > abs(sum/2 - s) ) {
                min = s ;
            }
            s = 0;
    }
    return  min;
}
int main(void)
{
    int  a ,ch[100],sum = 0 ;
    printf("请输入拔河比赛的总人数:");
    scanf("%d",&a);
    for ( int i =0;  i < a;  i ++)
    {
        printf("请输入第%d个人:",i+1);
        scanf("%d",&ch[i]);
        
    }
    int min = swap(ch, a);
    for ( int i = 0; i < a ; i ++ ) {
        sum += ch[i];
    }
    printf("比较轻的队伍:%d\n",min);
    printf("比较重的队伍:%d\n",sum-min);
     //printf("%d\n",a, j);
    return  0 ;
}
//将n向右循环a位  然后输出
#include <stdio.h>
int ch[32] = {} , ch_l[32] = {};
void printfArry( int *a , int b);//输出数组
int * del_num( long a);//分解数字为2进制 存进数组
int * Toright( int * p, int m);//向右移动
int To_arry_num( int k);//每个数组应该加的2的k次幂
int  To_sum( int *p ,int h);//最后输出的 转换后的 数字
void printfArry( int *a , int b){
    int i = 0 ;
    for ( i = 0 ;  i < b;  i ++) {
        printf("%d\t",a[i]);
    }
}
int * del_num( long a){
    int i = 0;
    for ( i = 0;  a > 0; i ++ )
    {
        if ( a %2 == 0) {
            ch[ 31 - i] = 0;
        }
        else{
            ch[ 31 - i] = 1;
        }
        a /= 2 ;
    }
    return ch ;
}
int * Toright( int * p, int m)
{
    int i = 0 , k = 31 , j ;
    for ( i =  31- m;  i >=0;   k-- ,i --)
    {
        ch_l[k] = p[i];
    }
    for ( j = 31 ; j >31 - m ; k -- ,j --) {
        ch_l[k] = p[j];
    }
    
    return  ch_l;
};

int To_arry_num(int k){
    int i = 0 , s=1;
    for ( ;  i < k ;  i ++) {
        s *= 2 ;
    }
    return  s;
}
int  To_sum( int *p , int h){
    int sum = 0 , i = 0;
    for ( i = 0; i < h;  i ++ )
    {
        if ( p[i] == 1)
        {
            sum += To_arry_num( 31-i);
        }
    }
    return sum;
}
int main(int argc, const char * argv[])
{

    long a ;
    int  *p , m = 0;
    scanf("%ld",&a);
    scanf("%d",&m);
  //  printf("%ld\n",a);
    if ( a  == -1) {
        printf("-1");
    }
    else
    {
    p =del_num(a);//分解数字为2进制
    p=Toright(p, m);
    
    printfArry(p,32);
    printf("\n");
    a = To_sum(p, 32);
    printf("%ld\n",a);
    }
        return 0;
}
//循环左边移动 n位
#include <stdio.h>
int ch[32] = {} , ch_l[32] = {};
void printfArry( int *a , int b);
int * del_num( long a);
int * Toleft( int * p, int m);
int To_arry_num( int k);
int To_sum( int *p ,int h);
void printfArry( int *a , int b){
int i = 0 ;
for ( i = 0 ; i < b; i ++) {
printf("%d",a[i]);
}
}
int * del_num( long a){
int i = 0;
for ( i = 0; a > 0; i ++ )
{
if ( a %2 == 0) {
ch[ 31 - i] = 0;
}
else{
ch[ 31 - i] = 1;
}
a /= 2 ;
}
return ch ;
}
int * Toleft( int * p, int m)
{
int i = 0 , k = 31 - m ;
for ( i = 31 ; k == 0 ; k-- ,i --)
{
ch_l[k] = p[i];
}
for ( k = 31 -m ; i >= 0; k -- ,i --) {
ch_l[k] = p[i];
}

return ch_l;
};

int To_arry_num(int k){
int i = 0 , s=1;
for ( ; i < k ; i ++) {
s *= 2 ;
}
return s;
}
int To_sum( int *p , int h){
int sum = 0 , i = 0;
for ( i = 0; i < h; i ++ )
{
if ( p[i] == 1)
{
sum += To_arry_num( 31-i);
}
}
return sum;
}
int main(int argc, const char * argv[])
{

long a ;
int *p , m = 0;
scanf("%ld",&a);
scanf("%d",&m);
// printf("%ld\n",a);
if ( a == -1) {
printf("-1");
}
else
{
if ( m != 32)
{
p =del_num(a);//分解数字为2进制
p=Toleft(p, m);

// printfArry(p,32);
printf("\n");
a = To_sum(p, 32);
printf("%ld\n",a);
}else
{
printf("%d",a);
}
}
return 0;
}

//实现一个按照ID进行排序的链表,并按有序的方式初始化链表中的结点,然后实现向链表中插入一个新结点,在插入完成后,不会破坏链表的排序。

#include <stdio.h>
//#include<malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct stu)
struct stu {
        int id;
    char a[1];
    struct stu *next;
    };
    int n;
struct stu* c(void){//创建链表
        struct stu *head ;
        struct stu *p1,*p2;
        n = 0;

    p1 =p2 =(struct stu*)malloc(LEN);
    scanf("%d%c",&p1->id,&p1->a[0]);
    head =  NULL ;
     do{
                 n += 1;
           if ( n == 1) head = p1;
        else    p2->next = p1;
            p2 = p1;
         
         if(p2->a[0] == '\n' )
             break;
            p1 = (struct stu *)malloc(LEN);
            scanf("%d%c",&p1->id,&p1->a[0]);
            p2 -> next  = NULL;
    }while (1);
     
        return  (head);
}
int main(int argc, const char * argv[])
{
    struct stu *p ,*p3, *p4,*p5;
     p = c();
    p5 = p;
    p3=(struct stu *)malloc(LEN);
    scanf("%d",&p3->id);
    p4=p->next;
    while (1) {
        
    
    if (  (p4 ->id) < (p->id) ) {
        if ( p3->id >p4->id && p3->id < p->id)
        {
            p3->next = p4;
            p->next = p3;//插入一个指针
            break;
        }
        else
        {
             p = p4;
            p4 = p4->next;//向后边移动一个地址
        }
    }
    if ( p4 ->id > p->id)
    {
        if (p3->id > p->id && p3->id < p4->id)
        {
            p3->next = p4;
            p->next = p3;//向后边移动一个地址
            break;
        }
        else
        {
            p = p4;//向后边移动一个地址
            p4 = p4->next;
        }

      }
    }
    while (p5 != NULL) {
        printf("%d",p5->id);
        p5 = p5->next;
    }

      printf("\n");
    return 0;
}

posted @ 2014-07-12 20:52  ifgyong  阅读(691)  评论(0编辑  收藏  举报