c语言实现:4和7幸运数字的题

#include <stdio.h>
#include <math.h>
#include <vector>
 
using namespace std;

int main()
{
    int i;
     
    int x,y,z;
    char *p;
    double w=2;
    int temp;
    char ch;

    vector<int> vec1;

    vector<unsigned int> vec2;
    
    vector<int> vec3;
    
    scanf("%d",&i);

    for(x=0;x<i;x++)
    {
    scanf(
"%d",&temp);   vec1.push_back(temp);
}
for(x=0;x<i;x++) { for(y=1;y<32;y++) { if((pow(w,y)-1)*2>=vec1[x] && (pow(w,y-1)-1)*2<=vec1[x]) break; } vec2.push_back(y); } for(x=0;x<i;x++) { vec3.push_back(vec1[x]-2*(pow(w,y-1)-1)-1); } for(x=0;x<i;x++) {
     //内存的管理问题 p
= (char*)malloc(sizeof(char)*(vec2[x]+1)); memset(p,0,vec2[x]+1); for(y=0;y<vec2[x];y++) { if(vec3[x]&0x1==1) p[y]=7+'0'; else p[y]=4+'0'; vec3[x] = vec3[x]>>1; } for(y=0,z=vec2[x]-1;y<vec2[x]/2;y++,z--) { ch = p[y]; p[y]= p[z]; p[z]=ch; } printf("%s\n",p); } }

 

posted @ 2016-09-05 22:08  小德cyj  阅读(3057)  评论(0编辑  收藏  举报