一道编程问题 求解一道编程题~~~~急~~~~~有高手吗

作者&投稿:阎砍 (若有异议请与网页底部的电邮联系)
r = m mod n 的意思是求余数,比如 5 mod 2 = 1 因为5/2 余数为1,而5 mod 5 = 0即为能除尽,而 3 mod 7 = 3 因为3/7 = 0 余 3
接下来的loop是在用欧几里得算法算两个数的最大公约数。

a和b的最大公约数是a和b都能除尽的最大的数,ie最大的x st a mod x =0 and b mod x = 0。比如8和12的最大公约数是4
看code:
当m>n时,如果 r = m mod n 为0,那么n就是最大公约数,比如25 mod 5=0最大公约数为5, 16 mod 8=0最大公约数为8

如果r 不是0, 比如r=12 mod 8 = 4,进入loop,那设置新的m=8, n=4, 得到 r = 8 mod 4 = 0, 结束loop。n=4是12和8之间的最大公约数
再看 如果是9和5之间,r = 9 mod 5 = 4, 进入loop,新的m=5, n = 4, 得到r = 5 mod 4 = 1 r仍然不是0,那再loop,设新的m = 4, n = 1, 得到r = 4 mod 1 = 0. 结束loop。得到9和5的最大公约数是n=1。

所以,只有当r=0, 即r不再>0时,loop结束,此时的n,为最大公约数。

最小公倍数 是 一个数x,它是最小的,能同时除尽a和b 的数。比如(12,8)的最小公倍数 = 24 = 12*8/4, 此处4是12和8的最大公约数。

所以有了最大公约数(gcd),可以计算最小公倍数为:x=a*b/gcd(a,b)
所以最下方是通过求出的最大公约数n, 原本的ml和nl, 算出最小公倍数:ml*nl/n

可以百度一下最大公约数,最小公倍数,余数的概念哦,属于代数基础。

一道c++编程问题~

改成这样:
#include
using namespace std;
class Car
{
private:
const char *brand;
const char *type;
int year;
double price;
public:
Car(const char *a = "undefinition", const char *b = "undefinition", int y = 2000, double p = 0);
const char *GetBrand();
const char *GetType();
int GetYear();
double GetPrice();
};
Car::Car(const char *a, const char *b, int y, double p)
{
brand = a;
type = b;
year = y;
price = p;
}
const char *Car::GetBrand()
{
return brand;
}
const char *Car::GetType()
{
return type;
}
int Car::GetYear()
{
return year;
}
double Car::GetPrice()
{
return price;
}
int main()
{
Car car1("FIAT", "Palio", 2007, 6.5);
cout << car1.GetBrand() << "|" << car1.GetType() << "|" << car1.GetYear() << "|" << car1.GetPrice() << endl;
Car car2;
cout << car2.GetBrand() << "|" << car2.GetType() << "|" << car2.GetYear() << "|" << car2.GetPrice() << endl;
return 0;
}

