1006. Output an integer in another format, and an integer in the 1006 format
1/* 2 * Main. c 3*1006. output an integer 4 * Created on: August 28, 2014 5 * Author: boomkeeper 6 ************ passed the test *********** 7 */8 9 # include <stdio. h> 10 11 int main (void) {12 13 int temp [4] = {0, 0, 0}; // each digit containing an integer 14 int in; 15 int I, j; 16 17 scanf ("% 3d", & in); 18 19 j = 0; 20 while (in! = 0) {21 temp [j] = in % 10; 22 j ++; 23 in/= 10; 24} 25 26 for (I = 0; I <temp [2]; I ++) 27 printf ("B"); 28 for (I = 0; I <temp [1]; I ++) 29 printf ("S"); 30 for (I = 0; I <temp [0]; I ++) 31 printf ("% I", I + 1 ); 32 printf ("\ n"); 33 34 return 0; 35}
Question link:
Http://pat.zju.edu.cn/contests/pat-b-practise/1006
Write a program: output an integer of 1100. Each row outputs 5 numbers. Run the program in the following format: 1 2 3 4 5 6 7 8 9 10
This is simple. Do it yourself ..
The general logic is as follows:
Algorithm 1: Use the while statement
Define the variable: int I;
I = 1;
Loop when (I is less than or equal to 100)
{
If (I <10)
{3 spaces are output;
Output I ;}
If (I> 10 and I <100)
{2 spaces are output;
Output I ;}
If (I = 100)
{Output 1 space;
Output I;
}
If (I % 5 = 0), the new line is output;
I increased by 1;
}
Check the code by yourself, just a few printf
What are C language conversion format characters?
% C characters
% D decimal integer
% E floating point number, in the e format % s string
% E floating point number, in the E format % u unsigned decimal integer
% F floating point number, % g output in decimal form % f and % e shorter
% O unsigned octal integers % G output % F and % E are shorter
% X unsigned hexadecimal integer, ranging from 0 ~ F indicates % X unsigned hexadecimal integer, ranging from 0 ~ F indicates
% L long integer, before d, o, u, x, X, for example, % ld (note that % l is an English letter l, not a number 1)