c++代码转java c++代码转c
如何将下面c语言代码转换成Java代码?
完整的直译为java方法是这样的,但是这样写dataLen的值必须小于等于数组长度,不然会出现下标越界的异常
创新互联网站建设公司是一家服务多年做网站建设策划设计制作的公司,为广大用户提供了成都网站制作、网站设计、外贸网站建设,成都网站设计,广告投放,成都做网站选创新互联,贴合企业需求,高性价比,满足客户不同层次的需求一站式服务欢迎致电。
public int accumulativeVerification(int[] data, int dataLen) {
int sum = 0;
for (int i = 0; i dataLen; i++) {
sum += data[i];
}
return sum;
}
如果是意译(计算int数组的所有数之和),那么java可以这样写
public int accumulativeVerification(int[] data) {
int sum = 0;
for (int i = 0; i data.length; i++) {
sum += data[i];
}
return sum;
}
这段C代码如何转为java代码?
public class Test {
private int HASH_LOG = 14;
private int HASH_SIZE=(1 HASH_LOG);
private int HASH_MASK=(HASH_SIZE - 1);
public Long flz_hash(Long a){
Long l1 = 2654435769L;
Long index = (a * l1) 32-HASH_LOG;
return index HASH_MASK;
}
public static void main(String[] args) {
Test test = new Test();
long a = 2;
Long index = test.flz_hash(a);
System.out.println(index);
}
}
c语言如何转化为java 语言
public class Test{
public staitc void main(String args){
int i,j,k=3;
for(i=0;i4;k=k-1-i,i++)
{
for(j=0;j4+i;j++)
{
if(jk)
System.out.print(" ");
else
{
System.out.print("*");
j++;
}
}
}
k=3;
}
}
保存为Test.java,然后运行javac Test.java和java Test就可以运行了。。
把下面C语言代码改成java代码
import java.util.Arrays;
import java.util.Scanner;
public class A {
static final int maxn=100000+5;
static long[] a=new long[maxn],t=new long[maxn];
static long ans=0;
static void mymerge(int x,int m,int y) {
int tx=x,tm=m+1;
int k=0;
while(tx=m tm=y) {
if(a[tx]=a[tm])
t[k++]=a[tx++];
else {
ans+=m-tx+1;
a[k++]=a[tm++];
}
}
while(tx=m) t[k++]=a[tx++];
while(tm=y) t[k++]=a[tm++];
for(int i=0;ik;i++)
a[x++]=t[i];
}
static void mergesort(int x,int y) {
if(x==y)return ;
int mid=(x+y)/2;
int xx=x,yy=y;
mergesort(xx,mid);
mergesort(mid+1,yy);
mymerge(xx,mid,yy);
//System.out.println(xx+" "+mid+" "+yy);
}
public static void main(String[] arg) {
int n,k;
Scanner input=new Scanner(System.in);
while(input.hasNext()) {
n=input.nextInt();
k=input.nextInt();
Arrays.fill(a, 0);
Arrays.fill(t, 0);
for(int i=0;in;i++) {
a[i]=input.nextLong();
}
ans=0;
mergesort(0, n-1);
if(kans)
System.out.println("0");
else
System.out.println((ans-k)+"");
}
}
}
把C语言代码转成Java代码
package dis;
import java.io.*;
import java.math.*;
public class dis
{
public static void main(String [] s) throws Exception
{
Float a,b,c;
double x2;
double x1;
System.out.println("请输入a, b, c的值");
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
a=Float.parseFloat(bReader.readLine());
b=Float.parseFloat(bReader.readLine());
c=Float.parseFloat(bReader.readLine());
if (b*b - 4*a*c = 0)
System.out.println("输入的系数不对,b2-4ac不大于0!程序退出!");
else
{
x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;
x2 = (-b - Math.sqrt(b*b - 4*a*c))/2*a;
System.out.println("x1 = "+ x1);
System.out.println("x2 = "+x2);
}
return;
}
}
把下面的C语言代码改成Java 代码
unsigned short crc(unsigned char [] crc_pointer,unsigned short data_length)
{
unsigned short k,k0,bit_flag;
unsigned short int_crc=0xffff;
for (k=0;kdata_length;k++)
{
int_crc^=(crc_pointer)[k];
for(k0=0;k08;k0++)
{
bit_flag=int_crc0x0001;
int_crc=1;
if (bit_flag==1)
int_crc^=0xa001;
}
}
return(int_crc);
}
网站栏目:c++代码转java c++代码转c
网站地址:http://scjbc.cn/article/hpooie.html