2015.1.23
//Loop (calculates the number of digits) Enter a positive integer to calculate the number of digits of the positive integer
int main (int argc, const char * argv[]) {
int n;
int cnt=0;
scanf ("%d", &n);
while (n) {
cnt++;
n/=10;
// }
printf ("cnt =%d\n", CNT);
//
return 0;
//}
A=1 b=1
a+b/a 2/1 A = 2 b = 1
a+b/a 3/2 A = 3 B = 2
a+b/a 5/3 A = 5 B = 3
//...
9 Flow control (numeric sequence 2) output digital sequence 2/1,3/2,5/3,8/5,13/8,21/13 ..., the number of outputs is entered by the keyboard. Note the input uses scanf input Feibonaqi
//\
Like what:
//
Input 3 output to
//
2/1
3/2
5/3
//
Input 4 output to
2/1
3/2
5/3
8/5
//\
int main (int argc,const char *argv[])
//{
int n;
int a=1,b=1;
scanf ("%d", &n);
for (int i=0; i<n; i++) {
int temp;
printf ("%d/%d", a+b,a);
temp = a+b;
b = A;
A = temp;
// }
printf ("\ n");
return 0;
//}
Exchange values for two two variables
int main (int argc,const char *argv[])
//{
int a=3, b=5;
// /*
int tmp = A;
A = b;//a = 5;
b = tmp;//b=3;
printf ("A =%d b=%d\n", A, b);
// */
The value of a, B is not exchanged with an intermediate variable
A=a+b;
B=a-b;
A=a-b;
printf ("A =%d b=%d\n", A, b);
return 0;
//}
Process Control (numeric sequence 3) output digital sequence 1,2,3,5,8,13,21 ..., the number of outputs is entered by the keyboard. Note Input using scanf input
//\
Like what:
//\
Input 3 output to
//
1
2
3
//
Input 4 output to
1
2
3
5
/*int Main (int argc,const char *argv[])
{
int n, a=0,b=1;
scanf ("%d", &n);
for (int i=0; i<n; i++) {
printf ("%d", a+b);
int tmp = A+B;
A = b;
b = tmp;
}
return 0;
}*/
Beg Greatest common divisor
Euclidean method
A b a%b
319 377---> 319
377 319---> 58
319---> 29
---> 0
29 0
Control + I
int main (int argc, const char *argv[])
//{
int A, B;
scanf ("%d%d", &a, &b);
while (b) {
int tmp = A%B;
A = b;
b = tmp;
// }
printf ("%d\n", a);
return 0;
//}
Process Control (for factorial and) input n (int type), print 1! +2! +3! +4! +5! + ... The value of the +n!
Like what:
//
Input: 2
Output:
3
//
Input: 3
//
Output:
9
int main (int argc,const char *argv[])
//{
int Sn = 0;
int n;
int temp=1;
scanf ("%d", &n);
for (int i=1; i<=n; i++) {
temp = i*temp;//n!
Sn + = temp;
// }
printf ("SN =%d\n", SN);
//
return 0;
//}
decomposes a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5.
n i n%i
90/2---> 0
45/2---> 1
45/3---> 0
15/3---> 0
5/3---> 2
5/4---> 1
5/5---> 0
1
int main (int argc, const char *argv[])
//{
int n;
scanf ("%d", &n);
printf ("%d=", N);
for (int i=2; n!=1; i++) {
if (! ( n%i)) {
n!=i?printf ("%d*", i):p rintf ("%d", I);
N/=i;
i--;
// }
// }
printf ("\ n");
return 0;
//}
19. Process Control (for loop) Enter an uppercase letter, such as F
//\
Like what:
//\
Input: F
//\
Output:
//
A
ABA
ABCBA
Abcdcba
Abcdedcba
Abcdefedcba
/*int Main (int argc,const char *argv[])
{
Char ch;
scanf ("%c", &ch);
for (int i=0; i<ch-' A ' +1; i++) {
for (int j=0; j<ch-i-' A '; j + +) {
printf ("");
}
for (int j=0; j<=i; J + +) {
printf ("%c", ' A ' +j);
}
for (int j=i; j>0; j--) {
printf ("%c", ' A ' +j-1);
}
printf ("\ n");
}
return 0;
}
*/
21st. Process Control (for loop) Enter an uppercase character, such as F
//\
Like what:
//
Input: F
//
Output:
//
Fedcba
Edcbab
Dcbabc
Cbabcd
Babcde
ABCDEF
/*int Main (int argc,const char *argv[])
{
Char ch;
scanf ("%c", &ch);
for (int i=0; i<ch-' A ' +1; i++) {
for (int j=0; j<i; J + +) {
printf ("");
}
for (int j=i; j<ch-' A ' +1; j + +) {
printf ("%c", ch-j);
}
for (int j=0; j<i; J + +) {
printf ("%c", ' A ' +j+1);
}
printf ("\ n");
}
return 0;
}*/
Enter an uppercase character, such as F
Like what:
Input: F
Output:
Fedcba
Edcbab
Dcbabc
Cbabcd
Babcde
ABCDEF
//
Input B
Output:
BA
Ab
int main (int argc,const char *argv[])
//{
Char ch;
scanf ("%c", &ch);
for (int i=0; i<ch-' A ' +1; i++) {
for (int j=i; j<ch-' A ' +1; j + +) {
printf ("%c", ch-j);
// }
for (int j=0; j<i; J + +) {
printf ("%c", ' A ' +1+j);
// }
//
printf ("\ n");
// }
return 0;
//}
Enter an uppercase letter, such as F
Like what:
Input: F
Output:
A
ABA
ABCBA
Abcdcba
Abcdedcba
Abcdefedcba
//
Input: C
Output:
A
ABA
ABCBA
/*int Main (int argc, const char *argv[])
{
Char ch;
scanf ("%c", &ch);
for (int i=0; i<ch-' A ' +1; i++) {
for (int j=0; j<=i; J + +) {
printf ("%c", ' A ' +j);
}
for (int j=i; j>0; j--) {
printf ("%c", ' A ' +j-1);
}
printf ("\ n");
}
return 0;
}*/
Enter month day, judge how many days of the year
2015 1 31//31
2015 3 1//31 + 28 + 1 = 60
2012 3 1//31 + 29 + 1 = 61
int main (int argc,const char *argv[])
{
int year, month, day;
int total=0;
scanf ("%d%d%d", &year, &month, &day);
for (int i=1; i<month; i++) {
switch (i) {
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
total+=31;
Break
Case 2:
if ((!) ( year%4) && year%100) | | ! (year%400)) {
total+=29;
}
Else
{
total+=28;
}
Break
Case 4:
Case 6:
Case 9:
Case 11:
total+=30;
Break
Default
Break
}
}
Total+=day;
printf ("Days%d\n", total);
return 0;
}
ios-c_day5___ Cycle Practice