- UID
- 244137
- 在线时间
- 7 小时
- 最后登录
- 2020-1-7
- 注册时间
- 2012-10-7
- 宅魂
- 0 点
- 贡献
- 0 点
- 宅币
- 58947 枚
- 灵石
- 1 块
- 元气(技能点)
- 19 点
- 活跃
- 2402 ℃
- 听众
- 9
- 收听
- 0
签到天数: 538 天 连续签到: 37 天 [LV.9]以坛为家II
第三章
- 积分
- 59946
|
发表于 2013-4-17 22:57:55
|
显示全部楼层
本帖最后由 夏颉 于 2013-4-18 09:14 编辑
参与人ID(UID): 夏颉(244137)
参与类型: A 编程题目类
答案:由于代码有点长,不方便,所以已经打包上传,工程基于.NET FrameWork2.0,VS2005及其以上版本均可以打开;
Release目录下为已编译程序,可双击直接运行!
=============================================
[mw_shl_code=csharp,true]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using System.IO;
namespace AcOnline_gn00
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//一
this.textBox1.Text = "1000";
//二
this.textBox3.Text = "100";
this.textBox4.Text = "10";
//第六题
this.textBox9.Text = "abcdefghijklmnopqrstuvwxyz";
//第七题
this.textBox11.Text = "1654656";
//第九题
this.textBox14.Text = "123 25 45 765 345 99 34 55 77";
this.textBox14.Enabled = false;
}
#region 第一题
/// <summary>
/// 第一题
/// 一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完数。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int inputVal = Utils.StrToInt(this.textBox1.Text.Trim(),0);
if(inputVal >= 6)
{
StringBuilder sb = new StringBuilder();
List<int> array = new List<int>();
int i,j,s;
for (j = 2; j < inputVal;j++)
{
s = j;
for (i = 1; i < j; i++)
{
if ((j % i) == 0)
{
s = s - i;
array.Add(i);
}
}
if (s == 0)
{
sb.Append(j + " ");
}
}
this.textBox2.Text = sb.ToString();
}
else
{
MessageBox.Show(this,"请输入大于5的值","提示",MessageBoxButtons.OK);
}
}
#endregion
#region 第二题
/// <summary>
/// 第二题
/// 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
float inputHigh = Utils.StrToFloat(this.textBox3.Text.Trim(), 0.00F);
int inputF = Utils.StrToInt(this.textBox4.Text.Trim(),0);
if (inputHigh > 0 && inputF > 0)
{
float allCount = -1 * inputHigh;
float tmpHigh = inputHigh;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < inputF; i++)
{
allCount += tmpHigh * 2;
tmpHigh = tmpHigh / 2;
}
sb.Append(String.Format("共经过的{0}米;",allCount) + "\r\n");
sb.Append(String.Format("第{0}次反弹的高度为{1}", inputF, tmpHigh));
this.textBox5.Text = sb.ToString();
}
else
{
MessageBox.Show(this, "请输入大于0的值", "提示", MessageBoxButtons.OK);
}
}
#endregion
#region 第三题
/// <summary>
/// 第三题
/// 两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。
/// 有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
//string[] arr_jia = { "a", "b", "c"};
//string[] arr_yi = { "x", "y", "c"};
StringBuilder sb = new StringBuilder();
//i是a的对手,j是b的对手,k是c的对手
char i, j, k;
for (i = 'x'; i <= 'z'; i++)
{
for (j = 'x'; j <= 'z'; j++)
{
if (i != j)
{
for (k = 'x'; k <= 'z'; k++)
{
if (i != k && j != k)
{
if (i != 'x' && k != 'x' && k != 'z')
{
sb.Append(String.Format("比赛顺序: a--{0}\tb--{1}\tc--{2}\r\n", i, j, k));
}
}
}
}
}
}
this.textBox6.Text = sb.ToString();
}
#endregion
#region 第四题
/// <summary>
/// 第四题
/// 有5个人坐在一起,问第五个人多少岁?
/// 他说比第4个人大2岁。
/// 问第4个人岁数,他说比第3个人大2岁。
/// 问第三个人,又说比第2人大两岁。
/// 问第2个人,说比第一个人大两岁。
/// 最后问第一个人,他说是10岁。请问第五个人多大?
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
this.textBox7.Text = "第五个人的年龄:" + calage(5).ToString();
}
private int calage(int n)
{
int age;
if(n == 1)
{
age = 10;
}
else
{
age = calage(n - 1) + 2;
}
return age;
}
#endregion
#region 第五题
/// <summary>
/// 第五题
/// 编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
/// Start AWS:
/// 由于我不会C/C++,这道题用C#作答 代码跟其它题相比稍多,所以就选择对单链表进行模拟排序
/// 在本题的排序过程中,我发现冒泡排序和选择排序都要求内层循环从链表的末尾向前走,这明显是不合适的;所以我最终选择了插入排序算法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
Link head = GenerateLink();
Link newHead = SortLink(head);
Link curr = newHead;
while (curr.Next != null)
{
sb.Append(curr.Next.Data + " ");
curr = curr.Next;
}
this.textBox8.Text = sb.ToString();
}
public static Link GenerateLink()
{
Link element8 = new Link(null, 113);
Link element7 = new Link(element8, 123);
Link element6 = new Link(element7, 143);
Link element5 = new Link(element6, 91);
Link element4 = new Link(element5, 7);
Link element3 = new Link(element4, 65);
Link element2 = new Link(element3, 25);
Link element1 = new Link(element2, 5);
Link head = new Link(element1, int.MinValue);
return head;
}
public static Link SortLink(Link head)
{
Link pre1 = head;
Link pre2 = head.Next;
Link min = null;
for (Link curr1 = head.Next; curr1 != null; curr1 = min.Next)
{
if (curr1.Next == null)
break;
min = curr1;
for (Link curr2 = curr1.Next; curr2 != null; curr2 = curr2.Next)
{
if (curr2.Data < curr1.Data)
{
min = curr2;
curr2 = curr1;
curr1 = min;
pre1.Next = curr1;
curr2.Next = curr1.Next;
curr1.Next = pre2;
if (pre2 != curr2)
pre2.Next = curr2;
}
pre2 = curr2;
}
pre1 = min;
pre2 = min.Next;
}
return head;
}
#endregion
#region 第六题
/// <summary>
/// 第六题
/// 编写反转字符串的程序,要求优化速度、优化空间。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
int times = 1000000;
string initStr = this.textBox9.Text;
int strLen = initStr.Length;
string[] resultStrArr = new string[strLen];
string result = String.Empty;
Stopwatch watch1 = new Stopwatch();
watch1.Start();
for (int i = 0; i < times; i++)
{
for (int j = strLen-1; j >= 0;j--)
{
resultStrArr[strLen-j-1] = initStr.Substring(j,1);
}
result = String.Concat(resultStrArr);
}
watch1.Stop();
this.textBox9.Text = result;
this.textBox10.Text = watch1.Elapsed.ToString();
}
#endregion
#region 第七题
/// <summary>
/// 第七题
/// 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分
/// 按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的
/// 部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入
/// 当月利润,求应发放奖金总数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
float inputVal = Utils.StrToFloat(this.textBox11.Text.Trim(), 0);
if (inputVal >= 0)
{
float allCount = inputVal * 0.1F;
if (inputVal > 100000)
{
allCount += (inputVal - 100000) * 0.075F;
if (inputVal > 200000)
{
allCount += (inputVal - 200000) * 0.05F;
if (inputVal > 400000)
{
allCount += (inputVal - 200000) * 0.03F;
if (inputVal > 600000)
{
allCount += (inputVal - 200000) * 0.015F;
if (inputVal > 1000000)
{
allCount += (inputVal - 200000) * 0.01F;
}
}
}
}
}
this.textBox12.Text = allCount.ToString();
}
else
{
MessageBox.Show(this, "请输入大于0的值", "提示", MessageBoxButtons.OK);
}
}
#endregion
#region 第八题
/// <summary>
/// 第八题
/// 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
/// 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。
/// 到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
int result = 1;
for (int i = 9; i > 0; i--)
{
result = (result + 1) * 2;
}
this.textBox13.Text = result.ToString();
}
#endregion
#region 第九题
/// <summary>
/// 第九题
/// 输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
string[] initStrArr = this.textBox14.Text.Split(' ');
int[] initIntArr = new int[initStrArr.Length];
for(int i = 0;i<initIntArr.Length;i++)
{
initIntArr = Utils.StrToInt(initStrArr,0);
}
//int minIndex = 0;
int min = 0;
//int maxIndex = 0;
int max = 0;
//遍历数组,找出最大值,跟max交换,遍历数组,找出最小值,跟min交换
for (int i = 0; i < initIntArr.Length;i++)
{
if (initIntArr < initIntArr[min])
{
min = i;
}
if (initIntArr > initIntArr[max])
{
//max = initIntArr;
max = i;
}
}
int tmp = initIntArr[min];
initIntArr[min] = initIntArr[0];
initIntArr[0] = tmp;
tmp = initIntArr[max];
initIntArr[max] = initIntArr[initIntArr.Length - 1];
initIntArr[initIntArr.Length - 1] = tmp;
foreach (int item in initIntArr)
{
this.textBox15.Text += item.ToString() + " ";
}
}
#endregion
#region 第十题
/// <summary>
/// 第十题
/// 设计并实现一个密码加密算法,要求加密后的密文不能依据程序而进行逆向解析(尽可能的不能被解析)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_Click_1(object sender, EventArgs e)
{
string s = "";
s += Test("");
s += Test("a");
s += Test("abc");
s += Test("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
s += Test("abcdefghijklmnopqrstuvwxyz");
s += Test("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
s += Test("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
this.textBox16.Text = s;
}
private static UInt32 Message_Digest1 = 0x67452301;
private static UInt32 Message_Digest2 = 0xEFCDAB89;
private static UInt32 Message_Digest3 = 0x98BADCFE;
private static UInt32 Message_Digest4 = 0x10325476;
private static UInt32 Message_Digest5 = 0xC3D2E1F0;
private static UInt32 SHA1CircularShift(int bits, UInt32 word)
{
return ((word << bits) & 0xFFFFFFFF) | (word) >> (32 - (bits));
}
private static void SHA1_Init()
{
Message_Digest1 = 0x67452301;
Message_Digest2 = 0xEFCDAB89;
Message_Digest3 = 0x98BADCFE;
Message_Digest4 = 0x10325476;
Message_Digest5 = 0xC3D2E1F0;
}
private static UInt32[] SHA1_Append(byte[] input)
{
int zeros = 0;
int ones = 1;
int size = 0;
int n = input.Length;
int m = n % 64;
if (m < 56)
{
zeros = 55 - m;
size = n - m + 64;
}
else if (m == 56)
{
zeros = 63;
ones = 1;
size = n + 8 + 64;
}
else
{
zeros = 63 - m + 56;
size = n + 64 - m + 64;
}
ArrayList bs = new ArrayList(input);
if (ones == 1)
{
bs.Add((byte)0x80); // 0x80 = 10000000
}
for (int i = 0; i < zeros; i++)
{
bs.Add((byte)0);
}
UInt64 N = (UInt64)n * 8;
byte h8 = (byte)(N & 0xFF);
byte h7 = (byte)((N >> 8) & 0xFF);
byte h6 = (byte)((N >> 16) & 0xFF);
byte h5 = (byte)((N >> 24) & 0xFF);
byte h4 = (byte)((N >> 32) & 0xFF);
byte h3 = (byte)((N >> 40) & 0xFF);
byte h2 = (byte)((N >> 48) & 0xFF);
byte h1 = (byte)(N >> 56);
bs.Add(h1);
bs.Add(h2);
bs.Add(h3);
bs.Add(h4);
bs.Add(h5);
bs.Add(h6);
bs.Add(h7);
bs.Add(h8);
byte[] ts = (byte[])bs.ToArray(typeof(byte));
/* Decodes input (byte[]) into output (UInt32[]). Assumes len is
* a multiple of 4.
*/
UInt32[] output = new UInt32[size / 4];
for (Int64 i = 0, j = 0; i < size; j++, i += 4)
{
UInt32 temp = 0;
temp = temp | (((UInt32)ts) << 24);
temp = temp | (((UInt32)ts[i + 1]) << 16);
temp = temp | (((UInt32)ts[i + 2]) << 8);
temp = temp | (((UInt32)ts[i + 3]));
output[j] = temp;
}
return output;
}
private static UInt32[] SHA1_Transform(UInt32[] x)
{
SHA1_Init();
UInt32[] K = {0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};
int t;
UInt32 temp;
UInt32[] W = new UInt32[80];
UInt32 A, B, C, D, E;
for (int k = 0; k < x.Length; k += 16)
{
for (t = 0; t < 16; t++)
{
W[t] = x[t + k];
}
for (t = 16; t < 80; t++)
{
W[t] = SHA1CircularShift(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
}
A = Message_Digest1;
B = Message_Digest2;
C = Message_Digest3;
D = Message_Digest4;
E = Message_Digest5;
for (t = 0; t < 20; t++)
{
temp = SHA1CircularShift(5, A) +
((B & C) | ((~B) & D)) + E + W[t] + K[0];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30, B);
B = A;
A = temp;
}
for (t = 20; t < 40; t++)
{
temp = SHA1CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[1];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30, B);
B = A;
A = temp;
}
for (t = 40; t < 60; t++)
{
temp = SHA1CircularShift(5, A) +
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30, B);
B = A;
A = temp;
}
for (t = 60; t < 80; t++)
{
temp = SHA1CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[3];
temp &= 0xFFFFFFFF;
E = D;
D = C;
C = SHA1CircularShift(30, B);
B = A;
A = temp;
}
Message_Digest1 = (Message_Digest1 + A) & 0xFFFFFFFF;
Message_Digest2 = (Message_Digest2 + B) & 0xFFFFFFFF;
Message_Digest3 = (Message_Digest3 + C) & 0xFFFFFFFF;
Message_Digest4 = (Message_Digest4 + D) & 0xFFFFFFFF;
Message_Digest5 = (Message_Digest5 + E) & 0xFFFFFFFF;
}
return new UInt32[] { Message_Digest1, Message_Digest2, Message_Digest3, Message_Digest4, Message_Digest5 };
}
public static string SHA1Array(UInt32[] input)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
sb.Append(String.Format("{0:X8}", input).ToUpper());
}
return sb.ToString();
}
public static string MySHA1String(string message)
{
char[] c = message.ToCharArray();
byte[] b = new byte[c.Length];
for (int i = 0; i < c.Length; i++)
{
b = (byte)c;
}
UInt32[] output = SHA1_Append(b);
UInt32[] str = SHA1_Transform(output);
return SHA1Array(str);
}
public static string MySHA1File(string fileName)
{
FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read);
byte[] array = new byte[fs.Length];
fs.Read(array, 0, (int)fs.Length);
fs.Close();
UInt32[] output = SHA1_Append(array);
UInt32[] str = SHA1_Transform(output);
return SHA1Array(str);
}
public static string Test(string message)
{
return "\r\nSHA1 (\"" + message + "\") = " + MySHA1String(message);
}
#endregion
}
/// <summary>
/// 五
/// </summary>
public class Link
{
public Link Next;
public int Data;
public Link(Link next, int data)
{
this.Next = next;
this.Data = data;
}
}
}[/mw_shl_code][mw_shl_code=csharp,true] public class Utils
{
public static int StrToInt(string str, int defValue)
{
int rtnValue = defValue;
if (String.IsNullOrEmpty(str) || str.Trim().Length > 10)
{
return rtnValue;
}
bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*(\.\w*)?$").IsMatch(str.Trim());//@"^([-]|[0-9])[0-9]*$"
if (IsInt)
{
Int32.TryParse(str.Trim(), out rtnValue);
}
return rtnValue;
}
public static float StrToFloat(string strValue, float defValue)
{
float rtnValue = defValue;
if ((strValue == null) || (strValue.Trim().Length > 10))
{
return rtnValue;
}
bool IsFloat = new Regex(@"^([-]|[0-9])[0-9]*(\.\w*)?$").IsMatch(strValue.Trim());
if (IsFloat)
{
Single.TryParse(strValue, out rtnValue);
}
return rtnValue;
}
}[/mw_shl_code]
|
评分
-
查看全部评分
|