老师布置的作业
【程序 1】
有 1,2,3,4 个数字,能组成多少个互不相同且无重复数字的三位数? 都是多少?【程序 2】
有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。【程序3】
有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
【程序 1】
有 1,2,3,4 个数字,能组成多少个互不相同且无重复数字的三位数? 都是多少?【程序 2】
有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。【程序3】
有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
for (int i = 1; i <= 4; i++)
{
number.Clear();
number.Add(1);
number.Add(2);
number.Add(3);
number.Add(4);
number.Remove(i);
int a1 = number[0] * 100 + number[1] * 10 + number[2];
int a2 = number[1] * 100 + number[0] * 10 + number[2];
int a5 = number[1] * 100 + number[2] * 10 + number[0];
int a3 = number[0] * 100 + number[2] * 10 + number[1];
int a4 = number[2] * 100 + number[1] * 10 + number[0];
int a6 = number[2] * 100 + number[0] * 10 + number[1];
Console.Write(" " + a1 + " " + a2 + " " + a3 + " " + a4 + " " + a4 + " " + a5 + " " + a6);
{
Queue<int> q = new Queue<int>();
for (int i = 1; i <= size; i++)
{
q.Enqueue(i);
}
int n = 1;
while (q.Count > 1)
{
if (n == 3)
{
n = 1;
q.Dequeue();
}
else
{
n++;
q.Enqueue(q.Dequeue());
}
}
return q.Dequeue();
}
// 第一题 这是最懒的算法
int[] nums = { 1, 2, 3, 4 };int[] output = new int[0];for (int x = 0; x < nums.Length; x++)
{
for (int y = 0; y < nums.Length; y++)
{
for (int z = 0; z < nums.Length; z++)
{
if (x == y || x == z || y == z) continue; int val = nums[x] * 100 + nums[y] * 10 + nums[z];
Array.Resize(ref output, output.Length + 1);
output.SetValue(val, output.Length - 1);
}
}
}
Console.WriteLine("count: {0}", output.Length);
Console.WriteLine("list: {0}", string.Join(",", output));
错误 2 参数“2”: 无法从“string”转换为“string[]” C:\415\ConsoleApplication1\var\vvat\last\Program.cs 52 61 last
那是因为 dotNetFramework 版本不同 我用的是 4.0 有5个重载的方法你改下代码吧
int[] nums = { 1, 2, 3, 4 };string[] output = new string[0]; // 改为 stringfor (int x = 0; x < nums.Length; x++)
{
for (int y = 0; y < nums.Length; y++)
{
for (int z = 0; z < nums.Length; z++)
{
if (x == y || x == z || y == z) continue; string val = string.Format("{0}{1}{2}",
nums[x], nums[y], nums[z]); // 不用计算的 Array.Resize(ref output, output.Length + 1);
output.SetValue(val, output.Length - 1);
}
}
}
Console.WriteLine("count: {0}", output.Length);
Console.WriteLine("list: {0}", string.Join(",", output));
// 用来输出 int[] 数组
private void print(int[] nums)
{
string[] output = new string[nums.Length];
for (int i = 0; i < nums.Length; i++) output[i] = string.Format("{0}", nums[i]);
Console.WriteLine(string.Join(",", output));
}
private void print(int[] nums, int offset, int count)
{
string[] output = new string[nums.Length];
for (int i = 0; i < nums.Length; i++) output[i] = string.Format("{0}", nums[i]);
Console.WriteLine(string.Join(",", output, offset, count));
}
方法一:利用数组索引进行计算由于索引从0开始,所以报3的人实际是 3 - 1 表达式为 n % 3 == 2
然后跳过报3的人,而将其余的数追加到数组末尾,直到定义的标识位 < 3
最后输出该数组的最后2位即可
private void mod(int n, int m)
{
// 定义并初始化数组
int[] nums = new int[n]; for (int i = 0; i < n; i++) nums[i] = i + 1; int index = 0, remain = n; // 定义索引和标识位 do
{
if ((index) % m == m - 1)
{
remain--; // 标识位 - 1
}
else
{
// 将不能被3整除的数追加到数组末尾
Array.Resize(ref nums, nums.Length + 1);
nums.SetValue(nums[index], nums.Length - 1);
}
index++;
}
while (remain >= m); // 输出
this.print(nums, nums.Length - remain, remain);
}
方法二:利用递归生成新数组实现可以输出下面的数据1,2,3,4,5,6,7,8,9,10
10,1,2,4,5,7,8
8,10,1,4,5
4,5,8,10
10,4,5
10,41,2,3,4,5,6,7,8,9,10,11
10,11,1,2,4,5,7,8
7,8,10,11,2,4
7,8,11,2
2,7,8
2,7观察上面的序列发现每一层的个数,是上一层个数减去上一层个数整除3后的差
假设 n = 10, m = 3; 那么新数组的 lenght = n - (int)(n / m);而最后剩余的数可以放到前面来再次进行计算
这样,就可以在每次递归计算时直接定义数组
int[] output = new int[n - (int)(n / m)];然后将原数组的余数 Copy 到新数组
Array.Copy(input, intput.Length - intput.Length % m, output, 0, intput.Length % m);然后将input前面不能整除3的数放在新数组output的后面最后再次递归,直到数组的 length < m
// 递归方法 用于处理数组
private int[] procNums(int[] nums, int m)
{
int l = nums.Length, index = l % m;
int[] result = new int[l - (int)(l / m)]; for (int i = 0; i < l - l % m; i++)
{
if (i % m == m - 1) continue;
result[index] = nums[i];
index++;
} Array.Copy(nums, l - l % m, result, 0, l % m); return result;
}// n:10, m:3 用于生成输入数组
private void iniNums(int n, int m)
{
int[] nums = new int[n];
for (int i = 0; i < n; i++) nums[i] = i + 1; this.print(nums); while (nums.Length >= m)
{
nums = procNums(nums, m);
this.print(nums);
}
}// 调用
this.mod(10, 3);
this.mod(11, 3);
this.mod(15, 3);
this.mod(100, 3);this.iniNums(10, 3);
this.iniNums(11, 3);
this.iniNums(15, 3);
this.iniNums(100, 3);
for (int i = 0; i < ss.Length; i++)
{
for (int k = 0; k < ss.Length; k++)
{
for (int j = 0; j < ss.Length; j++)
{
if (i != k && k != j&&i!=j )
{
Console.WriteLine("{0},{1},{2}",
ss[i],ss[k], ss[j]);
}
}
}
{
public int id { get; set; }
public string name { get; set; }
public float chs { get; set; }
public float math { get; set; }
public float eng { get; set; }
public float avg
{
get
{
return (float)Math.Round((chs + math + eng) / 3f, 1,
MidpointRounding.AwayFromZero);
}
}
}private void subject3()
{
// 模拟输入
student[] students = new student[]
{
new student(){id = 1, name = "student1", chs = 85, eng = 90, math = 93 },
new student(){id = 2, name = "student2", chs = 92, eng = 91, math = 99 },
new student(){id = 3, name = "student3", chs = 65, eng = 73, math = 88 },
new student(){id = 4, name = "student4", chs = 82, eng = 69, math = 76 },
new student(){id = 5, name = "student5", chs = 84, eng = 87, math = 88 }
}; using (StreamWriter sw = new StreamWriter("c:\\students.txt", false, Encoding.Default))
{
sw.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
"id", "name", "chs", "math", "eng", "avg")); foreach (student stud in students)
{
sw.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",
stud.id, stud.name, stud.chs, stud.math, stud.eng, stud.avg));
} sw.Close();
}
}
你这也是学?自己不动脑,学毛啊。先想想,看看书,然后和同学讨论讨论,再不济就GOOGLE一下,伸手党算什么。
using namespace std;void main()
{
int i,j,k,num=0;
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
for(k=1;k<=4;k++)
if((i!=j)&&(i!=k)&&(j!=k))
num++;
cout<<num<<endl;
}
using namespace std;
void main()
{
int i,j,n,num=1;
cout<<"请输入n的值:";
cin>>n;
int *a=new int[n+1];
for(i=1;i<n;i++)
a[i]=i+1;
a[n]=1;
i=1;
while(a[i]!=i)
{
num=1;
while(num<3)
{
j=i;
num++;
i=a[i];
}
a[j]=a[i];
i=a[i];
}
cout<<i<<endl;
}
int main()
{
int n,i,m=0;
printf("Enter the num:"); scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(m==0) m=i;
if(i+1<=n) m=(m+3)%(i+1);
}
printf("The result :%d\n",m);
scanf("%d",&n); /* pause */
return 0;
}
{
int i,j,k,n=0;
for(i=1;i<5;i++){
for(j=1;j<5;j++){
for(k=1;k<5;k++){
if(i!=k&&i!=j&&k!=j){
n++;
printf("%d%d%d ",i,j,k);
if(n%4==0){
printf("\n");}
}
}
}
}
return 0;
}
for($i=1,$num=0;$i<=4;$i++){
for($j=1;$j<=4;$j++){
if($j==$i) continue;
for($k=1;$k<=4;$k++){
if($k==$i||$k==$j) continue;
$num++;
echo $i.$j.$k."<br>";
}
}
}
echo $num."<br>";
/* 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。*/
$n=5; $arr1=array();
for($i=1;$i<=$n;$i++) $arr1[]=$i; while(count($arr1)>2){
for($j=0,$pos=0;$j<count($arr1);$j++){
if(($j-2)%3==0){
$pos=$j;
continue;
}
}
$arr2=array();
for($j=$pos+1,$num=0;$j<count($arr1);$j++){
$arr2[]=$arr1[$j];
$num++;
}
for($j=0;$j<count($arr1)-$num;$j++){
if(($j-2)%3==0){
continue;
}
$arr2[]=$arr1[$j];
} $arr1=array();
$arr1=$arr2;
} print_r($arr1);