求問這六道題用C#怎么編呢






uj5u.com熱心網友回復:
1. 毋庸置疑for回圈雙層for2.a)反而轉成string格式方便.leng完事 ,b)輸出每位知道位數截取Substring截取唄,c).截取都截取到了存陣列for回圈倒著遍歷
3.雙層for回圈遍歷這些所有的數是只可以被1和自身整除,是則add到陣列!輸出完事!
4.二進制公式你知道了! 不斷除以取余數存陣列,拼接! 建議使用while()回圈 do{} while()也行

5.冒泡法 不斷對比就完事
6.switch(){ case 0: case 1 ......... } 就那樣
uj5u.com熱心網友回復:
一
using System;
namespace Study {
class ExeC1 {
static void Main() {
bool flag = true;
while (flag) {
Console.Write("請輸入層數(2-10),輸入0退出:");
string value = Console.ReadLine();
int layer;
if (!int.TryParse(value, out layer)) {
Console.WriteLine("請輸入有效的數字!");
continue;
}
if (layer == 0) {
flag = false;
continue;
}
if (layer < 2 || layer > 10) {
Console.WriteLine("請輸入2-10的數字!");
continue;
}
Triangle(layer);
}
}
static void Triangle(int n) {
for (int y = 1; y <= n; y++) {
for (int space = n - y - 1; space >= 0; space--) {
Console.Write(" ");
}
for (int star = 1; star <= y * 2 - 1; star++) {
Console.Write("*");
}
Console.WriteLine();
}
}
}
}
uj5u.com熱心網友回復:
二, 這題目要求有點變扭,直接就可以寫出的脫褲子放屁用陣列,寫的一點也不優雅.導致后面的不想寫了.
using System;
using System.Collections.Generic;
namespace Study {
class ExeC2 {
static void Main() {
bool flag = true;
while (flag) {
Console.Write("請輸入一個正整數,輸入0退出:");
string value = Console.ReadLine();
int positiveInteger;
if (!int.TryParse(value, out positiveInteger)) {
Console.WriteLine("請輸入有效的數字!");
continue;
}
if (positiveInteger == 0) {
flag = false;
continue;
}
if (positiveInteger < 0) {
Console.WriteLine("請輸入正整數!");
continue;
}
Console.WriteLine("第一題:");
Console.WriteLine(Fun1(positiveInteger));
Console.WriteLine("第二題:");
Fun2(positiveInteger);
Console.WriteLine("第三題(1):");
Fun31(positiveInteger);
Console.WriteLine("第三題(2):");
Fun32(positiveInteger);
}
}
static int Fun1(int n) {
int quotient = n;
int count = 0;
while (quotient >= 10) {
count++;
quotient /= 10;
}
return count + 1;
}
static void Fun2(int n) {
int[] array = PreProcessor(n);
int count = 1;
for (int i = array.Length - 1; i >= 0; i--) {
Console.Write("第" + count + "位:");
count++;
Console.WriteLine(array[i]);
}
}
static void Fun31(int n) {
int[] array = PreProcessor(n);
int count = 1;
for (int i = 0; i <= array.Length - 1; i++) {
Console.Write("第" + (array.Length - count + 1).ToString() + "位:");
count++;
Console.WriteLine(array[i]);
}
}
static void Fun32(int n) {
int[] array = PreProcessor(n);
int count = 1;
for (int i = 0; i <= array.Length - 1; i++) {
count++;
Console.Write(array[i]);
}
Console.WriteLine();
}
static int[] PreProcessor(int n) {
List<int> block = new List<int>();
int quotient = n;
while (quotient >= 1) {
block.Add(quotient % 10);
quotient /= 10;
}
return block.ToArray();
}
}
}
uj5u.com熱心網友回復:
大佬啊,感謝救命啊,謝謝大佬!
uj5u.com熱心網友回復:
第三題, 不是大佬,這題目都很基礎,大佬沒興趣做的,我也是做著玩,給有心學習的看看吧
using System;
namespace Study {
class ExeC3 {
static void Main() {
int count = 0;
for(int i = 1; i <= 1000; i++) {
if (IsPrime(i)) {
count++;
Console.Write(i + " ");
}
}
Console.WriteLine();
Console.WriteLine("1到1000共有素數" + count + "個");
}
static bool IsPrime(int n) {
if (n==1) { return false; }
//可優化,回圈不需要到n,只需要到根號n即可
for (int i = 2; i < n; i++) {
if (n % i == 0) { return false; }
}
return true;
}
}
}
uj5u.com熱心網友回復:
后面幾題沒啥興趣做了,本身都已經提供方法了,一句話就可以實作的,再自己實作一次沒意思.
using System;
namespace Study
{
class Program
{
static void Main() {
//第4題
int n = 100;
Console.WriteLine(Convert.ToString(n,2));
//第5題
int[] arr = { 9,8,7,6,5,4,3,2,1 };
Array.Sort(arr);
//第6題,用datatable的計算功能很方便
System.Data.DataTable table = new System.Data.DataTable();
string e = "1+1";
object test = table.Compute(e, "");
Console.WriteLine(test);
}
}
}
uj5u.com熱心網友回復:
大佬謙虛了,謝謝大佬
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/63481.html
標籤:C#
