算法竞赛经典入门第二章

2-4 子序列的和

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n,m;
	int Case=0;  //case报错 [Error] expected identifier or '(' before 'case' 
	while(scanf("%d %d",&n,&m)!=EOF&&(n&&m)){
		int i=n;
		float s=0.0;
		for(;i<=m;i++)
		    s+=1/((float)i*(float)i);
		printf("Case %d:%.5f\n",++Case,s);
	} 
	 
	return 0;
}

2-5 分数化小数(未解决)(%取余只针对整数)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int a,b,c;
	int Case=0;
	while(scanf("%d %d %d",&a,&b,&c)!=EOF&&(a&&b&&c)){
		float i;
		int j;
		i=(float)a/(float)b;
		if(i%(pow(0.1,c))>=0.5)
			j=1;
		else
		    j=0;
		i=(int)(i/pow(0.1,c))+j;
		i=(float)i*pow(0.1,c);
		printf("Case %d:%f\n",++Case,i);
	} 
	 
	return 0;
}

2-6 排列(未解决)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    int abc,def,ghi;
    int i[10],j;
    memset(i,0,sizeof(i));
    for(abc=123;abc<=329;abc++){
    	int tag=1;
    	def=2*abc;
    	ghi=3*abc;
    	i[0]=1;
    	i[abc/100]++;i[abc%100/10]++;i[abc%10]++;
    	i[def/100]++;i[def%100/10]++;i[def%10]++;
    	i[ghi/100]++;i[ghi%100/10]++;i[ghi%10]++;
    	for(j=0;j<=9;j++)
    	{
    		if(i[j]!=1){
    			tag=0;break;
    		}
    	}
    	if(tag=1)
    	    printf("%d %d %d\n",abc,def,ghi);
    }
	 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值