首页 > 代码库 > C语言代码编程题汇总:显示表达式1*2+3*4+...+99*100的表示形式(采取交互的形式)
C语言代码编程题汇总:显示表达式1*2+3*4+...+99*100的表示形式(采取交互的形式)
显示表达式1*2+3*4+...+99*100的表示形式(采取交互的形式)
程序源代码如下:
1 /* 2 2017年6月8日08:03:38 3 功能:采取交互的形式输出以下的表达式 4 */ 5 #include "stdio.h" 6 #include "string.h" 7 8 int main() 9 { 10 char seq[1000]; 11 int i, j = 0, first, second, num; 12 printf("please input a odd number: "); 13 scanf("%d",&num); 14 if(num <= 10) 15 { 16 for (i = 1; i <= num; i++) 17 { 18 if (i % 2 != 0) 19 seq[j++] = ‘0‘ + i,seq[j++] = ‘*‘; 20 else if(i % 2 == 0 && i != num) 21 seq[j++] = ‘0‘ + i,seq[j++] = ‘+‘; 22 else if (i == num) 23 seq[j++] = ‘0‘ + 1,seq[j++] = ‘0‘; 24 } 25 } 26 else if(num > 10 && num < 100) 27 { 28 for (i = 1; i <= num; i++) 29 { 30 if (i < 10) 31 if (i % 2 != 0) 32 seq[j++] = ‘0‘ + i,seq[j++] = ‘*‘; 33 else 34 seq[j++] = ‘0‘ + i,seq[j++] = ‘+‘; 35 else if (i >= 10 && i < num) 36 { 37 first = i % 10; 38 second = (i - first) / 10; 39 if (i % 2 != 0) 40 seq[j++] = ‘0‘ + second,seq[j++] = ‘0‘ + first,seq[j++] = ‘*‘; 41 else if (i % 2 == 0 && i != num) 42 seq[j++] = ‘0‘ + second,seq[j++] = ‘0‘ + first,seq[j++] = ‘+‘; 43 } 44 else if (i == num) 45 seq[j++] = ‘0‘ + second ,seq[j++] = ‘0‘ + (first + 1) ; 46 } 47 48 } 49 else if(num == 100) 50 { 51 for (i = 1; i <= 100; i++) 52 { 53 if (i < 10) 54 if (i % 2 != 0) 55 seq[j++] = ‘0‘ + i,seq[j++] = ‘*‘; 56 else 57 seq[j++] = ‘0‘ + i,seq[j++] = ‘+‘; 58 else if (i >= 10 && i < 100) 59 { 60 first = i % 10; 61 second = (i - first) / 10; 62 if (i % 2 != 0) 63 seq[j++] = ‘0‘ + second,seq[j++] = ‘0‘ + first,seq[j++] = ‘*‘; 64 else 65 seq[j++] = ‘0‘ + second,seq[j++] = ‘0‘ + first,seq[j++] = ‘+‘; 66 } 67 else if (i == 100) 68 seq[j++] = ‘0‘ + 1,seq[j++] = ‘0‘,seq[j++] = ‘0‘; 69 } 70 } 71 seq[j] = ‘\0‘; 72 puts(seq); 73 } 74 /* 75 总结: 76 在VC++6.0中显示的结果: 77 ----------------------------------------------------------------- 78 please input a odd number: 98 79 1*2+3*4+5*6+7*8+9*10+11*12+13*14+15*16+17*18+19*20+21*22+23*24+25*26+27*28+29*30 80 +31*32+33*34+35*36+37*38+39*40+41*42+43*44+45*46+47*48+49*50+51*52+53*54+55*56+5 81 7*58+59*60+61*62+63*64+65*66+67*68+69*70+71*72+73*74+75*76+77*78+79*80+81*82+83* 82 84+85*86+87*88+89*90+91*92+93*94+95*96+97*98 83 84 please input a odd number: 100 85 1*2+3*4+5*6+7*8+9*10+11*12+13*14+15*16+17*18+19*20+21*22+23*24+25*26+27*28+29*30 86 +31*32+33*34+35*36+37*38+39*40+41*42+43*44+45*46+47*48+49*50+51*52+53*54+55*56+5 87 7*58+59*60+61*62+63*64+65*66+67*68+69*70+71*72+73*74+75*76+77*78+79*80+81*82+83* 88 84+85*86+87*88+89*90+91*92+93*94+95*96+97*98+99*100 89 ----------------------------------------------------------------- 90 注意:将整数型数据转化成字符型数据的格式 91 char m; 92 int n; 93 m = n + ‘0‘;(像10、100之类的数据除外) 94 */
C语言代码编程题汇总:显示表达式1*2+3*4+...+99*100的表示形式(采取交互的形式)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。