Heliocentic in c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
So perhaps, you've generated some fancy text, and you're content that you can now copy and paste your fancy text in the comments section of funny cat videos, but perhaps you're wondering how it's even possible to change the font of your text? Is it some sort of hack? Are you copying and pasting an actual font?
days++;
earth++;
mars++;
earth %= 365;
mars %= 687;
}
Console.Write(days);
Console.Write("\n");
Console.ReadLine();
n++;
}
}
}

It appears you're only missing a way to process the sample input. It sounds like it's coming in "lines", but that's not really defined, so in this example I'm just using an array. I'm ignoring the fact that the first item specifies the number of testcases, since in this implementation the number is calculated.
Basically I'm just walking through the array, grabbing two items at a time (skipping the first one), and using those values for earth and mars. The rest is mostly your code (except some changes to outputting to the console):
int[] sampleInput = {5, 0, 0, 364, 686, 360, 682, 0, 1, 1, 0};
int n = 1;
for (var i = 1; i < sampleInput.Length - 1; i += 2)
{
int earth = sampleInput[i];
int mars = sampleInput[i + 1];
int days = 0;
while (earth != 0 || mars != 0)
{
days++;
earth++;
mars++;
earth %= 365;
mars %= 687;
}
Console.WriteLine($"Case {n}: {days}");
n++;
}
Console.ReadLine();
Output

Related

Printing numbers 1 to 100 C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am new here and in C# and I dont't know how to figure out that example..Printing numbers 1 to 100 that way "100, 1, 2, 99, 98, 3, 4, 97,....52, 49, 50, 51". I have a course work and should write it befoure 25.10..Thank you all!
for (int i = 1; i < 51; i+=2)
{
Console.WriteLine(101-i);
Console.WriteLine(i);
Console.WriteLine(1+i);
Console.WriteLine(100-i);
}
EDIT: just i want to add that i use (1+i) // (100-i) because if I use i-- or i++ inside the writeline, it will execute the ++ or -- after the print message
By using a lower and a upper variable and a while-loop, you can do it stright forward without any calculations:
int lower = 1, upper = 100;
while (lower < upper) {
Console.WriteLine(upper--);
Console.WriteLine(lower++);
Console.WriteLine(lower++);
Console.WriteLine(upper--);
}

c# How to replace the for loop? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
var input = Convert.ToInt32(Console.ReadLine());
for (var i = 1; i <= input; i++)
{
for (var tr = 1; tr <= input; tr++)
{
for (var ch = 1; ch <= input; ch++)
{
if (ch <= i) Console.Write("+");
else Console.Write(" ");
}
}
Console.WriteLine();
}
The program outputs triangles. The code uses three loops. How to make it so that there are only two loops and the program works the same as before?
Notice in the pattern of triangles that on the first line you print 1 + then print n-1 blanks repeatedly. On the second line you print two + then print n-2 blanks repeatedly.
To decide if you need to print a plus or a blank you can use modulo arithmetics. if c % n < lineno print + else print a blank.
You print n * n characters in each line.
That's not the complete code but a good boost to implement the solution.

Counting backwards from 2000 in for loop C3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
for(i = 2000; i >= 0; i -= 40)
{
Console.WriteLine("Your score is {0}", i);
}
i'm trying to count backwards from 2000 by 40 every time someone loops another for loop:
int count = 0;
for (int i = 0; i < random || i > random; i++)
{
count++;
Console.WriteLine("\r\nGuess [{0}]: Please enter a number:", i + 1);
string guessString = Console.ReadLine();
I'm trying to make a scoreboard so every time you don't guess a number correctly, it deducts 40 from 2000.
If I understand correctly, the variables you need are: the amount of guesses, the score, the value to find, and then in each iteration you need the user's input. This can be solved with a single loop.
Here's some pseudocode to show how you can approach this:
remainingGuesses = 50
score = 2000
while remainingGuesses > 0
userInput = GetInput()
if userInput == random
break
remainingGuesses = remainingGuesses - 1;
score = score - 40
return score
You could otherwise go from attempt 0 to 49 and say score = 2000 - 40 * attempts
If I understand correctly from the comments above, you want to deduct 40 points every time someone guesses a number incorrectly.
You can have a variable that holds the score and initialize it with 2000 points and create a function that deducts 40 points each time someone guesses wrong.
int startingPoints = 2000;
public void DeductPoints() {
startingPoints -= 40;
}
And then, you can do some if/else statements to do checks on the score and the corresponding output you want to show to the user.

How to count the sum of the numeric found in a string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to count sum of the numbers found in a string, not digits. For example, there are string = "abc12df34", and the answer must be 46 (12+34), not 10. Also maybe negative numbers for string = "abc10gf-5h1" answer must be 6. I can not understand how to implement this.
RegEx approach:
string input = "abc10gf-5h1";
int result = Regex.Matches(input, "-?[0-9]+").Cast<Match>().Sum(x =>int.Parse(x.Value));
While the above answer is elegant, it's not really something to understand what to do or how to approach the problem. Here is another, more explicit solution.
In words, you iterate through your string, collect digits as long as there are any, if you find a nondigit, your number is finished, you convert the number to integer and sum it up, clear the number string. The same you do if you find the end of the string. On the next found digit you start collecting digits again.
This algorithm will fail on any number larger than than 10 digits in multiple ways (as the other answer will also), but this is just for demonstration anyway.
string input = "abc10gf-5-1h1";
var number = new char[10];
int numberlength = 0;
int pos = 0;
int sum = 0;
while (pos < input.Length)
{
char c = input[pos++];
if (char.IsDigit(c))
{
number[numberlength++]=c;
}
else
{
if (numberlength > 0)
{
sum += int.Parse(new String(number, 0, numberlength));
numberlength = 0;
}
if (c=='-')
number[numberlength++]=c;
}
}
if (numberlength > 0)
sum += int.Parse(new String(number, 0, numberlength));
Console.WriteLine(sum);

Dividing BigInteger in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to divide BigIntegers.
this is my code so far:
BigInteger x = BigInteger.Parse("2697841949839130393229424953460693359375000000");
BigInteger y = BigInteger.Parse("2");
for (int i = 0; i < 300; i++) {
double result = Math.Exp(BigInteger.Log(x) / BigInteger.Log(y));
x = result;
Console.WriteLine(result);
}
It does the divide. it does it only once. I want to divide the code to minimum it has. it should be 6.
I know that this:
for (int i = 0; i < 300; i++){}
is not the right way to do it. does any one know any other way how to?
I remember from your other question that you are trying to decode Godel numbers.
You shouldn't stick the result in a double. It's too big. You're looking for BigInteger's Divide method. For your application DivRem is better.
Here's some sample code. It computes 6. This should get you going for the first character. You'll also need to build a list of primes beyond '2' to continue on to the rest of the string.
using System;
using System.Numerics;
public class Program
{
public static void Main()
{
BigInteger x = BigInteger.Parse("2697841949839130393229424953460693359375000000");
BigInteger y = BigInteger.Parse("2");
int counter = 0;
BigInteger remainder;
do{
BigInteger result = BigInteger.DivRem(x, y, out remainder);
if(!remainder.IsZero)
break;
x = result;
counter++;
} while (true);
Console.WriteLine(counter);
}
}

Categories

Resources