C Language exercises (1)

Source: Internet
Author: User

1. Programming to encrypt English sentences entered by the keyboard. The encryption method is as follows: when the content is English letters, the last three letters in the 26 letters are

Replace the letter. If it is another character, it remains unchanged.
Int _ tmain (INT argc, _ tchar * argv [])
{
Printf ("Please input your words:/N ");
Char STR [100];
Char C;
Gets (STR );
For (INT I = 0; I <100; I ++)
{
C = STR [I];
If (C> = 'A' & C <= 'Z ')
Putchar (c-'A' + 3) % 26 + 'A ');
Else if (C> = 'A' & C <= 'Z ')
Putchar (c-'A' + 3) % 26 + 'A ');
}

Return 0;
}
2. Programming to convert any decimal integer to the R-base number (R is between 2 and 16 ).
Int _ tmain (INT argc, _ tchar * argv [])
{
Int n, m;
Int I = 0;
Int temp [100];
Printf ("Please input your number:/N ");
Scanf ("% d", & N );
Printf ("Please input your base number:/N ");
Scanf ("% d", & M );
While (n! = 0)
{
Temp [I] = n % m;
N = N/m;
I ++;
}
While (I --! = 0)
Printf ("% d", temp [I]);
Return 0;
}
3. Enter a specified amount (in Yuan, for example, 345.78) on the keyboard, and then display the number of different denominations paid for this amount in RMB. 100 yuan is required.

50 RMB, 10 RMB, 5 RMB, 2 RMB, 1 RMB, 1 cent, 5 minutes, and 1 cent.
Int type [9] = {1000,500,200,100, 5, 1 };
Printf ("Please input your money:/N ");
Float N;
Scanf ("% F", & N );
Int I = 0;
Float M = N * 100;
Int number = (INT) m;
Printf ("% d", number );
While (number! = 0)
{
Printf ("% d", number );
Printf ("the Count of % F is % d/N", (float) type [I])/100, number/type [I]);
Number % = type [I ++];

}
Return 0;
The problem is: When I replace float M = N * 100; int number = (INT) m; with int number = (INT) (N * 100, why 21.4

Is it 2139, not 2140? That is, it changes 21.4 to 21.39, and the difference is 0.01?
4. A Random Number of 20 [10, 50] integers are generated and stored in the array. The maximum, minimum, average, and sum of all elements in the array are obtained.
# Include <time. h>
# Include <stdlib. h>

Int _ tmain (INT argc, _ tchar * argv [])
{
Int A [20];
Int I, min = 0, max = 0, sum = 0;
Srand (INT) time (0 ));
For (I = 0; I <20; I ++)
{
A [I] = rand () % 41 + 10;
Sum + = A [I];
If (min> A [I]) min = A [I];
If (max <A [I]) max = A [I];
}
Printf ("min = % d max = % d sum = % d Ave = % F", Min, Max, sum, float (SUM)/20 );

Return 0;
}
5. Programming in a known string to find the longest word, assuming that the string contains only letters and spaces, spaces are used to separate different words.
# Include <stdlib. h>

Int _ tmain (INT argc, _ tchar * argv [])
{
Char STR [100], save [100];
Int start = 0, max = 0, heigh = 0;
Int I = 0;
Printf ("Please input your numbers:/N ");
Gets (STR );
While (STR [I]! = '/0 ')
{
If (STR [I] = '')
{
If (max {
Max = heigh;
Start = I-Heigh;
}
Heweigh = 0;
}
Else
Heweigh ++;
I ++;
}
If (max <= heigh)
Max = heigh; // easy to forget, never mind
Printf ("max = % d", max );
While (max --> 0)
{
Printf ("% C", STR [start ++]);
}
Return 0;
}
6. Any cube of a natural number M can be written into the sum of M consecutive odd numbers. For example:
1 ^ 3 = 1
2 ^ 3 = 3 + 5
3 ^ 3 = 7 + 9 + 11
4 ^ 3 = 13 + 15 + 17 + 19
# Include <stdlib. h>

Int _ tmain (INT argc, _ tchar * argv [])
{
Int N, J;
Int sum = 0, start = 0;
Scanf ("% d", & N );
For (j = 0; j <n; j ++)
Sum + = J;
Start = sum * 2 + 1;
For (j = 0; j <n; j ++)
Printf ("% d", start + J * 2 );
Return 0;
}
7. ABC + CBA = 1333, where A, B, and C are all single digits. All combinations of A, B, and C that meet the conditions are obtained by programming.
# Include <stdlib. h>

Int _ tmain (INT argc, _ tchar * argv [])
{
Int A, B, C;
For (A = 4; A <9; A ++)
{
C = 13-A;
If (C + 1*10 + A * 100 + A + 1*10 + C * 100) = 1333)
Printf ("A = % d, B = 1, C = % d/N", a, c );
}
Return 0;
}
8. Compile a program to complete the four arithmetic operations of two numbers. For example, if you enter 34 + 56, the output result is 90.00. It is required that the calculation result be retained with two decimal places. When a user inputs two numbers and operators at a time.
# Include <stdlib. h>

