邮票

某人有8 角的邮票5 张,1 元的邮票4 张,1 元8 角的邮票6 张,用这些邮票中的一张或若干张可以得到多少中不同的邮资?

枚举了,反正容量也不大……再排序,之前因为数组容量问题,数组一共是756=210,导致两次测试没通过,剩下就是排序,去重(最简单的冒泡排序居然不会写了,果然过完年就咸鱼了)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Main2{
public static void main(String args[]){
int a;
int m[]=new int[210];
int c=0;
int num=0;
for(int i=0;i<=5;i++){
for(int j=0;j<=4;j++){
for(int k=0;k<=6;k++){
m[c++]=8*i+10*j+18*k;
// System.out.println(m[c-1]);
num++;
}
}
}
//System.out.println(num);
int n=209;
for(int i=n;i>0;i--) {
for(int j=1;j<i;j++) {
if(m[j]<m[i]) {
int temp=m[j];
m[j]=m[i];
m[i]=temp;
}else if(m[i]==m[j]) {
m[j]=m[(n--)];
}
}
}
System.out.println(n);
}
}

在别人的博客偷来一份C++版的,参考一下吧,他还用到了容器
邮票,c++版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;
void sort(float a[],int n)
{
float temp;
for(int i=0;i<n;++i)
for(int j=i;j<n;++j)
{
if(a[i]>=a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
int main()
{
int sum=0;
float s[1000];
float s1[1000];
for(int i=0;i<=5;++i)
{
for(int j=0;j<=4;++j)
{
for(int k=0;k<=6;++k)
{
s[sum++]=0.8*i+j+1.8*k;
}
}
}
for(int p=0;p<sum;++p)
cout<<s[p]<<" ";
cout<<endl<<"总数:"<<sum<<endl;
sort(s,sum);
for(int b=0;b<sum;++b)
cout<<s[b]<<" ";//
cout<<endl<<"排好序:"<<sum<<endl;
cout<<endl;

//去除0
int u=0;
for(int d=0;d<sum;++d)
{
if(s[d]!=s[d+1]&&s[d]!=0)
s1[u++]=s[d];
}
for(int m=0;m<u;++m)
cout<<m<<" "<<s1[m]<<endl;
cout<<endl<<"去除0:"<<u<<endl;
return 0;
}

容器版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<float> fset;//double不精确
int i,j,k,count;
for(i=0;i<=5;++i)
for(j=0;j<=4;++j)
for(k=0;k<=6;++k)
fset.insert(0.8*i+1*j+1.8*k);
cout<<"总数:"<<fset.size()<<endl;
set<float>::iterator it;
cout<<"去除重复和0:"<<endl;
for(count=1,it=fset.begin();it!=fset.end();++it,++count)
cout<<count<<" "<<*it<<endl;
return 0;
}

居然发现我的代码还比这个短一些,不过这是人家两年前的博客,两年前的我……