已过OJ,仅作参考(代码不一定是最优的,可能存在错误)
NOJ练习一
1、计算A+B
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int a,b; 7 cin >> a >> b; 8 cout << a + b; 9 10 return 0; 11 }
2、面积及体积的计算
1 #include <iostream> 2 using namespace std; 3 4 const double PI = 3.1415926; 5 int main() 6 { 7 double r, h; 8 cin >> r >> h; 9 printf("%.2f\n", 2*PI*r); 10 printf("%.2f\n", PI*r*r); 11 printf("%.2f\n", 4*PI*r*r); 12 printf("%.2f\n", 4.0/3*PI*r*r*r); 13 printf("%.2f\n", PI*r*r*h); 14 return 0; 15 }
3、总成绩和平均成绩
#include <iostream> using namespace std; int main() { double m,l,e; cin >> m >> l >> e; printf("%.6f\n", m+l+e); printf("%.6f\n", (m+l+e)/3); return 0; }
NOJ练习二
1、找最大数
#include <cstdio> int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); int max=a; if(b>max) max=b; if(c>max) max=c; printf( "%d\n", max ); return 0; }
2、发奖金
1 #include <cstdio> 2 3 int main() 4 { 5 double i,b; 6 scanf("%lf",&i); 7 8 if(i<=10){ 9 b=i*0.1; 10 } 11 else if(i<20){ 12 b=1+(i-10)*0.075; 13 } 14 else if(i<40){ 15 b=1.75+(i-20)*0.05; 16 } 17 else if(i<60){ 18 b=2.75+(i-40)*0.03; 19 } 20 else if(i<100){ 21 b=3.35+(i-60)*0.015; 22 } 23 else{ 24 b=3.95+(i-100)*0.01; 25 } 26 27 printf( "%f\n", b ); 28 29 return 0; 30 }
3、第几天
1 #include <cstdio> 2 3 int main() 4 { 5 int y,m,d,t=0; 6 scanf("%d-%d-%d",&y,&m,&d); 7 8 int day[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 9 if(y%4==0&&y%10!=0||y%400==0) 10 day[2]++; 11 12 for(int i=1; i<m; ++i) 13 t += day[i]; 14 t+=d; 15 16 printf("%d\n",t); 17 18 return 0; 19 }
4、成绩转换
1 #include <cstdio> 2 3 int main() 4 { 5 int score,ch; 6 scanf("%d",&score); 7 8 switch(score/10){ 9 case 10: 10 case 9: 11 ch='A';break; 12 case 8: 13 ch='B';break; 14 case 7: 15 ch='C';break; 16 case 6: 17 ch='D';break; 18 default: 19 ch='E'; 20 } 21 22 printf("%c\n",ch); 23 24 return 0; 25 }
5、出租车费
1 #include <cstdio> 2 3 int main() 4 { 5 double d,m; 6 scanf("%lf",&d); 7 if(d<=2){ 8 m=7; 9 } 10 else if(d<=15){ 11 if(d-2==(int)(d-2)) 12 m=7+(d-2)*1.5; 13 else 14 m=7+((int)(d-2)+1)*1.5; 15 } 16 else if(d-15==(int)(d-15)){ 17 m=26.5+(d-15)*2.1; 18 } 19 else 20 m=26.5+((int)(d-15)+1)*2.1; 21 22 printf("%f\n",m); 23 24 return 0; 25 }
NOJ练习三
1、完数
1 #include <stdio.h> 2 3 #define N 100 4 int main() 5 { 6 int n; 7 scanf("%d",&n); 8 9 for(int k=1; k<=n;++k ) 10 { 11 int a[100] = {0},cnt = 0; 12 int sum = 0; 13 for(int i=1;i<k;++i){ 14 if(k%i==0){ 15 sum += i; 16 a[cnt++] = i; //因子 17 } 18 } 19 if(sum==k){ 20 printf("%d=",sum); 21 for(int j=0;j<cnt;++j){ 22 if(j!=0){ 23 printf("+"); 24 } 25 printf("%d",a[j]); 26 } 27 printf("\n"); 28 } 29 } 30 31 return 0; 32 }
2、羽毛球比赛
1 #include <stdio.h> 2 3 int main() 4 { 5 for(char a='X'; a<='Z'; ++a) 6 { 7 for(char b='X'; b<='Z'; b++){ 8 for(char c='X'; c<='Z'; c++){ 9 if(a==b||a==c||b==c|| //重复 10 a=='X'||c=='X'||c=='Z')//条件 11 continue; 12 printf("A=%c\nB=%c\nC=%c\n", a, b, c); 13 } 14 } 15 } 16 return 0; 17 }
3、求素数
1 #include <stdio.h> 2 3 int main() 4 { 5 int a,b,t; 6 scanf("%d%d", &a,&b); 7 if(a>b) 8 t=a,a=b,b=t; 9 10 for(;a<b;a++){ // a<b 11 for(t=2;t<a;t++){ 12 if(a%t==0) 13 break; 14 } 15 if(a==t) 16 printf("%d ",a); 17 } 18 19 return 0; 20 }
4、数列之和
1 #include <stdio.h> 2 3 int main() 4 { 5 int n,a1=1,a2=1,t; 6 double sum=0.0; 7 8 scanf("%d",&n); 9 10 for(int i=0; i<n; ++i){ 11 sum += 1.0*(a1+a2)/a2; 12 t=a1+a2; 13 a1=a2; 14 a2=t; 15 } 16 printf("%f\n",sum); 17 18 return 0; 19 }
5、自然数立方和
1 #include <cstdio> 2 3 int main() 4 { 5 int n,t,sum=0,i,j; 6 scanf("%d",&n); 7 8 for(i=1; sum!=n*n*n; i+=2)//结论不成立 i递增 9 { 10 t=0,sum=0; 11 for( j=i; t<n; j+=2, t++)//第一个奇数 i 最后一个奇数 j 12 { 13 sum+=j; //累加n次奇数 14 } 15 } 16 17 printf("%d*%d*%d=%d=",n,n,n,n*n*n); 18 for( i-=2; i<j-2; i+=2 ) //i,j都多累加2 19 printf( "%d+",i ); 20 printf( "%d\n",i ); 21 22 return 0; 23 }
6、整数次方
1 #include <cstdio> 2 3 int main() 4 { 5 int x,a; 6 scanf("%d%d",&x,&a); 7 8 int s=x; //1次方 9 for(int i=2; i<=a; ++i){ 10 s*=x; //x保持x值 11 s%=1000; 12 } 13 printf("%d\n",s); 14 15 return 0; 16 }
7、韩信点兵
1 #include <cstdio> 2 3 int main() 4 { 5 int a,b,c,i,flag=0; 6 scanf("%d%d%d",&a,&b,&c); 7 8 for( i=10; i<=100; ++i ){ 9 if(i%3==a && i%5==b && i%7==c) 10 { 11 flag=1; 12 break; 13 } 14 } 15 if(flag) 16 printf("%d\n",i); 17 else 18 printf("-1\n"); 19 20 return 0; 21 }
NOJ练习四
1、逆序数
1 #include <cstdio> 2 3 void print( long int n); 4 5 int main() 6 { 7 long int n; 8 scanf( "%ld", &n ); 9 10 print( n ); 11 12 return 0; 13 } 14 15 void print( long int n) 16 { 17 if(n==0) 18 return; 19 printf("%d",n%10); 20 print( n/10 ); 21 }
2、组合数
1 #include <cstdio> 2 3 int output( int n, int m); 4 5 int main() 6 { 7 int n, m; 8 scanf( "%d%d", &n, &m ); 9 10 if( n < m ) 11 printf("wrong\n"); 12 else 13 printf( "%d\n", output( n, m ) ); 14 15 return 0; 16 } 17 18 int output( int n, int m) 19 { 20 int a=1,b=1; 21 for(int i=1; i<=m; ++i){ 22 a *= n--; 23 b *= i; 24 } 25 return a/b; 26 }
3、梯形法求积分
4、ack函数
1 #include <cstdio> 2 int ack( int m, int n ) ; //! 3 int main() 4 { 5 int m,n; 6 scanf( "%d%d",&m,&n ); 7 printf("%d\n", ack( m, n ) ); 8 9 return 0; 10 } 11 int ack( int m, int n ) { 12 if(m==0) 13 return n+1; 14 if(n==0) 15 return ack(m-1,1); 16 return ack(m-1,ack(m,n-1)); 17 }
5、数字加密
1 #include <cstdio> 2 3 int encryption(int n); 4 5 int main() 6 { 7 int n=1998; 8 scanf( "%d",&n ); 9 printf("%d\n", encryption( n ) ); 10 11 return 0; 12 } 13 14 int encryption(int n){ 15 int one,two,three,four; 16 one = (n/1000+5)%10; //4->1 17 two = 10 * ( (n/100%10+5)%10 ); //3->2 18 three = 100 * ( (n/10%10+5)%10 ); //2->3 19 four = 1000 *( (n%10+5)%10 ); //1->4 20 return one+two+three+four; 21 }
6、最小整数
1 #include <cstdio> 2 3 int fceil(double x); 4 5 int main() 6 { 7 double x; 8 scanf( "%lf",&x ); 9 printf("%d\n", fceil( x ) ); 10 11 return 0; 12 } 13 14 int fceil(double x){ 15 if(x<0) 16 return (int)x; 17 return x+0.5; 18 }
7、发工资
1 #include <cstdio> 2 3 int salary( int s ); 4 5 int main() 6 { 7 int count=0,n,s; 8 scanf( "%d", &n ); 9 10 while(n--){ 11 scanf("%d",&s); 12 count+=salary(s); 13 } 14 15 printf("%d\n", count ); 16 17 return 0; 18 } 19 20 int salary( int s ) 21 { 22 /* int CNY[] = {100,50,10,5,2,1}; 23 int count = s/100; 24 for(int i=0; i<sizeof(CNY)/sizeof(int); ++i) 25 { 26 s%=CNY[i]; 27 count += (s%CNY[i])/CNY[i+1]; 28 } 29 return count; */ 30 return 31 s/100+(s%=100)/50+(s%=50)/10+(s%=10)/5+(s%=5)/2+(s%=2)/1; 32 }
8、时间求和
1 #include <cstdio> 2 3 struct Time{ 4 int hour; 5 int minute; 6 int second; 7 }; 8 9 void sum(struct Time a, struct Time b); 10 11 int main() 12 { 13 struct Time a,b; 14 scanf( "%d%d%d%d%d%d", 15 &a.hour,&a.minute,&a.second, 16 &b.hour,&b.minute,&b.second); 17 sum( a, b ); 18 19 return 0; 20 } 21 22 void sum(struct Time a, struct Time b){ 23 struct Time c; 24 c.second = a.second + b.second; 25 c.minute = a.minute + b.minute; 26 c.hour = a.hour + b.hour; 27 if(c.second>=60) 28 { 29 c.second -= 60; 30 c.minute++; 31 } 32 if(c.minute>=60) 33 { 34 c.minute-=60; 35 c.hour++; 36 } 37 printf("%d %d %d\n", c.hour,c.minute,c.second); 38 }
1 #include <cstdio> 2 3 void sum(int AH,int AM,int AS,int BH,int BM,int BS); 4 5 int main() 6 { 7 int AH, AM, AS, BH, BM, BS; 8 scanf( "%d%d%d%d%d%d", 9 &AH, &AM, &AS, 10 &BH, &BM, &BS); 11 sum( AH, AM, AS, BH, BM, BS ); 12 13 return 0; 14 } 15 16 void sum(int AH,int AM,int AS,int BH,int BM,int BS){ 17 int CS = AS + BS; 18 int CM = AM + BM; 19 int CH = AH + BH; 20 if(CS>=60) 21 { 22 CS -= 60; 23 CM++; 24 } 25 if(CM>=60) 26 { 27 CM-=60; 28 CH++; 29 } 30 printf("%d %d %d\n", CH,CM,CS); 31 }
NOJ练习五
1、数组“赋值”
1 #include <cstdio> 2 3 int main() 4 { 5 int a[100],b[100]; 6 int n; 7 scanf("%d", &n ); 8 for(int i=0; i<n; ++i) 9 { 10 scanf("%d",&a[i]); 11 b[i] = a[i]; 12 } 13 for(int j=0; j<n; ++j) 14 if(j%2) 15 printf("%d ",b[j]); 16 printf("\n"); 17 18 return 0; 19 }
2、左上角
1 #include <cstdio> 2 3 int main() 4 { 5 int arr[100][100]; 6 int n; 7 scanf("%d", &n ); 8 9 for(int i=0; i<n; ++i) 10 { 11 for(int j=0; j<n; ++j){ 12 scanf("%d",&arr[i][j]); 13 } 14 } 15 16 for(int i=0; i<n; ++i) 17 { 18 for(int j=0; j<n-i; ++j){ 19 printf("%d ",arr[i][j]); 20 } 21 printf("\n"); 22 } 23 24 return 0; 25 }
3、边沿与内芯的差
1 #include <cstdio> 2 3 int main() 4 { 5 int arr[100][100]; 6 int n,m,s1=0,s2=0; 7 scanf("%d%d", &n,&m ); 8 9 for(int i=0; i<n; ++i) 10 { 11 for(int j=0; j<m; ++j){ 12 scanf("%d",&arr[i][j]); 13 if(i==0||i==n-1||j==0||j==m-1) 14 s1+=arr[i][j]; 15 else 16 s2+=arr[i][j]; 17 } 18 } 19 20 printf("%d\n",s1-s2); 21 22 return 0; 23 }
4、平均值
1 #include <cstdio> 2 3 double avg(int A[],int s,int e); 4 5 int main() 6 { 7 int a[100]; 8 int n,s,e; 9 scanf("%d", &n); 10 11 for(int i=0; i<n; ++i) 12 scanf("%d",&a[i]); 13 14 scanf("%d%d",&s,&e); 15 printf("%f\n",avg(a,s,e)); 16 17 return 0; 18 } 19 20 double avg(int A[],int s,int e) 21 { 22 double sum=0.0; 23 for(int j=s; j<=e; ++j)//! 24 sum+=A[j]; 25 return sum/(e-s+1); //! 26 }
5、冒泡排序
1 #include <cstdio> 2 3 void BubbleSort(int A[],int s,int m); 4 5 int main() 6 { 7 int a[100]; 8 int n,s,m; 9 scanf("%d", &n); 10 11 for(int i=0; i<n; ++i) 12 scanf("%d",&a[i]); 13 14 scanf("%d%d",&s,&m); 15 BubbleSort(a,s,m); 16 17 for(int i=0; i<n; ++i) 18 printf("%d ",a[i]); 19 20 return 0; 21 } 22 23 void BubbleSort(int A[],int s,int m) 24 { 25 for( ; m>0; m--) //连续m个元素,s是开始元素,m不是结束元素 26 { 27 for(int j=s; j<s+m-1; ++j) //s+m<=100 j最大98即<99,保证j+1最大99的边界 28 if(A[j]<A[j+1]) 29 { 30 int t=A[j]; 31 A[j]=A[j+1]; 32 A[j+1]=t; 33 } 34 } 35 }
6、选择排序
1 #include <cstdio> 2 3 void SelectionSort(int a[],int s,int m); 4 5 int main() 6 { 7 int a[100]; 8 int n; 9 scanf( "%d", &n ); 10 11 for(int i=0; i<n; ++i) 12 scanf("%d", &a[i]); 13 14 int s,m; 15 scanf("%d%d",&s,&m); 16 17 SelectionSort( a, s, m ); 18 19 for(int j=0; j<n; ++j) 20 printf("%d ", a[j]); 21 22 return 0; 23 } 24 void SelectionSort(int a[],int s,int m) 25 { 26 27 for(int i=s; i<s+m-1; ++i) 28 { 29 int index=i; 30 for( int j=i+1; j<s+m; ++j){ 31 if(a[index]<a[j]){ 32 index = j; 33 } 34 } 35 if(index!=i){ 36 int t=a[i]; 37 a[i]=a[index]; 38 a[index]=t; 39 } 40 } 41 }
7、插入排序
1 #include <cstdio> 2 3 void InsertionSort(int A[],int s,int m); 4 5 int main() 6 { 7 int a[100]; 8 int n; 9 scanf( "%d", &n ); 10 11 for(int i=0; i<n; ++i) 12 scanf("%d", &a[i]); 13 14 int s,m; 15 scanf("%d%d",&s,&m); 16 17 InsertionSort( a, s, m ); 18 19 for(int j=0; j<n; ++j) 20 printf("%d ", a[j]); 21 22 return 0; 23 } 24 void InsertionSort(int A[],int s,int m) 25 { 26 int i; 27 for(i=s+1; i<s+m; ++i) //! 28 { 29 int j=i-1,key=A[i];/*保存第i个元素,左边的元素i-1*/ 30 while( j>=s && key>A[j] ) //! 31 {/*保存的元素key与之前的元素从右向左逐个比较*/ 32 A[j+1]=A[j];/*移动(向右赋值)*/ 33 j--; 34 } 35 A[j+1]=key;/*j--退出,恢复正确值j+1*/ 36 } 37 }
8、恐怖水母
1 #include <cstdio> 2 3 void InsertionSort(int A[],int s,int m); 4 5 int main() 6 { 7 int d[100],p[100]; //d是n个触手的直径,p是m个海绵宝宝的能力值 8 int n,m; //n个触手,m个海绵宝宝 9 scanf( "%d%d", &n,&m ); 10 11 for(int i=0; i<n; ++i) 12 scanf("%d", &d[i]); 13 for(int j=0; j<m; ++j) 14 scanf("%d", &p[j]); 15 16 InsertionSort(d,0,n); 17 InsertionSort(p,0,m); 18 19 int x=0; //x是金币 20 for(int i=0; i<n; ++i) 21 { 22 for(int j=0; j<m; ++j){ 23 if( p[j] >= d[i] ){ //找到能力值大于等于触手直径 24 x += p[j]; 25 break; 26 } 27 } 28 } 29 30 if(x==0) //无解 31 printf("NULL\n"); 32 else 33 printf("%d\n",x); 34 35 return 0; 36 } 37 void InsertionSort(int A[],int s,int m) 38 { 39 int i; 40 for(i=s+1; i<s+m; ++i) 41 { 42 int j=i-1,key=A[i]; 43 while( j>=s && key<A[j] ) //! 44 { 45 A[j+1]=A[j]; 46 j--; 47 } 48 A[j+1]=key; 49 } 50 }
9、山迪的麻烦
#include <cstdio> int BubbleSort(int A[],int n); int main() { int card[100]; int n; scanf( "%d", &n ); for(int i=0; i<n; ++i) scanf("%d", &card[i]); printf("%d\n", BubbleSort(card,n) ); return 0; } int BubbleSort(int A[],int n) { int count=0; for(int i=0; i<n; ++i) { for(int j=0; j<n-1; ++j) if(A[j]>A[j+1]) { int t=A[j]; A[j]=A[j+1]; A[j+1]=t; count++; } } return count; }
10、二分查找
1 #include <cstdio> 2 3 int BinarySearch(int A[],int n, int m); 4 5 int main() 6 { 7 int a[100]; 8 int n,m; 9 scanf("%d", &n); 10 11 for(int i=0; i<n; ++i) 12 scanf("%d",&a[i]); 13 14 scanf("%d", &m); 15 16 int i = BinarySearch(a,n,m); 17 if(-1==i) 18 printf("null\n"); 19 else 20 printf("%d\n", i); 21 22 return 0; 23 } 24 25 int BinarySearch(int A[],int n, int m) 26 { 27 int left = 0; 28 int right = n-1; 29 while( left<=right ) 30 { 31 int mid = left + (right-left)/2; 32 if(A[mid]==m) 33 return mid; 34 if( A[mid]>m ) 35 right = mid-1; 36 else 37 left = mid+1; 38 } 39 return -1; 40 }
NOJ练习六
1、字符串左中右
1 #include <cstdio> 2 #include <cstring> 3 4 void Left(char src[],int n,char dest[]); 5 void Right(char src[],int n,char dest[]); 6 void Mid(char src[],int loc,int n,char dest[]); 7 8 int main() 9 { 10 char src[128] = "www.nwpu.edu.cn"; 11 char dest[128] = ""; 12 int n,loc; 13 scanf("%s%d%d", src, &n, &loc); 14 Left( src, n, dest); 15 Right( src, n, dest ); 16 Mid( src, loc, n, dest ); 17 return 0; 18 } 19 20 void Left(char src[],int n,char dest[]){ 21 int i=0; 22 for( ; i<n; ++i) 23 dest[i] = src[i]; 24 dest[i] = '\0'; 25 printf("%s\n", dest); 26 } 27 28 void Right(char src[],int n,char dest[]){ 29 int j=n, i=strlen(src)-1; 30 dest[j] = '\0'; 31 while(n--){ 32 dest[--j] = src[i--]; 33 } 34 printf("%s\n", dest); 35 } 36 37 void Mid(char src[],int loc,int n,char dest[]){ 38 int j=0; 39 for(; j<n; ++j) 40 dest[j] = src[loc++]; 41 dest[j] = '\0'; 42 printf("%s\n", dest); 43 }
2、字符串替换
1 #include <cstdio> 2 #include <cstring> 3 4 int main() 5 { 6 char src[1000] = ""; 7 gets( src ); 8 char* p = src; 9 char* pre = p; 10 11 while(*p){ 12 if( !strncmp(p,"you",3) ) 13 { 14 *pre++ = 'w'; 15 *pre++ = 'e'; 16 p+=3; 17 } 18 *pre++ = *p++; 19 } 20 *pre='\0'; 21 22 puts(src); 23 24 return 0; 25 }
3、字符串比较
1 #include <cstdio> 2 #include <cstring> 3 4 int stringcompare(char S1[],char S2[]); 5 6 int main() 7 { 8 char s1[128],s2[128]; 9 gets(s1); 10 gets(s2); 11 printf("%d\n",stringcompare(s1,s2)); 12 13 return 0; 14 } 15 16 int stringcompare(char S1[],char S2[]){ 17 int ret=0; 18 while(!(ret=*(unsigned char*)S1-*(unsigned char*)S2) && S2){ 19 S1++, S2++; //两字符串对应字符不等了 或者 S2到字符串尾部了 20 } //'\0'与其他字符不等 也相当于S1到尾部了 21 return ret; 22 }
4、字符串复制
1 #include <cstdio> 2 #include <cstring> 3 4 char* stringcopy(char S1[],char S2[],int m); 5 6 int main() 7 { 8 char s1[128]="",s2[128]=""; 9 int m; 10 gets(s2); 11 scanf("%d",&m); 12 printf("%s\n",stringcopy(s1,s2,m)); 13 14 return 0; 15 } 16 17 char* stringcopy(char S1[],char S2[],int m){ 18 char *p = (char*)S1; 19 char *q = (char*)S2+m; 20 while( *p++ = *q++ ); 21 return (char*)S1; 22 }
5、字符串排序
1 #include <cstdio> 2 #include <cstring> 3 4 void stringsort(char s[][32]); 5 6 int main() 7 { 8 char s[10][32]; 9 for( int i=0; i<10; ++i ){ 10 scanf("%s",s[i]); 11 } 12 stringsort( s ); 13 for(int i=0; i<10; ++i) 14 printf("%s ",s[i]); 15 16 return 0; 17 } 18 //冒泡排序 19 void stringsort(char s[][32]){ 20 char t[32]; 21 for(int i=0; i<10-1; ++i){ 22 for(int j=0; j<10-i-1; ++j){ 23 if(strcmp(s[j],s[j+1])>0){ 24 strcpy(t,s[j]); 25 strcpy(s[j],s[j+1]); 26 strcpy(s[j+1],t); 27 } 28 } 29 } 30 }
6、“摘”数字
1 #include <cstdio> 2 #include <cstdlib> 3 4 int main() 5 { 6 int arr[32]; 7 char s[128]; 8 char* p = s; 9 int i=0,flag=1; 10 11 gets( s ); 12 13 while( *p ) 14 { 15 if( *p>='0'&&*p<='9' ) 16 { 17 if(flag) 18 arr[i++]=atoi(p); 19 flag=0; 20 } 21 else{ 22 flag=1; 23 } 24 p++; 25 } 26 printf("%d\n", i ); 27 for(int j=0; j<i; ++j) 28 printf("%d ",arr[j]); 29 30 return 0; 31 }
7、字符串逆序
1 #include <cstdio> 2 #include <cstring> 3 4 int main() 5 { 6 char s[128]; 7 gets( s ); 8 9 int i,j=0; 10 while(s[j]!='=') 11 j++; 12 s[j]='\0'; 13 14 for(i=0,j--; i<j; i++,j--) 15 { 16 char t=s[i]; 17 s[i]=s[j]; 18 s[j]=t; 19 } 20 21 printf( "%s\n", s ); 22 23 return 0; 24 }
8、合并字符串
1 #include <cstdio> 2 #include <cstring> 3 4 void stringmerge(char S1[],char S2[]); 5 6 int main() 7 { 8 char s1[128],s2[128]; 9 gets( s1 ); 10 gets( s2 ); 11 12 stringmerge( s1, s2); 13 //strcat(s1,s2); 14 15 printf( "%s\n", s1 ); 16 17 return 0; 18 } 19 20 void stringmerge(char S1[],char S2[]){ 21 while( *(char*)S1 ) 22 (char*)S1++; 23 while( *(char*)S1++ = *(char*)S2++ ); 24 }
9、删除字符
1 #include <cstdio> 2 3 void deletechar(char S[],char c); 4 5 int main() 6 { 7 char s[128],c; 8 gets( s ); 9 c = getchar(); 10 11 deletechar( s, c); 12 13 printf( "%s\n", s ); 14 15 return 0; 16 } 17 18 void deletechar(char S[],char c){ 19 char *p1=S, *p2=S; 20 while(*p2){ 21 if(*p2!=c) //!!! 22 *p1++ = *p2; 23 p2++; 24 } 25 *p1='\0'; //!!! 26 }
10、文章统计
1 #include <cstdio> 2 3 int cletter,sletter,digit,space,other; 4 void count(char *s); 5 6 int main() 7 { 8 char s[128]; 9 10 int n=3; 11 while( n-- ){ 12 gets( s ); 13 count( s ); 14 } 15 16 printf("%d %d %d %d %d\n", 17 cletter,sletter,digit,space,other); 18 19 return 0; 20 } 21 22 void count(char *s) 23 { 24 while( *s ) 25 { 26 if(*s>='A'&&*s<='Z') 27 cletter++; 28 else if(*s>='a'&&*s<='z') 29 sletter++; 30 else if(*s>='0'&&*s<='9') 31 digit++; 32 else if(*s==' ') 33 space++; 34 else 35 other++; 36 s++; 37 } 38 }
11、分离单词
1 #include <cstdio> 2 #include <cstring> 3 4 int main() 5 { 6 char s1[128]="",s2[128]=""; 7 gets( s1 ); 8 9 int len = strlen(s1); 10 char* p1=s1+len-1; //字符串尾部指针 11 12 int flag =0;//不是字母 13 int count=0;//字母个数 14 15 for(int i=0; i<=len; ++i) 16 { 17 if( *p1>='a'&&*p1<='z' || 18 *p1>='A'&&*p1<='Z' ) //如果是字母 19 { 20 count++; //记录字母个数 21 flag = 1; //是字母 22 } 23 else { 24 flag=0; //不是字符 25 } 26 27 if(!flag && count) //出现不是字母且字母个数不为0,进行单词连接 28 { 29 strncat(s2,p1+1,count); //连接单词 30 if(i!=len) //最后一个单词没有空格 31 strcat(s2," "); 32 33 flag = 1; //设定是字母 34 count = 0; //字母个数统计归0 35 } 36 p1--; //移动字符串指针 37 } 38 39 printf("%s\n",s2); //有回车 40 41 return 0; 42 }
12、特殊要求的字符串
1 #include <cstdio> 2 #include <cstring> 3 int main() 4 { 5 char a[10000],b[5000],c[5000],t,k; 6 int n,i,j; 7 gets(a); 8 n=strlen(a); 9 if(n%2==0) 10 { 11 for(i=0;i<n/2;++i) 12 b[i]=a[i]; 13 for(i=n/2;i<n;++i) 14 c[i-n/2]=a[i]; 15 for(i=1;i<n/2;++i) 16 for(j=0;j<n/2-i;++j) 17 if(b[j]<b[j+1]) 18 t=b[j],b[j]=b[j+1],b[j+1]=t; 19 for(i=1;i<n/2;i++) 20 for(j=0;j<n/2-i;j++) 21 if(c[j]>c[j+1]) 22 t=c[j],c[j]=c[j+1],c[j+1]=t; 23 strcat(c,b); 24 puts(c); 25 } 26 else{ 27 for(i=0;i<n/2;i++) 28 b[i]=a[i]; 29 for(i=n/2+1;i<n;i++) 30 c[i-(n/2+1)]=a[i]; 31 for(i=1;i<n/2;i++) 32 for(j=0;j<n/2-i;j++) 33 if(b[j]<b[j+1]) 34 t=b[j],b[j]=b[j+1],b[j+1]=t; 35 for(i=1;i<n/2;i++) 36 for(j=0;j<n/2-i;j++) 37 if(c[j]>c[j+1]) 38 t=c[j],c[j]=c[j+1],c[j+1]=t; 39 printf("%s",c); 40 putchar(a[n/2]); 41 printf("%s",b); 42 } 43 return 0; 44 }
1 #include <cstdio> 2 #include <cstring> 3 4 int lessthan(char a,char b); 5 int greaterthan(char a,char b); 6 void bubble(char* s, int n, int(*compare)(char,char)); 7 8 int main() 9 { 10 char a[10000],b[5000],c[5000]; 11 int n,i; 12 gets(a); 13 n=strlen(a); 14 if(n%2==0) 15 { 16 for(i=0;i<n/2;++i) 17 b[i]=a[i]; 18 for(i=n/2;i<n;++i) 19 c[i-n/2]=a[i]; 20 bubble(b,n/2,lessthan); 21 bubble(c,n/2,greaterthan); 22 strcat(c,b); 23 puts(c); 24 } 25 else{ 26 for(i=0;i<n/2;i++) 27 b[i]=a[i]; 28 for(i=n/2+1;i<n;i++) 29 c[i-(n/2+1)]=a[i]; 30 bubble(b,n/2,lessthan); 31 bubble(c,n/2,greaterthan); 32 printf("%s",c); 33 putchar(a[n/2]); 34 printf("%s",b); 35 } 36 return 0; 37 } 38 int lessthan(char a,char b){ 39 return a < b; 40 } 41 int greaterthan(char a,char b){ 42 return a > b; 43 } 44 void bubble(char* s, int n, int(*compare)(char,char)) 45 { 46 int i,j; 47 char t; 48 for(i=1;i<n;i++){ 49 for(j=0;j<n-i;j++){ 50 if(compare(s[j],s[j+1])) 51 t=s[j],s[j]=s[j+1],s[j+1]=t; 52 } 53 } 54 }
1 //不知道为什么过不了OJ 2 #include <cstdio> 3 #include <cstring> 4 5 void stringsort1(char *s,int start, int end); 6 void stringsort2(char *s,int start, int end); 7 8 int main() 9 { 10 char s[10000]=""; 11 char t[10000]=""; 12 gets( s ); 13 //分割 14 int n=strlen( s ); 15 int left=n/2,right=n/2; 16 if(n%2){ 17 right++; 18 } 19 //排序 20 stringsort1(s,0,left); 21 stringsort2(s,right,n); 22 //交换 23 strncpy(t,s,left); 24 strncpy(s,s+right,left); 25 strncpy(s+right,t,left); 26 //输出 27 printf("%s\n", s ); 28 29 return 0; 30 } 31 //降序 32 void stringsort1(char *s,int start, int end) 33 { 34 int key[256]={0},index=start;//!!! 35 for(int i=start; i<end; ++i) 36 { 37 key[s[i]]++; 38 } 39 for(int j=256;j>=0; --j) 40 { 41 if(key[j]){ 42 s[index++] = j; 43 key[j]--; 44 j++; 45 } 46 } 47 } 48 //升序 49 void stringsort2(char *s,int start, int end) 50 { 51 int key[256]={0},index=start;//!!! 52 for(int i=start; i<end; ++i) 53 { 54 key[s[i]]++; 55 } 56 for(int j=0;j<=256; ++j) 57 { 58 if(key[j]){ 59 s[index++] = j; 60 key[j]--; 61 j--; //!!! 62 } 63 } 64 }
1 #include <cstdio> 2 #include <cstring> 3 4 int lessthan(char a,char b); 5 int greaterthan(char a,char b); 6 void bubble(char* s, int start,int end, int(*compare)(char,char)); 7 8 int main() 9 { 10 char s[10000]=""; 11 char t[10000]=""; 12 gets( s ); 13 //分割 14 int n=strlen( s ); 15 int left=n/2,right=n/2; 16 if(n%2){ 17 right++; 18 } 19 //排序 20 bubble(s,0,left,lessthan); 21 bubble(s,right,n,greaterthan); 22 //交换 23 strncpy(t,s,left); 24 strncpy(s,s+right,left); 25 strncpy(s+right,t,left); 26 //输出 27 printf("%s\n", s ); 28 29 return 0; 30 } 31 int lessthan(char a,char b){ 32 return a < b; 33 } 34 int greaterthan(char a,char b){ 35 return a > b; 36 } 37 void bubble(char* s, int start, int end, int(*compare)(char,char)) 38 { 39 int i,j; 40 char t; 41 for(i=start+1;i<end;i++){ 42 for(j=start;j<start+end-i;j++){ 43 if(compare(s[j],s[j+1])) 44 t=s[j],s[j]=s[j+1],s[j+1]=t; 45 } 46 } 47 }
NOJ练习七
1、分数加减法
1 #include <stdio.h> 2 3 int gcd(int a,int b); 4 5 int main() 6 { 7 int a,b,c,d; 8 int up,down; 9 char op; 10 11 scanf("%d/%d%c%d/%d",&a,&b,&op,&c,&d); 12 13 up = (op=='+') ? a*d+c*b : a*d-c*b; 14 down = b*d; 15 16 if(!up){ 17 printf("0\n"); 18 } 19 else{ 20 int reduce = gcd(up,down); 21 printf("%d/%d\n",up/reduce,down/reduce); 22 } 23 24 return 0; 25 } 26 27 int gcd(int a,int b){ 28 return b==0?a:gcd(b,a%b); 29 }
2、二进制
1 #include <stdio.h> 2 3 int main() 4 { 5 int n, cnt=32; 6 char binary[33]=""; 7 8 scanf("%d",&n); 9 10 while(n){ 11 binary[--cnt] = n&0x1 ? '1' : '0'; 12 n>>=0x1; 13 } 14 15 printf("%s\n",binary+cnt); 16 17 return 0; 18 }
3、字符串加密
1 #include <stdio.h> 2 3 void strencode(char *s); 4 5 int main() 6 { 7 char str[128]=""; 8 scanf("%s",str); 9 10 strencode(str); 11 printf("%s\n",str); 12 13 return 0; 14 } 15 16 void strencode(char *s){ 17 while(*s){ 18 if( (*s>='A'&&*s<='Z') && (*s+=3)>'Z' ){ 19 *s-=26; 20 } 21 else if( (*s>='a'&&*s<='z') && (*s-=3)<'a' ){ 22 *s+=26; 23 } 24 s++; 25 } 26 }
4、计算成绩表
1 #include <iostream> 2 using namespace std; 3 4 struct student{ 5 long no; 6 char name[128]; 7 int age; 8 //double english,maths,chinese,politics,physics,chemistry,computer; 9 double achievement[7]; 10 /* double total;//题目要求 无输出 11 double ave; //题目要求 无输出 12 bool operator < (const student& s){ 13 return total<s.total; 14 } */ 15 }; 16 int main() 17 { 18 student stu[10],max; 19 /* max.total=0.0; */ 20 double total=0.0; 21 int i=0; 22 while(i<10) 23 { 24 int j=0; 25 double t=0.0; 26 scanf("%ld%s%d",&stu[i].no,stu[i].name,&stu[i].age); 27 while(j<7){ 28 scanf("%lf",&stu[i].achievement[j]); 29 t+=stu[i].achievement[j]; 30 j++; 31 } 32 /* stu[i].total = t; 33 stu[i].ave = stu[i].total/7; 34 if(max.total<stu[i].total) //if(max<stu[i]) 35 max=stu[i]; */ 36 if(total<t){ 37 total=t; 38 max = stu[i]; 39 } 40 i++; 41 } 42 printf("%ld %s %d %.2f %.2f %.2f %.2f %.2f %.2f %.2f ", //注意:最后有个空格 43 max.no, 44 max.name, 45 max.age, 46 max.achievement[0], 47 max.achievement[1], 48 max.achievement[2], 49 max.achievement[3], 50 max.achievement[4], 51 max.achievement[5], 52 max.achievement[6] 53 ); 54 return 0; 55 }
5、复数
1 #include <iostream> 2 using namespace std; 3 4 typedef struct complex { 5 double r; 6 double i; 7 }COMPLEX; 8 9 //和 (a+bi)+(c+di)=(a+c)+(b+d)i 10 void sum(const COMPLEX& c1, const COMPLEX& c2){ 11 //(-1.00+5.00i)+(4.00+3.00i)=(3.00+8.00i) 12 char op = '+'; 13 printf("(%.2f%+.2fi)%c(%.2f%+.2fi)=(%.2f%+.2fi)\n", 14 c1.r,c1.i,op,c2.r,c2.i, 15 (c1.r+c2.r), 16 (c1.i+c2.i) 17 ); 18 } 19 20 //差 (a+bi)-(c+di)=(a-c)+(b-d)i 21 void difference(const COMPLEX& c1, const COMPLEX& c2){ 22 //(-1.00+5.00i)-(4.00+3.00i)=(-5.00+2.00i) 23 char op = '-'; 24 printf("(%.2f%+.2fi)%c(%.2f%+.2fi)=(%.2f%+.2fi)\n", 25 c1.r,c1.i,op,c2.r,c2.i, 26 (c1.r-c2.r), 27 (c1.i-c2.i) 28 ); 29 } 30 31 //积 (a+bi)*(c+di)=(ac-bd)+(bc+ad)i 32 void product(const COMPLEX& c1, const COMPLEX& c2){ 33 //(-1.00+5.00i)*(4.00+3.00i)=(-19.00+17.00i) 34 char op = '*'; 35 printf("(%.2f%+.2fi)%c(%.2f%+.2fi)=(%.2f%+.2fi)\n", 36 c1.r,c1.i,op,c2.r,c2.i, 37 (c1.r*c2.r-c1.i*c2.i), 38 (c1.i*c2.r+c1.r*c2.i) 39 ); 40 } 41 42 //商 (a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +((bc-ad)/(c^2+d^2))i 43 void quotient(const COMPLEX& c1, const COMPLEX& c2){ 44 //(-1.00+5.00i)/(4.00+3.00i)=(0.44+0.92i) 45 char op = '/'; 46 printf("(%.2f%+.2fi)%c(%.2f%+.2fi)=(%.2f%+.2fi)\n", 47 c1.r,c1.i,op,c2.r,c2.i, 48 (c1.r*c2.r+c1.i*c2.i)/(c2.r*c2.r+c2.i*c2.i), 49 (c1.i*c2.r-c1.r*c2.i)/(c2.r*c2.r+c2.i*c2.i) 50 ); 51 } 52 53 int main() 54 { 55 COMPLEX c1,c2; 56 scanf("%lf%lf%lf%lf",&c1.r,&c1.i,&c2.r,&c2.i); 57 //和 sum 58 sum(c1,c2); 59 //差 difference 60 difference(c1,c2); 61 //积 product 62 product(c1,c2); 63 //商 quotient 64 quotient(c1,c2); 65 return 0; 66 }
6、删除链表的节点
1 #include <iostream> 2 using namespace std; 3 4 struct student{ 5 int no; 6 char name[12]; 7 int age; 8 }; 9 10 typedef struct node *linkedList; 11 struct node{ //链表结点 12 struct student stu; //学生信息 13 struct node *next; //结点指针 14 }; 15 16 linkedList createLinkedListNode()//创建链表结点 17 { 18 linkedList l; 19 l = (linkedList)malloc(sizeof (struct node) ); 20 l->next = NULL; 21 return l; 22 } 23 24 linkedList createLinkedList()//建立一个链表 25 { 26 linkedList head,p; 27 head = p = createLinkedListNode(); 28 29 struct student stu; 30 while(true) 31 { 32 scanf("%d",&stu.no); 33 if(!stu.no){ 34 break; 35 } 36 scanf("%s%d",stu.name,&stu.age); 37 p->next = createLinkedListNode();//尾插法 38 p->next->stu = stu; 39 p = p->next; //P指向新结点 40 } 41 42 return head; 43 } 44 45 void deleteNode(linkedList& head, int no) 46 { 47 linkedList p1=head->next , p2=head ; 48 while(p1&&p1->stu.no!=no) 49 { 50 p2=p1; 51 p1=p1->next; 52 } 53 p2->next = p1->next; //删除结点 54 free(p1); 55 } 56 57 void printLinkedList(linkedList head) 58 { 59 linkedList p = head->next; 60 61 while(p) 62 { 63 printf("%d ",p->stu.no); 64 p = p->next; 65 } 66 printf("\n"); 67 } 68 69 int main() 70 { 71 linkedList head = createLinkedList(); 72 printLinkedList(head); 73 74 int no; 75 scanf("%d",&no); 76 deleteNode(head,no); 77 78 printLinkedList(head); 79 return 0; 80 }
7、链表插入节点
1 #include <iostream> 2 using namespace std; 3 4 typedef struct node *linkedList; 5 struct node{ //链表结点 6 int no; //职工号 7 struct node *next; //结点指针 8 }; 9 10 linkedList createLinkedListNode()//创建链表结点 11 { 12 linkedList l; 13 l = (linkedList)malloc(sizeof (struct node) ); 14 l->next = NULL; 15 return l; 16 } 17 18 void insert( linkedList& head ) 19 { 20 int no; 21 scanf("%d",&no); 22 linkedList p=head,s; 23 24 while( p->next && no>p->next->no ){//升序 25 p=p->next; 26 } 27 28 s = createLinkedListNode(); //创建一个结点 29 s->no = no; //数据 30 31 s->next=p->next; //插入 32 p->next=s; 33 } 34 35 linkedList create()//建立一个链表 36 { 37 linkedList head; 38 head = createLinkedListNode(); 39 40 int n; 41 scanf("%d",&n); 42 43 while(n--) 44 { 45 insert( head ); 46 } 47 48 return head; 49 } 50 51 void list(linkedList head) 52 { 53 linkedList p = head->next; 54 while(p) 55 { 56 printf("%d ",p->no); 57 p = p->next; 58 } 59 printf("\n"); 60 } 61 62 int main() 63 { 64 linkedList head = create(); 65 insert(head); 66 list(head); 67 68 return 0; 69 }
8、链表动态增长或缩小
1 #include <iostream> 2 using namespace std; 3 4 int ageArr[100] = {0}; 5 6 typedef struct node *linkedList; 7 struct node{ //链表结点 8 int age; //职工号 9 struct node *next; //结点指针 10 }; 11 12 linkedList createLinkedListNode()//创建链表结点 13 { 14 linkedList l; 15 l = (linkedList)malloc(sizeof (struct node) ); 16 l->next = NULL; 17 return l; 18 } 19 20 void deleteNode(linkedList& head, int age) 21 { 22 linkedList p1=head->next , p2=head ; 23 while(p1&&p1->age!=age) 24 { 25 p2=p1; 26 p1=p1->next; 27 } 28 p2->next = p1->next; //删除结点 29 free(p1); 30 } 31 32 void insert( linkedList& head ) 33 { 34 int age; 35 scanf("%d",&age); 36 if(ageArr[age]){ 37 deleteNode(head,age);//删除 38 ageArr[age]--; 39 return; 40 } 41 ageArr[age]++; 42 43 linkedList p=head,s; 44 while( p->next ){//最后一个结点 45 p=p->next; 46 } 47 s = createLinkedListNode(); //创建一个结点 48 s->age = age; //数据 49 50 s->next=p->next; //插入 51 p->next=s; 52 } 53 54 linkedList create()//建立一个链表 55 { 56 linkedList head; 57 head = createLinkedListNode(); 58 59 int n; 60 scanf("%d",&n); 61 62 while(n--) 63 { 64 insert( head ); 65 } 66 67 return head; 68 } 69 70 void list(linkedList head) 71 { 72 linkedList p = head->next; 73 while(p) 74 { 75 printf("%d ",p->age); 76 p = p->next; 77 } 78 printf("\n"); 79 } 80 81 int main() 82 { 83 linkedList head = create(); 84 list(head); 85 86 return 0; 87 }
9、大数加法
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 100; 6 7 class Number 8 { 9 int digit[N];//数组指针 10 int length;//长度 11 bool sign; //负数为1 12 13 public: 14 15 Number():length(0), sign(0) 16 { 17 memset(digit,0,sizeof(digit)); 18 } 19 20 Number(const char* s)//构造,类型转换 21 { 22 memset(digit,0,sizeof(digit)); 23 if(s[0]=='-'){ //符号 24 sign=1; 25 s++; 26 } 27 else sign =0; 28 29 length = strlen(s); //长度 30 31 int j=length-1; 32 while( *s ) //数组 33 digit[j--] = *s++ -'0'; 34 } 35 36 int compare( const Number&t )const{ //对象比较 37 if(this->length==t.length){ 38 int i=t.length-1; 39 for( ; i>=0; i-- ){ 40 if(this->digit[i]<t.digit[i]) 41 return -1; //长度相等字符小返回-1 42 else if(this->digit[i]>t.digit[i]) 43 return 1; //长度相等字符大返回1 44 } 45 if(i<0) //长度字符相等返回0 46 return 0; 47 } 48 else if( this->length< t.length ) 49 return -1; //长度小返回-1 50 else 51 return 1; //长度大返回1 52 } 53 54 Number add(const Number &t)const //对象数组相加 55 { 56 Number result; 57 int len = compare(t)<0 ? t.length: length; 58 result.length=len; 59 60 for(int i=0; i<len; i++) 61 { 62 result.digit[i] += digit[i]+t.digit[i]; 63 result.digit[i+1] += result.digit[i]/10; //进位 64 result.digit[i] %= 10; //取余 65 } 66 if(result.digit[length]!=0) //最高位不是0,长度加1 67 result.length++; 68 return result; 69 } 70 71 Number subtract(const Number &t) const//对象数组相减 72 { 73 Number result; 74 int len = compare(t)<0 ? t.length: this->length; 75 result.length=len; 76 77 for(int i=0; i<len; i++) 78 { 79 result.digit[i] += this->digit[i]-t.digit[i]; 80 if(result.digit[i]<0){ 81 result.digit[i] += 10; //借位 82 result.digit[i+1]--; //退一 83 } 84 } 85 while( result.length>1 && result.digit[result.length-1]==0 ) 86 result.length--; //最高位为0 长度减1,直到最高位不是0且长度大于1(考虑到结果等于0的情况) 87 return result; 88 } 89 90 Number operator+(const Number &t){ 91 Number result; 92 if(this->sign==t.sign){ //符号一样 直接加 93 result = add(t); 94 result.sign= this->sign; 95 } 96 else{ //符号不一样 97 if(compare(t)==0){//相等 98 result = subtract(t);//result.digit[0] = 0;result.length = 1; 99 result.sign= 0; 100 } 101 else { 102 if(compare(t)<0){ 103 result = t.subtract(*this); 104 result.sign = t.sign; 105 } 106 else if(compare(t)>0){ 107 result = subtract(t); 108 result.sign = sign; 109 } 110 } 111 } 112 return result; 113 } 114 friend ostream& operator << (ostream& o, const Number& t) { 115 if(t.sign) 116 o << '-'; 117 for(int i=t.length-1; i>=0; i--) 118 { 119 o << t.digit[i]; 120 } 121 return o; 122 } 123 }; 124 125 int main() 126 { 127 char str[N]; 128 cin >> str; 129 Number a(str); 130 cin >> str; 131 Number b(str); 132 133 cout << a + b << endl; 134 135 return 0; 136 }
10、大数减法
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 100; 6 7 class Number 8 { 9 int digit[N];//数组指针 10 int length;//长度 11 bool sign; //负数为1 12 13 public: 14 15 Number():length(0), sign(0) 16 { 17 memset(digit,0,sizeof(digit)); 18 } 19 20 Number(const char* s)//构造,类型转换 21 { 22 memset(digit,0,sizeof(digit)); 23 if(s[0]=='-'){ //符号 24 sign=1; 25 s++; 26 } 27 else sign =0; 28 29 length = strlen(s); //长度 30 31 int j=length-1; 32 while( *s ) //数组 33 digit[j--] = *s++ -'0'; 34 } 35 36 int compare( const Number&t )const{ //对象比较 37 if(this->length==t.length){ 38 int i=t.length-1; 39 for( ; i>=0; i-- ){ 40 if(this->digit[i]<t.digit[i]) 41 return -1; //长度相等字符小返回-1 42 else if(this->digit[i]>t.digit[i]) 43 return 1; //长度相等字符大返回1 44 } 45 if(i<0) //长度字符相等返回0 46 return 0; 47 } 48 else if( this->length< t.length ) 49 return -1; //长度小返回-1 50 else 51 return 1; //长度大返回1 52 } 53 54 Number add(const Number &t)const //对象数组相加 55 { 56 Number result; 57 int len = compare(t)<0 ? t.length: length; 58 result.length=len; 59 60 for(int i=0; i<len; i++) 61 { 62 result.digit[i] += digit[i]+t.digit[i]; 63 result.digit[i+1] += result.digit[i]/10; //进位 64 result.digit[i] %= 10; //取余 65 } 66 if(result.digit[length]!=0) //最高位不是0,长度加1 67 result.length++; 68 return result; 69 } 70 71 Number subtract(const Number &t) const//对象数组相减 72 { 73 Number result; 74 int len = compare(t)<0 ? t.length: this->length; 75 result.length=len; 76 77 for(int i=0; i<len; i++) 78 { 79 result.digit[i] += this->digit[i]-t.digit[i]; 80 if(result.digit[i]<0){ 81 result.digit[i] += 10; //借位 82 result.digit[i+1]--; //退一 83 } 84 } 85 while( result.length>1 && result.digit[result.length-1]==0 ) 86 result.length--; //最高位为0 长度减1,直到最高位不是0且长度大于1(考虑到结果等于0的情况) 87 return result; 88 } 89 90 Number operator-(const Number &t){ //重载- 91 Number result; 92 //1.符号一样 93 if(this->sign==t.sign){ 94 if(t.sign==1) 95 { //都为负数 96 if( compare(t)<0 ||compare(t)==0){ 97 result = t.subtract(*this); 98 result.sign = 0; 99 } 100 else{ 101 result = subtract(t); 102 result.sign = 1; 103 } 104 } 105 else//都为正数 106 { 107 if(compare(t)<0){ 108 result = t.subtract(*this); 109 result.sign = 1; 110 } 111 else{ 112 result = subtract(t); 113 result.sign= 0; 114 } 115 } 116 } 117 //2.符号不一样 118 else{ 119 result = add(t); 120 result.sign = this->sign; 121 } 122 123 return result; 124 } 125 126 friend ostream& operator << (ostream& o, const Number& t) { 127 if(t.sign) 128 o << '-'; 129 for(int i=t.length-1; i>=0; i--) 130 { 131 o << t.digit[i]; 132 } 133 return o; 134 } 135 }; 136 137 int main() 138 { 139 char str[N]; 140 cin >> str; 141 Number a(str); 142 cin >> str; 143 Number b(str); 144 145 cout << a - b << endl; 146 147 return 0; 148 }
NOJ练习八
1、三角形
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 class Point{ 6 double x; 7 double y; 8 public: 9 Point(double x = 0.0, double y = 0.0){ 10 this->x = x; 11 this->y = y; 12 } 13 double getX()const{ 14 return x; 15 } 16 double getY()const{ 17 return y; 18 } 19 }; 20 21 class Triangle{ 22 Point p1,p2,p3; 23 double lengthOfTwoPoint(const Point& p1, const Point& p2){ 24 return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX()) + 25 (p1.getY()-p2.getY())*(p1.getY()-p2.getY()) ); 26 } 27 public: 28 Triangle(const Point& p1, const Point& p2, const Point& p3){ 29 if(isTriangle(p1,p2,p3)) 30 { 31 this->p1 = p1; 32 this->p2 = p2; 33 this->p3 = p3; 34 } 35 } 36 bool isTriangle(const Point& p1, const Point& p2, const Point& p3){ 37 if( lengthOfTwoPoint(p1,p2)+lengthOfTwoPoint(p1,p3)>lengthOfTwoPoint(p2,p3)&& 38 lengthOfTwoPoint(p2,p1)+lengthOfTwoPoint(p2,p3)>lengthOfTwoPoint(p1,p3)&& 39 lengthOfTwoPoint(p3,p1)+lengthOfTwoPoint(p3,p2)>lengthOfTwoPoint(p1,p2) 40 ){ 41 cout << "YES" << endl; 42 return true; 43 } 44 cout << "NO" << endl; 45 return false; 46 } 47 }; 48 49 int main() 50 { 51 double x1,y1,x2,y2,x3,y3; 52 cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; 53 Point p1(x1,y1),p2(x2,y2),p3(x3,y3); 54 Triangle t(p1,p2,p3); 55 return 0; 56 }
2、薪酬
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 const int N = 128; 6 class Employee{ 7 char no[N]; 8 char name[N]; 9 int salary; 10 public: 11 Employee(const char* no,const char* name,int salary): 12 salary(salary){ 13 strcpy(this->no,no); 14 strcpy(this->name,name); 15 } 16 const char* getNo()const{ 17 return no; 18 } 19 const char* getName()const{ 20 return name; 21 } 22 const int getSalary()const{ 23 return salary; 24 } 25 }; 26 class Salesman:public Employee{ 27 public: 28 Salesman(const char* no,const char* name,int salary):Employee(no,name,salary){} 29 void print(){ 30 cout << getNo() << " " << getName() << " " << getSalary() << endl; 31 } 32 }; 33 class Salesmanager:public Salesman{ 34 int com; 35 public: 36 Salesmanager(const char* no,const char* name,int salary,int com): 37 Salesman(no,name,salary+com/100),com(com){ } 38 void print(){ 39 Salesman::print(); 40 } 41 }; 42 43 int main() 44 { 45 char no[N]; 46 char name[N]; 47 int salary; 48 int com; 49 50 scanf("%s%s%d",&no,&name,&salary); 51 Salesman sman(no,name,salary); 52 scanf("%s%s%d%d",&no,&name,&salary,&com); 53 Salesmanager smanager(no,name,salary,com); 54 55 sman.print(); 56 smanager.print(); 57 return 0; 58 }
3、时间间隔
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 class Time{ 6 int hour; 7 int minute; 8 int second; 9 public: 10 Time(int hour=0, int minute=0, int second=0){ 11 this->hour = hour; 12 this->minute = minute; 13 this->second = second; 14 } 15 void setTime(int hour, int minute, int second){ 16 this->hour = hour; 17 this->minute = minute; 18 this->second = second; 19 } 20 void showTime(){ 21 printf("%02d:%02d:%02d\n",hour, minute, second); 22 } 23 int getHour()const{ 24 return hour; 25 } 26 int getMinute()const{ 27 return minute; 28 } 29 int getSecond()const{ 30 return second; 31 } 32 }; 33 34 class Interval{ 35 Time t1; 36 Time t2; 37 public: 38 Interval(const Time&t1,const Time&t2):t1(t1),t2(t2){} 39 void showInerval(){ 40 int s1 = t1.getHour()*3600+t1.getMinute()*60+t1.getSecond() ; 41 int s2 = t2.getHour()*3600+t2.getMinute()*60+t2.getSecond(); 42 43 int s = abs(s2-s1); //两个时间相差的秒数 44 45 int hour = s/3600; 46 int minute = s%3600/60; 47 int second = s%3600%60; 48 49 Time(hour,minute,second).showTime(); 50 } 51 }; 52 int main() 53 { 54 int h,m,s; 55 Time t1,t2; 56 57 scanf("%d:%d:%d",&h,&m, &s); 58 t1.setTime(h,m,s); 59 scanf("%d:%d:%d",&h,&m, &s); 60 t2.setTime(h,m,s); 61 62 Interval i(t1,t2); 63 i.showInerval(); 64 return 0; 65 }
4、面积
1 #include <iostream> 2 using namespace std; 3 4 const double PI = 3.1415926; 5 6 class Shape{ 7 public: 8 virtual double area() = 0; 9 virtual~Shape(){} 10 }; 11 12 class Circle:public Shape{ 13 double r; 14 public: 15 Circle(double r):r(r){ } 16 double area(){ 17 return PI*r*r; 18 } 19 }; 20 21 class Rectangle:public Shape{ 22 double l,w; 23 public: 24 Rectangle(double l,double w):l(l),w(w){ } 25 double area(){ 26 return l*w; 27 } 28 }; 29 30 class Trapezoid:public Shape{ 31 double l,w,h; 32 public: 33 Trapezoid(double l,double w,double h):l(l),w(w),h(h){ } 34 double area(){ 35 return (l+w)*h/2; 36 } 37 }; 38 39 class Triangle:public Shape{ 40 double s,h; 41 public: 42 Triangle(double s,double h):s(s),h(h){ } 43 double area(){ 44 return 0.5*s*h; 45 } 46 }; 47 48 void shapeArea( Shape* sp ) 49 { 50 if(sp){ 51 printf( "%.2f\n",sp->area() ); 52 delete sp; 53 } 54 } 55 56 int main() 57 { 58 double r,l,w,h; 59 60 scanf("%lf",&r); 61 shapeArea( new Circle(r)); 62 63 scanf("%lf%lf", &l,&w); 64 shapeArea( new Rectangle(l,w) ); 65 66 scanf("%lf%lf%lf", &l,&w,&h); 67 shapeArea ( new Trapezoid(l,w,h)); 68 69 scanf("%lf%lf", &l,&h); 70 shapeArea( new Triangle(l,h)); 71 72 return 0; 73 }
5、时钟
1 #include <iostream> 2 using namespace std; 3 4 class Clock{ 5 int hour; 6 int minute; 7 int second; 8 public: 9 Clock& operator++(){ 10 if(++second >= 60){ 11 second -= 60; 12 minute++; 13 } 14 if(minute >= 60){ 15 minute -= 60; 16 hour++; 17 } 18 hour %= 24; 19 return *this; 20 } 21 Clock operator++(int){ //后置++ 22 Clock c = *this; 23 if(++second >= 60){ 24 second -= 60; 25 minute++; 26 } 27 if(minute >= 60){ 28 minute -= 60; 29 hour++; 30 } 31 hour %= 24; 32 return c; 33 } 34 Clock& operator--(){ 35 if(--second <0){ 36 second += 60; 37 minute--; 38 } 39 if(minute < 0){ 40 minute += 60; 41 hour--; 42 } 43 if(hour < 0) 44 hour += 24; 45 return *this; 46 } 47 Clock operator--(int){ //后置-- 48 Clock c = *this; 49 if(--second <0){ 50 second += 60; 51 minute--; 52 } 53 if(minute < 0){ 54 minute += 60; 55 hour--; 56 } 57 if(hour < 0) 58 hour += 24; 59 return c; 60 } 61 void SetTime(int hour, int minute, int second){ 62 this->hour = hour; 63 this->minute = minute; 64 this->second = second; 65 } 66 void ShowTime(){ 67 printf("%02d:%02d:%02d\n",hour, minute, second); 68 } 69 }; 70 71 int main() 72 { 73 int n; 74 int h,m,s; 75 scanf("%d:%d:%d",&h,&m, &s); 76 77 Clock t1,t2; 78 t1.SetTime(h,m,s); 79 80 scanf("%d",&n); 81 switch(n){ 82 case 1: t2=t1++; t1.ShowTime(); t2.ShowTime();break; 83 case 2: t2=t1--; t1.ShowTime(); t2.ShowTime();break; 84 case 3: t2=++t1; t1.ShowTime(); t2.ShowTime();break; 85 case 4: t2=--t1; t1.ShowTime(); t2.ShowTime();break; 86 } 87 return 0; 88 }