Int _ tmain (INT argc, _ tchar * argv [])
{
Float a, B;
Char mark;
Scanf ("% F % C % F", & A, & mark, & B );
Switch (mark)
{
Case '+': printf ("%. 2f", A + B); break;
Case '-': printf ("%. 2f", a-B); break;
Case '*': printf ("%. 2f", a * B); break;
Case '/': printf ("%. 2f", a/B); break;
}
Return 0;
}
9. jesus had 13 disciples, and one of them was a traitor who sold Jesus. Please use division to find the traitor: 13 people sat around and started from the first to report: 1, 2, 3, 1, 2, 3 ......, Anyone who reports to "3" will leave the circle. The last person in the circle is the traitor who sold Jesus. Please find out its original serial number.
Int _ tmain (INT argc, _ tchar * argv [])
{
Int people [14] = {0 };
Int I, j, now = 1;
For (I = 1; I <13; I ++)
{
J = 1;
While (J! = 3 ){
If (People [now] = 0)
J ++;
If (now> 13)
Now = 1;
Else
Now ++;
}
People [now] = 1;
Printf ("% d", now );
J = 0;
}
Return 0;
}
Printf ("A = % d/N", a);/* the output result is a decimal integer a = 1234 */

Printf ("A = % 6D/N", a);/* output 6-digit decimal number A = 1234 */

Printf ("A = % 06d/N", a);/* output 6-digit decimal number A = 001234 */

Printf ("A = % 2D/N", a);/* A exceeds 2 bits and Outputs A = 1234 */

Printf ("* I = % 4D/N", * I);/* output a four-digit decimal integer * I = 12 */

Printf ("* I = %-4D/N", * I);/* the output is left aligned with a four-digit decimal integer * I = 12 */

Printf ("I = % P/N", I);/* output address I = 06e4 */

Printf ("F = % F/N", f);/* output floating point number f = 3.141593 */

Printf ("F = 6.4f/N", f);/* output 6 floating point numbers with four decimal places

F = 3.1416 */

Printf ("x = % lf/N", x);/* output long floating point number x = 0.123457 */

Printf ("x = % 18.16lf/N", x);/* output the length floating point number x = 0.1234567898765432 */

Printf ("c = % C/N", c);/* output character c = */

Printf ("c = % x/N", c);/* the ASCII code value of the output character c = 41 */

Printf ("s [] = % s/n", S);/* output array string s [] = Hello, Comrade */

Printf ("s [] = % 6.9 S/N", S);/* output string s [] = Hello, Co */

Printf ("s = % P/N", S);/* output array string first character address S = ffbe */

Printf ("* P = % s/n", P);/* output pointer string P = How Do You do */

Printf ("P = % P/N", P);/* output pointer value p = 0194 */

 

 

 

All programs run successfully in vs2005, and all programs do not include the # include "stdafx. H" header file. This exercise is due to too little programming. Array

It does not write off overflow because it saves time.
I hope that the person who reads the question should give at least a few comments. It is better to simplify the program. If it is simpler than my program, please write it out. My code quality is not high, so I must give some comments. Look down on the small program, it was originally compiled but a pair of pants problems...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.