A. Is your horseshoe on the other hoof?

来源:互联网 发布:淘宝商城首页登录 编辑:程序博客网 时间:2024/06/04 19:03

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera the Horse is going to the party with friends. He has been following the fashion trends for a while, and he knows that it is very popular to wear all horseshoes of different color. Valera has got four horseshoes left from the last year, but maybe some of them have the same color. In this case he needs to go to the store and buy some few more horseshoes, not to lose face in front of his stylish comrades.

Fortunately, the store sells horseshoes of all colors under the sun and Valera has enough money to buy any four of them. However, in order to save the money, he would like to spend as little money as possible, so you need to help Valera and determine what is the minimum number of horseshoes he needs to buy to wear four horseshoes of different colors to a party.

Input

The first line contains four space-separated integers s1, s2, s3, s4 (1 ≤ s1, s2, s3, s4 ≤ 109) — the colors of horseshoes Valera has.

Consider all possible colors indexed with integers.

Output

Print a single integer — the minimum number of horseshoes Valera needs to buy.

Sample test(s)
input
1 7 3 3
output
1
input
7 7 7 7
output
3

解题说明:此题意思很明确,按照我的理解就是,给你四个数,其中可能存在重复的数字,问最少改变几个数字,能让每个数字都不一样。

本来,我打算用数组下标来模拟,比如输入为 1  7  3  3,开一个较大的数组,元素初始化为0,我让a[1]=1, a[7]=1,a[3]=2,以后只需要判断值不为1的位置,同时也知道了相同的数字有多少个。但是针对这个题目存在问题,那就是数字的范围很大,开一个10^9的数组不显示,如果数字范围较小,我这种解法倒是适用于输入不止四个而是多个数的情况。

后来经过修改,发现只有先排序然后循环判断数字是否相等即可,这样也适用于多个数的情况,这种解法较好。至于其他两两数判断,当输入数据量太大时,容易搞晕,显然不适合。


#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int main(){int i;int j;long int temp;int sum;long int s[5];sum=0;for(i=0;i<4;i++){scanf("%ld",&s[i]);}for(i=0;i<4;i++){for(j=i+1;j<4;j++){if(s[j]<s[i]){temp=s[j];s[j]=s[i];s[i]=temp;}}}for(i=1;i<4;i++){if(s[i]==s[i-1]){sum++;}}printf("%d\n",sum);return 0;}