map用法笔记

c++:

定义:
map<int,int> mp;

插入:
mp[a]=b;

查找:
 if(mp.find(a)!=mp.end()){
	存在
}else{
	不存在
}

map<int,int>::iterator it;
   it = mp.find(key);//返回键的位置
   cout << it->first << it->second <<endl;

清空:
mp.clear();

大小:
mp.size();

遍历:
正向遍历:
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
	cout << it->first << it->second <<endl;
}
反向遍历:
    map<int,int>::reverse_iterator it;
    for(it=mp.rbegin();it!=mp.rend();it++){
	cout << it->first << it->second<<endl;

删除:
mp.erase(2);//删除键
   mp.erase(mp.begin(),mp.end());清空

判断是否为空:
mp.empty()//空则返回true

java:

定义:
Map<Integer,Integer> mp = new HashMap<Integer,Integer>();
//java的容器只能存对象所以用Integer,并且需要实例化

取值:
mp.get(key);

放入键值对:
mp.put(key,value);

判断key是否存在:

if(mp.get(a)!=null){
存在
}else{
不存在
}

遍历:
System.out.println("通过Map.keySet遍历key和value:");

     for (String key : map.keySet()) {  //通过foreach方法来遍历

      System.out.println("key= "+ key + " and value= " + map.get(key));
     }


例题:
四平方和

四平方和定理,又称为拉格朗日定理:
每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。

比如:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2^符号表示乘方的意思)

对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法


程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开

例如,输入:
5
则程序应该输出:
0 0 1 2

再例如,输入:
12
则程序应该输出:
0 2 2 2

再例如,输入:
773535
则程序应该输出:
1 1 267 838

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 3000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。

```c
//c++
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int n,a,c,d,b;
   scanf("%d",&n);
   map<int,int> mp;
   for( c=0;c*c<=n;c++){
       for( d=c;d*d<=n;d++){
               if(mp.find(c*c+d*d)==mp.end())
           mp[c*c+d*d]=c;
       }
   }


   //cout << mp[773535]<<endl;
   for( a=0;a*a<=n;a++){
       for( b=a;b*b<=n;b++){
           int t = n-a*a-b*b;
           if(mp.find(t)!=mp.end()){
                   //cout << t <<endl;
                   int c = mp[t];
                   int d = (int)sqrt(t-c*c);
               cout << a <<" " <<b<<" "<<c<<" "<<d<<endl;
               return 0;
           }
       }
   }
}

//java
//int a = (int)Math.sqrt(b);需要强制转换为int才能赋值给a
import java.util.*;

public class Main{
	public static void main(String[] args){
		int n,a,c,d,b;
	   // scanf("%d",&n);
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
	   // map<int,int> mp;
	    Map<Integer,Integer> mp = new HashMap<Integer,Integer>();
	    for( c=0;c*c<=n;c++){
	        for( d=c;d*d<=n;d++){
	                if(mp.get(c*c+d*d)==null)
	            mp.put(c*c+d*d,c);
	        }
	    }


	    //cout << mp[773535]<<endl;
	    for( a=0;a*a<=n;a++){
	        for( b=a;b*b<=n;b++){
	            int t = n-a*a-b*b;
	            if(mp.get(t)!=null){
	            	  
	                     c = mp.get(t);
	                  //   System.out.printf("%d\n%d\n%d\n%d\n",t,c,t-c*c,d);
	                     d =(int)Math.sqrt(t-c*c);
	                   //  System.out.printf("%d\n%d\n%d\n%d\n",t,c,t-c*c,d);
	               System.out.printf("%d %d %d %d\n",a,b,c,d);
	                return ;
	            }
	        }
	    }
	}

	private static int sqrt(int i) {
		// TODO Auto-generated method stub
		return 0;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值