C言語古典プログラミング例(一)
65354 ワード
文書ディレクトリ一、C言語基礎 1.印刷素数 2.乗算口诀表 を印刷する 3.判断閏年 4.両数交換 5.最大公約数GCD 6.配列コンテンツ交換 7.1/1-1/2+1/3-1/4+1/5......+1/99-1/100の値 を計算 8.1~100のすべての整数の中で何回の数字9 が現れます 9.印刷菱形 10.水仙数 C++言語を使用してC言語のよくある重点、要点の問題を解決する
一、C言語の基礎
1.印刷素数
素数(素数):1より大きい自然数のうち1とそれ自体を除いて他の因数がない数を素数と呼び,残りを合数と呼ぶ
2.乗算口裏表の印刷
3.判断閏年
任意の年が閏年であるか否かを判断するには、以下の条件のいずれかを満たす必要がある. ②この年は400で除かれる
4.りょうすうこうかん
5.最大公約数GCD
自然数aが自然数bで除去される場合、aをbの倍数、bをaの約数と呼ぶ.いくつかの自然数の公有の約数を、このいくつかの自然数の公約数と呼ぶ.公約数の中で最大の公約数で、これらの自然数の最大公約数と呼ばれています.
6.配列コンテンツ交換
7.1/1-1/2+1/3-1/4+1/5......+1/99-1/100の値を計算
8.1~100のすべての整数に何回の数字が現れるか9
各整数の数字を取り出して判断する
9.ひし形印刷
10.水仙の数
「水仙の花の数」とは、その各数字の3桁とそれに等しい数そのものを指し、例えば153は「水仙の花の数」であり、153=13+53+33であるため、1つの数が「水仙の花の数」であるか否かを判断し、最も重要なのは与えられた3桁の個位、10位、百位をそれぞれ分割し、その立方和(sとする)を求めることである.sが与えられた3桁と等しく,3桁が「水仙花数」であり,逆にそうではない.
main関数とそのヘッダファイル
一、C言語の基礎
1.印刷素数
素数(素数):1より大きい自然数のうち1とそれ自体を除いて他の因数がない数を素数と呼び,残りを合数と呼ぶ
////////////////////////////////////////////////////////
//1.
void PrintPrime()
{
int n1 = 0;
int n2 = 0;
cout << "Please input two numbers: ";
cin >> n1 >> n2;
int flag = 0;
//① n , n 2 n-1
// n ,
for (int i = n1; i <= n2; i++)
{
bool IsNoPrime = false;
int data = i;
for (int j = 2; j < data; j++)
{
if ((data % j) == 0)
{
IsNoPrime = true;
break;
}
}
if (!IsNoPrime)
{
cout << data << ' ';
if (flag++ == 10)
{
cout << endl;
flag = 0;
}
}
}
cout << endl << endl;
//② : n 2~n-1 , √n,
// √n,
flag = 0;
for (int i = n1; i <= n2; i++)
{
bool IsNoPrime = false;
int data = i;
int k = (int)sqrt((double)data);//sqrt double
for (int j = 2; j <= k; j++)
{
if ((data % j) == 0)
{
IsNoPrime = true;
break;
}
}
if (!IsNoPrime)
{
cout << data << ' ';
if (flag++ == 10)
{
cout << endl;
flag = 0;
}
}
}
cout << endl;
}
2.乗算口裏表の印刷
////////////////////////////////////////////////////////
//2.
void PrintMultiplicationFormulatable()
{
//
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
if (j < i)
cout << " ";
else
{
printf("%d*%d=%2d ", i, j, i * j);
}
}
cout << endl;
}
cout << endl << endl;
//
for (int i = 1; i <= 9; i++)
{
for (int j = i; j <= 9; j++)
{
printf("%d*%d=%2d ", i, j, i * j);
}
cout << endl;
}
cout << endl << endl;
//
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9 - i; j++)
{
cout << " ";
}
for (int j = 1; j <= i; j++)
{
printf("%d*%d=%2d ", i, j, i * j);
}
cout << endl;
}
cout << endl << endl;
//
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d*%d=%2d ", i, j, i * j);
}
cout << endl;
}
cout << endl << endl;
}
3.判断閏年
任意の年が閏年であるか否かを判断するには、以下の条件のいずれかを満たす必要がある. ②この年は400で除かれる
////////////////////////////////////////////////////////
//3.
void IsLeapYear()
{
int year = 0;
cout << "Please input year:";
cin >> year;
assert(year > 0);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
cout << "Is Leap Year!" << endl;
}
else
{
cout << "Is Not Leap Year!" << endl;
}
}
4.りょうすうこうかん
////////////////////////////////////////////////////////
//4.
void Swap()
{
int n1 = 0;
int n2 = 0;
cout << "Please input two numbers: ";
cin >> n1 >> n2;
cout << "n1: " << n1 << " " << "n2: " << n2 << endl;
//
int tmp = n1;
n1 = n2;
n2 = tmp;
cout << "n1: " << n1 << " " << "n2: " << n2 << endl;
// --
n1 += n2;
n2 = n1 - n2;
n1 = n1 - n2;
cout << "n1: " << n1 << " " << "n2: " << n2 << endl;
//
n1 = n1 ^ n2;
n2 = n2 ^ n1;
n1 = n1 ^ n2;
cout << "n1: " << n1 << " " << "n2: " << n2 << endl;
// -- (2 :-32767-32767)
n1 <<= 16;
n1 += n2;
n2 = n1 >> 16;
n1 = n1 & 0xffff;
cout << "n1: " << n1 << " " << "n2: " << n2 << endl;
}
5.最大公約数GCD
自然数aが自然数bで除去される場合、aをbの倍数、bをaの約数と呼ぶ.いくつかの自然数の公有の約数を、このいくつかの自然数の公約数と呼ぶ.公約数の中で最大の公約数で、これらの自然数の最大公約数と呼ばれています.
////////////////////////////////////////////////////////
//5. GCD
void FindGCD()
{
unsigned int n1 = 0;
unsigned int n2 = 0;
cout << "Please input two numbers: ";
cin >> n1 >> n2;
unsigned int max = n1;
unsigned int min = n2;
if (n1 < n2)
{
max = n2;
min = n1;
}
// --
vector<int> num;
for (int i = 1;i <= min; i++)
{
if ((n1 % i == 0) && (n2 % i == 0))
{
num.push_back(i);
}
}
cout << "The GCD Is: " << num[num.size() - 1] << endl;
// ( ) ( 1)
//
for (int i = min; i > 0; i--)
{
if ((n1 % i == 0) && (n2 % i == 0))
{
cout << "The GCD Is: " << i << endl;
break;
}
}
}
6.配列コンテンツ交換
////////////////////////////////////////////////////////
//6.
//
//
void SwapNum(int* pa, int* pb)
{
*pa ^= *pb;
*pb ^= *pa;
*pa ^= *pb;
}
void SwapElements()
{
vector<int> arr1, arr2;
cout << "Please Input Arr1 Elements:";
int num = 0;
while (cin >> num)
{
arr1.push_back(num);
if (arr1.size() > N - 1)
break;
cout << "Please Input Arr1 Elements:";
}
cout << endl << endl;
cout << "Please Input Arr2 Elements:";
while (cin >> num)
{
arr2.push_back(num);
if (arr2.size() > N - 1)
break;
cout << "Please Input Arr2 Elements:";
}
cout << endl << endl;
cout << "The Arr1 Is: ";
for (int i = 0; i < arr1.size(); i++)
{
cout << arr1[i] << ' ';
}
cout << endl << endl;
cout << "The Arr2 Is:";
for (int i = 0; i < arr2.size(); i++)
{
cout << arr2[i] << ' ';
}
cout << endl << endl;
for (int i = 0; i < arr1.size(); i++)
{
SwapNum(&arr1[i], &arr2[i]);
}
cout << endl << endl;
cout << "The Arr1 Is: ";
for (int i = 0; i < arr1.size(); i++)
{
cout << arr1[i] << ' ';
}
cout << endl << endl;
cout << "The Arr2 Is: ";
for (int i = 0; i < arr2.size(); i++)
{
cout << arr2[i] << ' ';
}
cout << endl << endl;
}
7.1/1-1/2+1/3-1/4+1/5......+1/99-1/100の値を計算
////////////////////////////////////////////////////////
//7. 1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100
void SumofFraction()
{
double sum = 0.0;
for (int i = 1; i <= 100; i++)
{
sum += pow(-1, i + 1) / i;
}
cout << "The Sum of '1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100' Is: " << sum << endl;
}
8.1~100のすべての整数に何回の数字が現れるか9
各整数の数字を取り出して判断する
////////////////////////////////////////////////////////
//8.1~100 9
void NineNumber()
{
int cou = 0;
int i = 0;
for (i = 1; i <= 100; i++)
{
int tmp = 1;
int nu = i;
while (nu > 0)
{
tmp = nu % 10;
if ((tmp / 9) == 1)
cou++;
nu = nu / 10;
}
}
cout << "The number of 9 is: " << cou << endl;
}
9.ひし形印刷
////////////////////////////////////////////////////////
//9.
void PrintRhombic()
{
//①
int line = 0;// ==
cout << "Please input a Odd: ";
cin >> line;
int spacenumber = line / 2;//
int starnumber = 1;//
for (int i = 0; i < line; i++)
{
//
if (i <= line / 2)
{
for (int j = 0; j < spacenumber; j++)
{
cout << ' ';
}
for (int j = 0; j < starnumber; j++)
{
cout << '*';
}
cout << endl;
if (i == line / 2)
{
continue;
}
spacenumber--;
starnumber += 2;
}
else//
{
spacenumber++;
starnumber -= 2;
for (int j = 0; j < spacenumber; j++)
{
cout << ' ';
}
for (int j = 0; j < starnumber; j++)
{
cout << '*';
}
cout << endl;
}
}
//② , line==colnmn
// ( ), i,j 1
//j>=(column+1)/2-(i + 1) (column+1)/2-(i + 1) i
//j<=(column+1)/2+(i + 1) (column+1)/2+(i + 1) i
// ,
//j>=(column+1)/2-(line-i) (column+1)/2-(line-i) i
//j<=(column+1)/2+(line-i) (column+1)/2+(line-i) i
int colnmn = line;// ==
for (int i = 1; i <= line; i++)
{
//
if (i <= (line / 2 + 1))
{
for (int j = 1; j <= colnmn; j++)
{
if (((colnmn + 1) / 2) - (i - 1) <= j && j <= ((colnmn + 1) / 2) + (i - 1))
{
cout << '*';
}
else
{
cout << ' ';
}
}
cout << endl;
}
else//
{
for (int j = 1; j <= colnmn; j++)
{
if (((colnmn + 1) / 2) - (line - i) <= j && j <= ((colnmn + 1) / 2) + (line - i))
{
cout << '*';
}
else
{
cout << ' ';
}
}
cout << endl;
}
}
}
10.水仙の数
「水仙の花の数」とは、その各数字の3桁とそれに等しい数そのものを指し、例えば153は「水仙の花の数」であり、153=13+53+33であるため、1つの数が「水仙の花の数」であるか否かを判断し、最も重要なのは与えられた3桁の個位、10位、百位をそれぞれ分割し、その立方和(sとする)を求めることである.sが与えられた3桁と等しく,3桁が「水仙花数」であり,逆にそうではない.
////////////////////////////////////////////////////////
//10. 100~999 “ ”
void FindNarcissisticnumber()
{
int bit = 0;
int top = 0;
int hundred = 0;
cout << "Result Is: ";
for (int i = 153; i < 1000; i++)
{
int sum = 0;
int n = i;
bit = n % 10;
n = n / 10;
top = n % 10;
hundred = n / 10;
sum = pow(bit, 3) + pow(top, 3) + pow(hundred, 3);
if (sum == i)
{
cout << i << ' ';
}
}
cout << endl;
}
main関数とそのヘッダファイル
#include
#include
#include
#define N 5
using namespace::std;
int main()
{
////1.
//PrintPrime();
////2.
//PrintMultiplicationFormulatable();
////3.
//IsLeapYear();
////4.
//Swap();
////5. GCD
//FindGCD();
////6.
//SwapElements();
////7.
//SumofFraction();
////8.
//NineNumber();
////9.
//PrintRhombic();
////10.
FindNarcissisticnumber();
system("pause");
return 0;
}