PAT:1048. Find Coins (25)(哈希表法) AC

#include<stdio.h>
#include<string.h>

int HARSH[1066];

int main()
{
  memset(HARSH,0,sizeof(HARSH));
  int n,m;
  scanf("%d%d",&n,&m);
  for(int i=0 ; i<n ; ++i)
  {
    int tmp;
    scanf("%d",&tmp);
    ++HARSH[tmp];
  }
  for(int i=0 ; i<1066 ; ++i)
  {
    if(HARSH[i]==0)          //这个数字没有
      continue;
    if(HARSH[m-i]>0)
    {
      if(i==m-i && HARSH[i]<2)  //这两个数字相同的时候,但是只有一个可用,也是不行的
        continue;
      printf("%d %d",i,m-i);    //已经排除了输出不了的可能,条件都满足就输出
      return 0;
    }
  }
  printf("No Solution");
  return 0;
}
时间: 2024-12-25 09:35:13

PAT:1048. Find Coins (25)(哈希表法) AC的相关文章

PAT:1048. Find Coins (25)(双指针法) AC

#include<stdio.h> #include<algorithm> using namespace std; int arr[100066]; int main() { int n,m; scanf("%d%d",&n,&m); for(int i=0 ; i<n ; ++i) scanf("%d",&arr[i]); sort(arr,arr+n); int l=0,r=n-1; //左右指针 whil

PAT:1048. Find Coins (25)(二分查找) AC

#include<stdio.h> #include<algorithm> using namespace std; int arr[100066]; int FIND(int l,int r,int aim) //二分查找,从l到r,查找aim { int mid; while(l<=r) { mid=(l+r)/2; if(arr[mid]==aim) return mid; //找到:返回坐标 else if(arr[mid]<aim) l=mid+1; else

PAT Advanced 1078 Hashing (25) [Hash ?次?探查法]

题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the maximum size of

【PAT甲级】1048 Find Coins (25 分)(二分)

题意: 输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出"No Solution". 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[100007];int main(){ ios::sync_with_stdio(false

1048. Find Coins (25)

题目例如以下: Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the pa

PAT 1048. Find Coins

two sum题目,算是贪婪吧 #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; int main() { int N, M; scanf("%d%d", &N, &M); vector<int> coins(N); for (int i=0; i<N; i++)

PAT:循环-12. 打印九九口诀表(15) AC

#include<stdio.h> int main() { int x; scanf("%d",&x); for(int i=1 ; i<=x ; ++i) { for(int j=1 ; j<=i ; ++j) { printf("%d*%d=%-4d",j,i,i*j); } printf("\n"); } return 0; }

pat1048. Find Coins (25)

1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept a

哈希表算法实现(转)

转自:http://blog.csdn.net/jdh99 源码: 1 /********************************************************************* 2 * 哈希表算法实现 3 * (c)copyright 2013,jdh 4 * All Right Reserved 5 *文件名:main.c 6 *程序员:jdh 7 *******************************************************