杭电OJ第11页2095~2099算法题(C语言)

2095.find your present (2)

Problem Description
In the new year party, everybody will get a "special present".Now it's your turn to get your special present, a lot of presents now putting on the desk, and only one of them will be yours.Each present has a card number on it, and your present's card number will be the one that different from all the others, and you can assume that only one number appear odd times.For example, there are 5 present, and their card numbers are 1, 2, 3, 2, 1.so your present will be the one with the card number of 3, because 3 is the number that different from all the others.
 
Input
The input file will consist of several cases.
Each case will be presented by an integer n (1<=n<1000000, and n is odd) at first. Following that, n positive integers will be given in a line, all integers will smaller than 2^31. These numbers indicate the card numbers of the presents.n = 0 ends the input.
 
Output
For each case, output an integer in a line, which is the card number of your present.
 
Sample Input
5
1 1 3 2 2
3
1 2 1
0
 
Sample Output
3
2

Hint
use scanf to avoid Time Limit Exceeded

分析:本题实质是找出只出现一次的数。又由于异或具有以下性质:
(1)任何数和0异或为其本身
(2)相同的数异或,其结果为0
(3)异或满足交换律
所以根据以上的性质,可以得出,若在一组数中,除了某个数只出现一次,其余的数均出现了不为0的偶数次。那么这些数异或之后的结果就是那个只出现了1次的数。根据这个规律编写代码即可。

#include <stdio.h>

void Present(){
	int n,a,b;
	while(scanf("%d",&n)!=EOF && n){
		//有n个数 
		a=0;
		while(n--){
			scanf("%d",&b);
			//任何数和0异或为其本身,相同的数异或为0,且异或满足交换律 
			a^=b;
		}
		printf("%d\n",a);
	}
}

2096.小明A+B

Problem Description
小明今年3岁了, 现在他已经能够认识100以内的非负整数, 并且能够进行100以内的非负整数的加法计算.
对于大于等于100的整数, 小明仅保留该数的最后两位进行计算, 如果计算结果大于等于100, 那么小明也仅保留计算结果的最后两位.

例如, 对于小明来说:
1) 123434是相等的
2) 35+80=15

给定非负整数A和B, 你的任务是代表小明计算出A+B的值.
 
Input
输入数据的第一行为一个正整数T, 表示测试数据的组数. 然后是T组测试数据. 每组测试数据包含两个非负整数A和B(A和B均在int型可表示的范围内).
 
Output
对于每组测试数据, 输出小明A+B的结果.

Sample Input
2
35 80
15 1152

Sample Output
15
67

分析:按照题目要求计算即可。

#include <stdio.h>

void AAddB(){
	int T,A,B;
	scanf("%d",&T);
	//有T组数据
	while(T--){
		scanf("%d%d",&A,&B);
		if(A<0 || B<0){
			printf("输入的数应为非负整数!\n");
			continue;
		}
		A%=100;
		B%=100;
		printf("%d\n",(A+B)%100);
	} 
}

2097.Sky数

Problem Description
Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22,啊哈,真是巧啊。Sky非常喜欢这种四位数,由于他的发现,所以这里我们命名其为Sky数。但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是Sky数吧。
 
Input
输入含有一些四位正整数,如果为0,则输入结束。
 
Output
若n为Sky数,则输出“#n is a Sky Number.”,否则输出“#n is not a Sky Number.”。每个结果占一行。注意:#n表示所读入的n值。
 
Sample Input
2992
1234
0
 
Sample Output
2992 is a Sky Number.
1234 is not a Sky Number.

分析:单独编写一个求进制转换后数字之和的函数ScaleConvertSum(),然后再多次调用此函数进行判断即可。

#include <stdio.h>

int ScaleConvertSum(int n,int r){
	//n表示要被转换的数,r表示要转换成的进制数 
	int i,temp,sum=0;
	if(r<2 || r>16) {
		printf("r的取值范围为[2,16]之间的整数!\n");
	}
	i=0;  
	while(n>0){
		temp=n%r;
		sum+=temp;
		i++;
		n/=r;
	}
	return sum;
}

void Sky(){
	int n;
	while(scanf("%d",&n)!=EOF && n){
		if(n<0){
			printf("请输入非负整数!\n");
			continue;
		}
		if(ScaleConvert(n,10)==ScaleConvert(n,12) && ScaleConvert(n,10)==ScaleConvert(n,16)){
			printf("%d is a Sky Number.\n",n);
		}else{
			printf("%d is not a Sky Number.\n",n);
		}
	}
}

2098.分拆素数和

Problem Description
把一个偶数拆成两个不同素数的和,有几种拆法呢?

Input
输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。

Output
对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。

Sample Input
30
26
0

Sample Output
3
2

分析:直接判断即可。

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

//判断一个数是否为素数,若为素数返回1,否则返回0 
int IsPrime(int n){
    int i;
    for(i=2;i<=sqrt(n);i++){
    	if(n%i==0){
    		return 0;
    	}
    }  
    return 1;
}

void PrimeSum(){
	int n,count,i;
	while(scanf("%d",&n)!=EOF && n){
		count=0;
		for(i=2;i<n/2;i++){
			if(IsPrime(i) && IsPrime(n-i)){
				count++;
			}
		}
		printf("%d\n",count);
	}	 
}

2099.整除的尾数

Problem Description
一个整数,只知道前几位,不知道末二位,被另一个整数除尽了,那么该数的末二位该是什么呢?
 
Input
输入数据有若干组,每组数据包含二个整数a,b(0<a<10000, 10<b<100),若遇到0 0则处理结束。

Output
对应每组数据,将满足条件的所有尾数在一行内输出,格式见样本输出。同组数据的输出,其每个尾数之间空一格,行末没有空格。

Sample Input
200 40
1992 95
0 0

Sample Output
00 40 80
15

分析:使用穷举法即可。

#include <stdio.h>

void Mantissa(){
	int a,b,i,j,count;
	int res[200];
	while(scanf("%d%d",&a,&b)!=EOF){
		if(a==0 && b==0){
			break;
		}
		a=a*100;
		count=0;
		for(i=0;i<=9;i++){
			for(j=0;j<=9;j++){
				if((a+i*10+j)%b==0){
					res[count]=i;
					res[count+1]=j;
					count+=2;
				}
			}
		}
		for(i=0;i<count;i+=2){
			printf("%d%d",res[i],res[i+1]);
			//当前的结果不是最后一个 
			if((i+2)!=count){
				printf(" ");
			}
		}
		printf("\n");
	}
}

杭电OJ第11页2000~2004算法题(C语言)

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值