2013华为编程大赛初赛出试题
2013-08-26 10:18
197 查看
题目描述 由实部和虚部组成,形如(a,bi)这样的数,称为复数。通信系统中,通常用32bit数来表示复数(高16bit表示实部,低16bit表示虚部),如整数524295(16进制为0x00080007)所代表的复数,实部为0x0008,虚部为0x0007。 有别于实数运算,复数加、减、乘、除运算定义如下: 复数加公式:(a,bi) + (c,di) = (a + c),(b + d)i 复数减公式:(a,bi) + (c,di) = (a - c),(b - d)i 复数乘公式:(a,bi) * (c,di)
= (ac - bd),(ad + bc)i 复数除公式:(a,bi) / N = (a/N),(b/N)i 题目要求,输入N个复数,计算这个N个复数的平均值,复数Avg = (复数1*复数2 + 复数3*复数4 + … + 复数N-1*复数N) / N。 复数加、复数减、复数乘、复数除的结果仍然为复数,实部和虚部均为16bit有符号数,计算过程中,当结果大于32767(0x7fff)时,输出32767;当计算结果小于-32768(0x8000)时,输出-32768。
输入
输入共计两行 有别于实数运算,复数加减乘除运算定义如下第一行包含1个整数,表示输入复数个数N(N为偶数,N不大于1000) 第一行包含1个整数,表示输入复数个数N(N为偶数,N不大于1000)
输出 经计算得到的复数的平均值。
样例输入 4 262149,393223,524297,655371
样例输出 -458693
package com.huawei.test;
import java.math.BigInteger;
import java.util.Stack;
public class SecondApp {
/**
* @param args
*/
public static void main(String[] args) {
String str="262149,393223,524297,655371";
int result=getresult(4,str);
System.out.println(result);
}
public static int getresult(int num,String str){
int result=0;
int realsum=0;
int imagsum=0;
Stack<String> hexnum=new Stack<String>();
String[] array=str.split(",");
for(int i=0;i<num;i++){
int temp=Integer.parseInt(array[i]);
String hex=Integer.toHexString(temp);
while(hex.length()<8){
hex="0"+hex;
}
hexnum.push(hex);
}
while(!hexnum.empty()){
String hex1=hexnum.pop();
String hex1real1=hex1.substring(0, 4);
String hex1imag1=hex1.substring(4);
BigInteger bi1=new BigInteger(hex1real1,16);
int real1=bi1.intValue();
BigInteger bi2=new BigInteger(hex1imag1,16);
int imag1=bi2.intValue();
String hex2=hexnum.pop();
String hex1real2=hex2.substring(0, 4);
String hex1imag2=hex2.substring(4);
BigInteger bi3=new BigInteger(hex1real2,16);
int real2=bi3.intValue();
BigInteger bi4=new BigInteger(hex1imag2,16);
int imag2=bi4.intValue();
int temp1=real2*real1;
int temp2=imag2*imag1;
int temp3=real2*imag1;
int temp4=imag2*real1;
if(temp1>32767){
temp1=32767;
}
if(temp1<-32768){
temp1=-32768;
}
if(temp2>32767){
temp2=32767;
}
if(temp2<-32768){
temp2=-32768;
}
if(temp3>32767){
temp3=32767;
}
if(temp3<-32768){
temp3=-32768;
}
if(temp4>32767){
temp4=32767;
}
if(temp4<-32768){
temp4=-32768;
}
realsum+=(temp1-temp2);
imagsum+=(temp3+temp4);
}
int realavg=realsum/num;
int imagavg=imagsum/num;
String realhexavg=Integer.toHexString(realavg);
while(realhexavg.length()<4){
realhexavg="0"+realhexavg;
}
String imaghexavg=Integer.toHexString(imagavg);
while(imaghexavg.length()<4){
imaghexavg="0"+imaghexavg;
}
String hexavg=realhexavg+imaghexavg;
BigInteger bi=new BigInteger(hexavg,16);
result=bi.intValue();
return result;
}
}
= (ac - bd),(ad + bc)i 复数除公式:(a,bi) / N = (a/N),(b/N)i 题目要求,输入N个复数,计算这个N个复数的平均值,复数Avg = (复数1*复数2 + 复数3*复数4 + … + 复数N-1*复数N) / N。 复数加、复数减、复数乘、复数除的结果仍然为复数,实部和虚部均为16bit有符号数,计算过程中,当结果大于32767(0x7fff)时,输出32767;当计算结果小于-32768(0x8000)时,输出-32768。
输入
输入共计两行 有别于实数运算,复数加减乘除运算定义如下第一行包含1个整数,表示输入复数个数N(N为偶数,N不大于1000) 第一行包含1个整数,表示输入复数个数N(N为偶数,N不大于1000)
输出 经计算得到的复数的平均值。
样例输入 4 262149,393223,524297,655371
样例输出 -458693
package com.huawei.test;
import java.math.BigInteger;
import java.util.Stack;
public class SecondApp {
/**
* @param args
*/
public static void main(String[] args) {
String str="262149,393223,524297,655371";
int result=getresult(4,str);
System.out.println(result);
}
public static int getresult(int num,String str){
int result=0;
int realsum=0;
int imagsum=0;
Stack<String> hexnum=new Stack<String>();
String[] array=str.split(",");
for(int i=0;i<num;i++){
int temp=Integer.parseInt(array[i]);
String hex=Integer.toHexString(temp);
while(hex.length()<8){
hex="0"+hex;
}
hexnum.push(hex);
}
while(!hexnum.empty()){
String hex1=hexnum.pop();
String hex1real1=hex1.substring(0, 4);
String hex1imag1=hex1.substring(4);
BigInteger bi1=new BigInteger(hex1real1,16);
int real1=bi1.intValue();
BigInteger bi2=new BigInteger(hex1imag1,16);
int imag1=bi2.intValue();
String hex2=hexnum.pop();
String hex1real2=hex2.substring(0, 4);
String hex1imag2=hex2.substring(4);
BigInteger bi3=new BigInteger(hex1real2,16);
int real2=bi3.intValue();
BigInteger bi4=new BigInteger(hex1imag2,16);
int imag2=bi4.intValue();
int temp1=real2*real1;
int temp2=imag2*imag1;
int temp3=real2*imag1;
int temp4=imag2*real1;
if(temp1>32767){
temp1=32767;
}
if(temp1<-32768){
temp1=-32768;
}
if(temp2>32767){
temp2=32767;
}
if(temp2<-32768){
temp2=-32768;
}
if(temp3>32767){
temp3=32767;
}
if(temp3<-32768){
temp3=-32768;
}
if(temp4>32767){
temp4=32767;
}
if(temp4<-32768){
temp4=-32768;
}
realsum+=(temp1-temp2);
imagsum+=(temp3+temp4);
}
int realavg=realsum/num;
int imagavg=imagsum/num;
String realhexavg=Integer.toHexString(realavg);
while(realhexavg.length()<4){
realhexavg="0"+realhexavg;
}
String imaghexavg=Integer.toHexString(imagavg);
while(imaghexavg.length()<4){
imaghexavg="0"+imaghexavg;
}
String hexavg=realhexavg+imaghexavg;
BigInteger bi=new BigInteger(hexavg,16);
result=bi.intValue();
return result;
}
}
相关文章推荐
- 2013 华为编程大赛java组初赛试题
- 2013蓝桥杯 【初赛试题】 翻硬币
- 2013 蓝桥杯 【初赛试题】 错误票据
- 2013蓝桥杯 【初赛试题】 带分数
- 2013蓝桥杯【初赛试题】前缀判断
- 2013蓝桥杯 【初赛试题】 马虎的算式
- 2013蓝桥杯 【初赛试题】 带分数
- 2013蓝桥杯 【初赛试题】 马虎的算式
- 2013蓝桥杯 【初赛试题】 翻硬币
- 2013蓝桥杯 【初赛试题】 第39级台阶
- 2013蓝桥杯 【初赛试题】 翻硬币
- 2013蓝桥杯 【初赛试题】 第39级台阶
- 2013蓝桥杯【初赛试题】高斯日记
- 2013蓝桥杯 前缀判断 【初赛试题】
- 2013蓝桥杯【初赛试题】第39阶台阶
- 2013 蓝桥杯 【初赛试题】 连号区间数
- 2013 蓝桥杯 【初赛试题】 错误票据
- 2013 蓝桥杯 【初赛试题】 连号区间数
- 2012蓝桥杯【初赛试题】 取球游戏
- 2012 蓝桥杯【初赛试题】放棋子