解答在这里
http://bbs.csdn.net/topics/380154861?page=1#post-394077637
#include<stdio.h>
/*输入正整数a,b,c,输出a/b的小数形式,精确到小数点后c位。a,b<=10^6,c<=100.例如:a=1,b=6,c=4时应输出:0.1667*/
int main()
{
/*一个非常神奇的解法*/
int a,b,c;char format[10];
scanf("%d%d%d",&a,&b,&c);
sprintf(format,"%%.%df\n",c);
printf(format,(double)a/b);
}
利用的是格式化输出.
以后继续学习,这里有更简单的:http://blog.csdn.net/litiouslove/article/details/7891700
习题2-10 排列(permutition)
题目:用1,2,3……9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi = 1:2:3。输出所有解。提示:不必太动脑筋。
分析:上学期院里组织比赛的一道题,利用数组,a[1]~a[9]赋值为0,令a[出现的数字] = 1,若a[1] + a[2] + …… +a[9] == 9,则全部数字都出现。
源码:
#include<stdio.h>
#include<string.h>
int main()
{
int a[10]={0};//数组的下标代表1到9
int x,y,z,i;
for(x=123;x<987/3;x++)
{
y=x*2;z=x*3;
a[x%10]=a[x/10%10]=a[x/100]=1;
a[y%10]=a[y/10%10]=a[y/100]=1;
a[z%10]=a[z/10%10]=a[z/100]=1;
int m=0;
for(i=1;i<10;i++)m+=a[i];//注意要从1开始
if(m==9)
{
printf("%d :%d :%d\n",x,y,z);
}
for(i=1;i<10;i++)
{
a[i]=0;
}
}
}
蛇形数组
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,m;
int **a;
scanf("%d",&n);
a=(int **)malloc(sizeof(int *)*n);
for(i=0;i<n;i++)
{
a[i]=(int *)malloc(sizeof(int)*n);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
m=1;i=-1;j=n-1;
while(m<n*n)
{
while(i<n-1&&a[i+1][j]==0)a[++i][j]=m++;
while(j>0&&a[i][j-1]==0)a[i][--j]=m++;
while(i>0&&a[i-1][j]==0)a[--i][j]=m++;
while(j<n-2&&a[i][j+1]==0)a[i][++j]=m++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
}
2013年4月9日23:37:55
如何初始化new 出来的数组
int *a[]=new int [k]
memset(a,0,sizeof(int)*k);
打印组合数
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int mm;
void f(int n,int k,int *a)
{
if(k==0)
{
for(int i=0;i<mm;i++)
printf("%d ",a[i]);
printf("\n");
}
else
{
for(int i=1;i<n;i++)
{
a[k-1]=i;
f(n,k-1,a);
}
}
}
int main()
{
int n=10;
int k=3;
int *a=new int[k]{0};
mm=k;
memset(a,0,sizeof(int)*k);
freopen("d:\\a.txt","w",stdout);
f(n,k,a);
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#define mode (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
struct Node
{
char character;
int Counter;
struct Node* Next[26];
};
char* i2s(int i, char* s)
{
int j=0,t=i;
do
{
t = t/10;
j++;
}
while(t > 0);
while(j--)
{
s[j]=i%10+'0';
i=i/10;
}
return s;
}
void freeMemory(struct Node *head)
{
int i=0;
for(; i<26; i++)
{
if(head->Next[i]!=NULL)
freeMemory(head->Next[i]);
}
if(i==25)free(head);
}
void putIn(struct Node * head,char *word)
{
int wordlength=strlen(word);
int position=0;
struct Node *p=head;
int i=0;
for(i=0; i<wordlength; i++)
{
if('A'<=word[i]&&word[i]<='Z')
{
position=word[i]-65; //'A''s asscii is 65
}
else
{
position=word[i]-97; //'a''s asscii is 97
}
if(p->Next[position]==NULL) //new charactor in this route of tree
{
p->Next[position]=(struct Node *)malloc(sizeof(struct Node));
p=p->Next[position];
p->character=word[i];
int j=0;
for(j=0; j<26; j++)
{
p->Next[j]=NULL;
}
}
else // have one already
{
p=p->Next[position];
}
if(i==wordlength-1) //
{
p->Counter++;
}
}
}
void printfall(struct Node * head,char *container,int hight,int *fileds)
{
int i=0;
char dec[10]= {0};
for(; i<26; i++) //printf current node all 26 array
{
if(head->Next[i]!=NULL)
{
container[hight]=head->Next[i]->character;
container[hight+1]='\0';
//printf("%c",head->Next[i]->character);
if(head->Next[i]->Counter>0)
{
printf("%s",container);
write(*fileds,container,strlen(container));
write(*fileds,"\t\t",2);
sprintf(dec,"%d",head->Next[i]->Counter);
dec[strlen(dec)]='\n';
write(*fileds,dec,strlen(dec));
printf("\t%d\n",head->Next[i]->Counter);
}
printfall(head->Next[i],container,hight+1,fileds);
}
}
}
int beginCount(char *fileName,char * destination)
{
char c=0;
int n=0; //the length of words
FILE * filefd=NULL;
char s[30]= {0};
/*initial all head node for used in puIn()*/
struct Node * head=(struct Node *)malloc(sizeof(struct Node));
printf("dfffff..................................................................\n");
head->character='\0';
head->Counter=0;
int i;
for(i=0; i<26; i++)
{
head->Next[i]=NULL;
}
if(access(fileName,F_OK|R_OK)!=0)
{
perror(fileName);
exit(-1);
}
if((filefd=fopen(fileName,"r"))==NULL)
{
perror("open file erro: ");
}
while((c=getc(filefd))!=EOF)
{
if((('A'<=c)&&(c<='Z'))||(('a'<=c)&&(c<'z')))
{
s[n++]=c;
}
else
{
s[n]='\0';
if(n>0)
{
printf("put: %s\n",s);
putIn(head,s);
}
n=0;
memset(s,0,sizeof(s));
}
}
char container[30]= {'\0'}; //word want to be display
int hight=0;
int fileds=open(destination,O_WRONLY|O_CREAT|O_TRUNC,mode);
printfall(head,container,hight,&fileds);
close(fileds);
return 0;
}
int main(int argc,char * argv[])
{
if(argc!=2)
{
printf("usage: command argsn");
}
beginCount("/home/lyle/Documents/Advance/testFile","/home/lyle/Documents/Advance/result");
return 0;
}