How do i write simpla Java sequence in my editor (using notepad++)?
I am familiar with the basics but having trouble getting this one to print in my cmd.
int a = 1; a = a + a; a = a + a; a = a + a; ...
This should be what your looking for.
public static void main(String[] args)
{
int startValue = 1;
int numberOfAdditions = 10;
int currentValue = startValue;
for(int i = 0;i<numberOfAdditions;i++)
{
//do opperations here
currentValue = currentValue+currentValue;
//print out value
System.out.println(currentValue);
}
}
Try this:
int a = 1;
for (int i = 0 ; i < MAX_PRINTS ; i++) {
System.out.println(a);
a *= 2;
}
Or if you want to print until a certain value is reached:
int a = 1;
while (a <= MAX_VALUE) {
System.out.println(a);
a *= 2;
}
Related
I have been trying to submit my code, but I am getting Runtime Error everytime. I am not able to point out the problem with my code. The code works fine on my computer, it just shows RUNTIME ERROR when I try to submit it.
I coded in IntelliJ.
import java.util.Scanner;
class practice2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = 0;
do {
t = input.nextInt(); // number of test cases
} while (t < 1 || t > 100);
int n = 0; // variable to store length of the string
int k = 0; // variable to store the goodness number
String s = "";
for (int i = 0; i < t; i++) {
do {
n = input.nextInt(); // string length
} while (n < 1 || n > 100);
do {
k = input.nextInt(); // goodness number
} while (k < 0 || k > (n / 2));
input.nextLine(); // clearing buffer
do {
s = input.nextLine();
} while (s.length() != n);
s = s.toUpperCase(); // in uppercase
int minOp = checkGoodness(s, k, n);
System.out.println("case #" + (i + 1) + ": " + minOp);
}
}
public static int checkGoodness(String s, int k, int n) {
char[] sArr = new char[s.length()];
sArr = s.toCharArray();
int score = 0; int minOp = 0;
for (int i = 0; i < sArr.length / 2; i++) {
if (sArr[i] != sArr[sArr.length - i - 1]) {
score++;
}
}
if ( score == k)
minOp = 0;
else
minOp = Math.abs(score - k);
return minOp;
}
}
Make your class public and change it's name from "practice2" to "Solution".
The most frequent runtime errors with submitting to an online judge are the incorrect name of the main class. You should check the requirements for Java and see what name for the main class should you use. Change "practice2" to that and it should work.
Instead of
s = input.nextLine();
Try
s = input.next();
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,m;
string s;
int c=0;
cin>>n>>m;
int j=n-1;
cin>>s;
for(int k=0;k<n/2 && j>=0 ; k++)
{
if(s[k]!=s[j] )
{
c++;
}
j--;
}
cout<<"Case #"<<i<<": "<<abs(m-c);
cout<<endl;
}
}
I'm trying to solve https://leetcode.com/problems/longest-repeating-substring/
I want to use rolling hash to match strings.
However, my codes don't seem to work when I deal with modulo.
For a string with all same characters, the maximum length of repeating substring should be string.length - 1.
public class Main {
public static void main(String[] args) {
String str = "bbbbbbbbbbbbbbbbbbb";
System.out.println(str.length() - 1);
Solution s = new Solution();
System.out.println(s.longestRepeatingSubstring(str));
}
}
class Solution {
public int longestRepeatingSubstring(String S) {
HashSet<Long> h = new HashSet();
long mod = (long)1e7 + 7;
for(int i = S.length() - 1; i >0; i--){
h = new HashSet();
long c = 0;
int j = 0;
for(; j < i; j ++){
c = (c*26 % mod + S.charAt(j) - 'a')% mod;
}
h.add(c);
for(; j < S.length(); j++){
c -= (S.charAt(j - i ) - 'a') * Math.pow(26,i-1)% mod;
c = (c*26 % mod + S.charAt(j) - 'a')% mod;
if(h.contains(c)){
return i;
}
h.add(c);
}
}
return 0;
}
}
Playground for my codes: https://leetcode.com/playground/F4HkxbFQ
We cannot see your original link, we need a password.
The usage of modulo seems to be really complex.
Why not try something like this
class Scratch {
// "static void main" must be defined in a public class.
public static void main(String[] args) {
String str = "bbaaabbbbccbbbbbbzzzbbbbb";
System.out.println(str.length() - 1);
Solution s = new Solution();
System.out.println(s.longestRepeatingSubstring(str));
}
static class Solution {
public int longestRepeatingSubstring(String s) {
int max = -1;
int currentLength = 1;
char[] array = s.toCharArray();
for (int index = 1; index < array.length; index++) {
if (array[index - 1] == array[index]) {
currentLength++;
max = Math.max(max, currentLength);
} else {
currentLength = 1;
}
}
return max;
}
}
}
I was trying to solve a problem based on value and weight. In the task i had to pick out the elements by their value and weight, and find the highest efficiency solution. I receive an answer, however i am having trouble outputting the elements that were used in order to get an answer.
I've tried creating a string in which i place the values, however it gives out an outofbounds error.
public static void main(String[] args) {
String z[] = new String[]{"a","b","c","d","e","f","g","h","l","m"};
int w[] = new int[]{10,2,4,6,8,1,7,11,4,5};
int c[] = new int[]{20,3,5,7,4,1,8,15,8,6};
int maxW = 50;
int n = c.length;
System.out.println("");
int a = Find(w,c,maxW,n,z);
System.out.println("max value is " + a);
}
static int max(int a, int b)
{
if(a>b)
{
return a;
}
return b;
}
public static int Find(int w[],int c[], int maxW,int n, String[]z)
{
int K[][] = new int[n + 1][maxW + 1];
String s = "";
// Build table K[][] in bottom up manner
for (int i = 0; i<= n; i++)
{
for(int j = 0; j<= maxW; j++)
{
if (i == 0 || j == 0)
{
K[i][j] = 0;
}
else if (w[i - 1]<= j)
{
K[i][j] = max(c[i - 1] + K[i - 1][j - w[i - 1]], K[i - 1][j]);
}
else
{
K[i][j] = K[i - 1][j];
}
}
}
return K[n][maxW];
}
}
i want to output the same index element in string z, as the index element that is used to find the efficiancy.
The ideal result would be something like this in a string:
a a a b c d e m
(Just an example)
Thank you in advance.
So I'm trying to do what I thought would be a simple problem in Java. I don't know if it's because I'm new to Java, or that I'm just making a silly mistake, but I'm getting 3341, which is the wrong answer.
Link to Question: https://projecteuler.net/problem=53
import java.math.BigInteger;
public class Main {
public static BigInteger[] factorial = new BigInteger[101];
static public void main(String[] args)
{
factorial[0] = BigInteger.ONE;
for(int i = 1; i <= 100; i++)
factorial[i] = factorial[i-1].multiply( BigInteger.valueOf(i) );
int count = 0;
for(int n = 1; n <= 100; n++)
{
for(int r = 0; r <= n; r++)
{
if( choose(n, r) > 1000000)
{
count++;
}
}
}
System.out.println("There are " + count + " values > 1,000,000 ");
}
static public long choose(int n, int r)
{
return ( factorial[n].divide( ( factorial[r].multiply(factorial[n-r]) ) ) ).longValue();
}
}
Some of those numbers are pretty large. Certainly larger than would fit into a long value.
Try doing all the calculations, including the comparisons, as BigInteger.
public static BigInteger choose(int n, int r)
{
return factorial[n].divide(factorial[r].multiply(factorial[n - r]));
}
And then in the main code, something like:
final BigInteger max = BigInteger.valueOf(1000000);
for (int n = 1; n <= 100; n++)
{
for (int r = 0; r <= n; r++)
{
if (choose(n, r).compareTo(max) > 0)
{
count++;
}
}
}
I have the current code:
public class Individual{
static int DNA_LOWER_DOMAIN = -100;
static int DNA_UPPER_DOMAIN = 100;
private double fitness = 0.0;
private int x_coordinate = 0;
private String bit_x_coordinate;
Individual(){
int number = DNA_LOWER_DOMAIN + (int)(Math.random() * (DNA_UPPER_DOMAIN - DNA_LOWER_DOMAIN) + 1);
x_coordinate = number;
bit_x_coordinate = Integer.toBinaryString(x_coordinate);
}
public void store_gene(int i, int get_gene) {
bit_x_coordinate = bit_x_coordinate.replace(bit_x_coordinate.charAt(i),(char) get_gene);
}
}
The function that calls store_gene
private static Individual crossover(Individual inhabitant1, Individual inhabitant2){
Individual child = new Individual();
for(int i = 0; i < inhabitant1.size(); i++){
if(Math.random() <= uniformalRate){
child.store_gene(i, inhabitant1.get_gene(i));
}
else{
child.store_gene(i, inhabitant2.get_gene(i));
}
}
return child;
}
anyway when I go to compare using the index of the binary string, I have an out of bounds error because negative integers will be longer than positive ones, any idea how I can solve this problem?
ToBinaryString() doesn't print the leading zeros. You have to pad the String.
Ive solved the problem like this
Individual(){
int number = DNA_LOWER_DOMAIN + (int)(Math.random() * (DNA_UPPER_DOMAIN - DNA_LOWER_DOMAIN) + 1);
x_coordinate = number;
bit_x_coordinate = Integer.toBinaryString(x_coordinate);
if(bit_x_coordinate.length() < 32){
int number_of_bits = bit_x_coordinate.length();
for(int i = 0; i < (32-number_of_bits); i++){
bit_x_coordinate = "0" + bit_x_coordinate;
}
}