#include
#include
#include
#include
void eval1(int );//对正确答案的评价
void eval2(int );//对错误答案的评价
int main()
{
int a,b;
int answer1, answer2;//记录+、-、*的答案
float answer3, answer4;//记录/的答案
int flags = 1;
int select, evaluate;
int count1 = 0, count2 = 0;//记录总题数、及正确答案的题目数
int temp;
int scores = 0;//记录分数
float rate;//记录正确率
srand((unsigned)time(NULL));
while(flags)
{
count1++;
a = rand() % 10 + 1;
b = rand() % 10 + 1;
evaluate = rand() % 4 + 1;
select = rand() % 4 + 1;
//加法运算
if(select == 1)
{
answer1 = a + b;
printf("%d + %d = ?
",a,b);
printf("Please input the answer:");
scanf("%d",&answer2);
if(answer1 == answer2)
{
count2++;
scores += 10;
printf("Right
");
}
else
{
printf("Wrong
");
}
}
//减法运算
else if(select == 2)
{
if(b > a)
{
temp = a;
a = b;
b = temp;
}
answer1 = a - b;
printf("%d - %d = ?
",a,b);
printf("Please input the answer:");
scanf("%d",&answer2);
if(answer1 == answer2)
{
printf("Right
");
count2++;
scores += 10;
}
else
{
printf("Wrong
");
}
}
//乘法运算
else if(select == 3)
{
answer1 = a * b;
printf("%d * %d = ?
",a,b);
printf("Please input the answer:");
scanf("%d",&answer2);
if(answer1 == answer2)
{
printf("Right
");
count2++;
scores +=10;
}
else
{
printf("Wrong
");
}
}
//除法运算
else
{
//保证分母不为0
if( b == 0)
b++;
answer3 = (float)a / b;
printf("%d / %d = ?
",a,b);
printf("Please input the answer:");
scanf("%f",&answer4);
if(fabs(answer3 - answer4) < 0.001)
{
printf("Right
");
count2++;
scores += 10;
}
else
{
printf("Wrong
");
}
}
//判断正确率
if(count1 == 10)
{
rate = (float)count2 / count1;
if( rate > 0.75)
{
flags = 0;
printf("Scores: %d Rate: %%%.0f
",scores,rate*100);
eval1(evaluate);
}
else
{
printf("Scores: %d Rate: %%%.0f
",scores,rate*100);
printf("Please make the rate greater than %75
");
eval2(evaluate);
}
}
}
return 0;
}
void eval1(int a)
{
switch(a)
{
case 1:printf("Very good!
");break;
case 2:printf("Excellent!
");break;
case 3:printf("Nice work!
");break;
default:printf("Keep up the good work!
");break;
}
}
void eval2(int a)
{
switch(a)
{
case 1:printf("No. Please try again.
");break;
case 2:printf("Wrong. Try once more.
");break;
case 3:printf("Don’t give up!
");break;
default:printf("Not correct. Keep trying.
");break;
}
}

大概做了下任务6,其它的自己实践一下吧

几道大一编程题,想知道错误的原因~有答案
答:第一题 s是一个char型数组,用在表达式中时也可以看成是数组第一个元素的地址。不能作为左值,也就是不能放在赋值符号"="的左边。可以把它理解成是不能修改的常量。所以D选项肯定是错的。p的类型是char*,C选项 p="ABCD";使得p指向存放在字符常量区中的"ABCD",当然没有问题。至于题主的疑问...

c语言编程问题!!
答:其他类似问题 2016-11-07 C语言编程问题 2018-01-09 C语言编程问题 2020-05-21 C语言编程问题? 41 2020-04-21 关于c语言编程问题! 36 2020-04-09 关于c语言编程问题! 33 2020-01-15 C语言编程的问题! 27 2019-05-27 C语言编程问题! 30 2011-10-08 C语言编程问题!!! 更多类似问题 > ...

怎么解决这道编程c++问题?
答:include <bits/stdc++.h> using namespace std;typedef struct { char name[20];int time;} stu;stu a[200000];bool cmp(stu a,stu b){ return a.time>b.time||a.time==b.time&&strcmp(a.name,b.name)>0;} int main(){ int n,i,j,h,m,h0,m0;scanf("%d%d:%d",&n,&h0,...

关于VB的几道编程题 谢谢帮忙·
答:1-4:在名称为Form1的窗体上建立二个名称分别为Cmd1、Cmd2,标题为“按钮一”、“按钮二”的命令按钮(如图1所示)。要求程序运行后,如果单击“按钮一”,则把“按钮二”移到“按钮一”上(如图2所示),使两个按钮重合。private sub cmd1_click()cmd2.move cmd1.left,cmd1.top,cmd1.width,...

VB的几道编程题,麻烦帮我解答一下
答:* 2 T1.FontSize = T1.FontSize * 3 End Sub 练习1-18:在Form1上面绘制一个Shape1,然后把他的Shape属性设为 3 - Circle ,接着把Shape1的Height和Width属性都设为1500,然后把窗体的Caption属性设为 圆 ,最后把窗体的MinButton属性和MaxButton属性都设为False.希望我的回答能帮助你解决问题 ...

请问大家这道C++编程题怎么做?求帮忙
答:先将输入的数保存到数组,然后遍历数字,判断相邻数字是否相同即可 使用一个变量记录当前是否处于数字连续的状态,用于输出中括号 一个小技巧是给数组多分配一个位置并将其置为-1,方便判断数组最后一个数 C++代码和运行结果如下:输出符合样例,望采纳~附源码链接:判断连续数字 ...

问一道C语言编程的问题,看下图
答:按你的提问,每天都吃一半多两个,第6天剩一个。程序按图片中的程序照猫画虎即可。计算结果,第一天摘了156个桃子,而不是100个。include <stdio.h>int main(){int day,x1,x2;day=5;x2=1;while(day>0){x1=(x2+2)*2;x2=x1;day--;}printf("total=%d\n",x1);return 0;} 用...

JavaScript的两道编程题有大佬会吗?感激不尽!
答:<!DOCTYPE html> Title 1 <!-- 1.JavaScript编程编程计算1+1/2+1/3+…+1/n的和。按下图页面效果,编程实现所需功能。(1)使用循环结构进行编程,形式不限;(2)通过表单中文本输入框输入数n,n不能为空或null;(3)表单中添加2个文本输入框,用于输入数n和显示累加和;添加...

问几道vb编程的问题,急急急急,今天(就是星期二)要知道,各位高人们能...
答:1. 答案是10939 Private Sub Command1_Click()i = 10000 Do While n < 100 i = i + 1 For j = 2 To i - 1 If i Mod j = 0 Then Exit For Next If j = i Then n = n + 1 Loop Print i End Sub 2. 答案是17422350 Private Sub Command1_Click()For i = 100 To ...

这道编程题目如何搞
答:针对上题,一个直观的方法是递归;但是,这里将提出一个新思路供参考:令(s,t)表示起点,(x,y)表示终点,(p,q)表示当前落子点。初始点(s,t)作为当前落子点(p,q)开始寻找路径,(1) 先检查当前落子点(p,q)是否落入(x,y)一个日格中。(2) 如果当前落子点(p,q)没有落入(x,y)的任何一...