//
// main.c
// 10-循环的练习
//
// Created by apple on 14-6-14.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#include <stdio.h>
int main(int argc, const char * argv[])
{
/*
求S(n) = a+aa+aaa+aaaa+...+aa..a之值,其中a是一个数字,n表示a的位数例如:2+22+222+2222+22222(此时n=5),n和a都从键盘输入。
2+
22+ 2 * 10 + 2
222+ 2 * 10(2) + 2 * 10 + 2
2222+
22222
*/
int result = 0;
for (int i = 0; i < 5; i++) {
int xiShu = 1;
int item = 0;
for (int j = 0; j <= i; j++) {
//printf("2");
if (j == 0) {
xiShu = 1;
}else{
xiShu = xiShu * 10;
}
item = item + 2 * xiShu;
}
printf("item = %d\n",item);
result = result + item;
//printf("\n");
}
printf("result = %d\n",result);
return 0;
}
void test10()
{
/*
求S(n) = a+aa+aaa+aaaa+...+aa..a之值,其中a是一个数字,n表示a的位数例如:2+22+222+2222+22222(此时n=5),n和a都从键盘输入。
0
2+ 2
22+ 2 * 10 + 2
222+ 2 * 10(2) + 2 * 10 + 2
2222+ 2 * 10(3) + 2 * 10(2) + 2 * 10 + 2
22222 2 * 10(4) + 2 * 10(3) + 2 * 10(2) + 2 * 10 + 2
0
10
100
1000
10000
*/
int xiShu = 1;
int item = 0;
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 0) {
xiShu = 1;
}else{
xiShu = xiShu * 10;
}
item = 2 * xiShu + item;
result = result + item;
printf("result = %d\n",result);
}
}
void test9()
{
/*
使用循环打印三角形
---* //i == 0 1 i * 2 + 1
--*** //i == 1 3
-***** //i == 2 5
******* //i == 3 7
*/
for (int i = 0 ;i < 4 ; i++) {
for (int j = i + 1 ; j < 4; j++) {
printf("-");
}
for (int j = 0; j < 2 * i + 1; j++) {
printf("*");
}
printf("\n");
}
}
void test1()
{
int a = 4;
int b = 8;
int start = a > b ? b : a;
while (start != 0 ) {
if (a % start == 0 && b % start == 0) {
printf("%d,%d的最大公约数%d\n",a,b,start);
break;
}
start--;
}
}
void test()
{
int a = 4;
int b = 6;
for (int i = 1;;i++) {
if(i % a == 0 && i % b == 0 ){
printf("%d,%d的最小公倍数%d\n",a,b,i);
break;
}
}
}
|