Article Directory
1. List the multiplication formula table
[Sample code]
#include<iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
cout << j << " * " << i << " = " << i * j;
cout << " ";
}
cout << endl;
}
system("pause");
return 0;
}
[Reference results]
2. Find the maximum
Enter a few numbers by yourself, when the input 0 is the end, the maximum value will be output
[Sample code]
#include<iostream>
using namespace std;
int main()
{
int num, max;
cin >> num;
max = num;
while (1)
{
if (num == 0)
break;
cin >> num;
if (num > max)
max = num;
}
cout << "最大值为:" << max << endl;
system("pause");
return 0;
}
3. Judgment prime
Input a number num, judge whether num is a prime number, if it is, the output num is a prime number, otherwise the output num is not a prime number
[Sample code]
#include<iostream>
using namespace std;
int main()
{
int num, flag = 1;
cin >> num;
for (int i = 2; i < num; i++)
{
if (num%i == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
cout << num << "是素数" << endl;
else
cout << num << "不是素数" << endl;
system("pause");
return 0;
}
4. Summation of Sequences
Enter a number n, calculate the sum of the first n items: 1+1/4+1/7+1/10+1/(3*n-2)
[Sample code]
#include<iostream>
using namespace std;
int main()
{
double i, n;
double sum = 1;
cout << "请输入:";
cin >> n;
for (i = 2; i <= n; i++)
{
sum = sum + 1 / (3 * i - 2);
}
cout << "结果为:" << sum << endl;
system("pause");
return 0;
}
[Reference results]
5. Watermelon sold for a few days
For n watermelons, sell half and two more on the first day, and then sell the remaining half and two more every day, and ask how many days later they can be sold out
(when the number of watermelons is odd, sell half as an integer, for example, the number of watermelons is At 3 o'clock, half of the sale will be sold 1)
[Sample code]
#include<iostream>
using namespace std;
int main()
{
int n, day;
cout << "请输入西瓜总数:";
cin >> n;
n = n - n / 2 - 2;
day = 1;
while (n>0)
{
n = n - n / 2 - 2;
day++;
}
cout << day << "天以后能卖完" << endl;
system("pause");
return 0;
}
[Reference results]
6. Judgment of results
Enter the score of n students, if the score is less than 60, output "fail", otherwise output "pass"
[Sample code]
#include<iostream>
using namespace std;
int main()
{
int n, score;
cout << "请输入学生数n:";
cin >> n;
while (n)
{
cout << "请输入学生成绩:";
cin >> score;
if (score < 60)
cout << "Fail" << endl;
else
cout << "Pass" << endl;
n--;
}
system("pause");
return 0;
}
[Reference results]
7. Greatest common divisor
Enter two positive integers m and n, and find their greatest common divisor
[Sample code]
#include<iostream>
using namespace std;
int main()
{
int m, n, i,max,num;
cout << "n = ";
cin >> n;
cout << "m = ";
cin >> m;
num = n;
if (m < n)
num = m;
max = 1;
for (i = 2; i <= num; i++)
{
if ((n%i == 0) && (m%i == 0))
max = i;
}
cout << "最大公约数为:" << max << endl;
system("pause");
return 0;
}
[Reference results]