一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程找出1000以内的所有完数?
include <iostream>
using namespace std;
int main() {
for (int i = 2; i <= 1000; i++) {//从2到1000的数
int sum = 0;//因子总和
for (int j = 1; (j * j) <= i; j++) {//j做除数,√n复杂度,减少一半的计算量
if (i % j == 0) {//可以整除,为其因子
sum = j + (i / j) + sum;//因子之和
}
}
if ((sum - i) == i) cout << "1000以内的完数:" << i << endl;//因子之和减去其本身(1*i也为其因子)如果等于这个数本身,则为完数
}
}
java后台怎样传json格式的数据?
通过 JSONObject类就可以了首先 你把这几个包 下下来 放到你项目。如果有就不要下了:
1.commons-lang.jar2.commons-beanutils.jar3.commons-collections.jar4.commons-logging.jar 5.ezmorph.jar6.json-lib-2.
2.2-jdk15.jar像你这种是数据形式 就通过 JSONArray 如:JSONArray datasJson = JSONArray.fromObject(datas);最好把datas toString 一下
java在后台如何将前台传过来的json格式数据转换为map?
我们需要先把json字符串转化为
net.sf.json.JSONObject
对象,java中这样就可以完成json字符串到Map的转换了。1.将数组转换为JSON:String[] arr = {"asd","dfgd","asd","234"};JSONArray jsonarray = JSONArray.fromObject(arr);System.out.println(jsonarray);
2.对象转换成JSON:UserInfo user = new UserInfo(1001,"张三");JSONArray jsonArray = JSONArray.fromObject(user);System.out.println( jsonArray );
3.把Map转换成json, 要使用jsonObject对象:Map<String, Object> map = new HashMap<String, Object>();map.put("userId", 1001);map.put("userName", "张三");map.put("userSex", "男");JSONObject jsonObject = JSONObject.fromObject(map);System.out.println(jsonObject);

