i hate string-prosessing!

整数与IP地址间的转换

https://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea

#include <math.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
long int tenTotwo(int ten,int tag){
    int temp,tempten=ten;
    int i=0;
    long int sum=0;
    while (tempten!=0) {
        temp=tempten%2;
        sum=sum+temp*pow(2,((3-tag)*8+i));
        tempten=tempten/2;
        i++;
    }
    return sum;
}
void twoToten(long int a){
    int result[4];
    memset(result, 0, 4*sizeof(int));
    for(int i=0;i<4&&a!=0;i++){
        int count=0;
        while(count<8){
            result[3-i]+=(a%2)*pow(2,count);
            a=a/2;
            count++;
        }
    }
    for(int k=0;k<4;k++){
        printf("%d",result[k]);
        if(k<3){printf(".");}
    }
}
int main() {
    int index[4];
    long int totalsum=0;
    for(int i=0;i<4;i++){
        scanf("%d",&index[i]);
        totalsum+=tenTotwo(index[i],i);
        getchar();
    }
    printf("%ld\n",totalsum);
    long int ten=0;
    scanf("%ld",&ten);
    twoToten(ten);
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务