标题:[原创]引导C++初学者走进ACM
只看楼主
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
得分:0 
This introduction to acm is very good.

+1

To apib2007 --- I tried to "sticky" this post but did not get it. Could you do that?

[此贴子已经被作者于2007-7-20 16:19:02编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-20 15:47
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
得分:0 
four consecutive integers problem

target int is x

suppose you have an integer a such that

a + (a+1)+(a+2)+(a+3) == x

then

4*a + 6 == x


or

4*a == x-6

Therefore, a target x is the sum of four consecutive ints if and only if

(x-6) % 4 == 0

You can even ((x-6)&3) == 0


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-20 16:45
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
得分:0 
回复:(HJin)four consecutive integers problemtar...

umm, smart and detailed analysis ... ,GO ON.

Time Limit:3s Memory Limit:8192K In/Out:Stdin/stdout


There is chessboard which has N * M squares size of 1 * 1 and small squares can build up to a large one. Can you tell Mr. Guo that how many squares in the chessboard?

For Example, when N = 2 and M = 3, there are 6 squares size of 1 * 1 and 2 squares size of 2 * 2 in the chessboard, so the answer is 8.
Input
There are several lines in the input, each line consist of two positive integers N and M (1 <= N <= 100, 1 <= M <= 100) except the last line which N = 0 and M = 0 implying the end of the input and you should not process it.
Output
Please output the number of the squares for each chessboard in a single line.

Sample Input
1 1
2 3
3 3
0 0

Sample Output
1
8
14

程序代码:

#include <iostream>
using namespace std;
#define max(x, y) ((x)>(y)?(x):(y))
int num(int, int);

int main()
{
int N, M, ar[50];
int i=0;
while( cin>>N>>M )
{
if( N<0|| N>100 || M<0 || M>100 ) exit(-1);
ar[i]=N;
ar[i+1]=M;
if(ar[i]==0 && ar[i+1]==0 ) break;
i+=2;
}
for(int j=0; ar[j]!=0 && ar[j+1]!=0; j+=2, cout<<endl)
cout<<num(ar[j], ar[j+1]);

return 0;

}

int num(int n, int m)
{

int sum = 0;
for(int i=2; i<=max(n, m); ++i)
if( n >= i && m >= i)
{
if( n*m ==i*i ) sum += 1;
else
sum += ( m-i+1 )*( n-i+1 );
}
return sum+n*m;
}



Code:416B Time(s):0.00 Mem(k):812/2316


2.3.2.2.2.0
2007-07-20 20:23
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
得分:0 
帮你排了下菱形,没关系吧?

女侠,约吗?
2007-07-21 00:24
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1026
专家分:177
注 册:2007-5-10
得分:0 
回复:(Jackzdong)就是不知道为什么别人的CODE用的内...
不要过多的关注开销,题目已经有限制,一般要求动态规划的,搜索绝对过不了,所以只要过了就说明算法复杂度要求达到了,最多差个常数而已。
你可能花1个小时让你的代码少用了0.0几秒,感觉还不如再做一道题好
2007-07-21 01:08
ichigo
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2007-5-27
得分:0 
多谢LZ了
2007-07-21 09:29
一番宝瓶
Rank: 1
等 级:新手上路
帖 子:239
专家分:0
注 册:2007-7-14
得分:0 
Time Limit:3s Memory Limit:16384K In/Out:Stdin/stdout


A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.

f(1)=1, f(2)=1, f(n>2)=f(n-1)+f(n-2)
Your task is to take a number as input, and print that fibonacci number.

Sample Input

100

Sample Output

354224848179261915075

Note:
No generated fibonacci number in excess of 1000 digits will be in the test data, i.e. f(20)=6765 has 4 digits.

#include <iostream>
#include <string>
#include <cassert>
#include <algorithm>
using namespace std;

string LongIntSum(string, string);
int main()
{
int n;
cin>>n;
string *str =new string[n];
for(int i=0; i<n; ++i)
( i < 2 ) ? str[i] = '1' : str[i] = LongIntSum( str[i-2], str[i-1] );
assert(str[n-1].size() < 1000);
cout<<str[n-1]<<endl;
delete []str;
return 0;
}
string LongIntSum(string s1, string s2)
{
register int i, j;
int sum, c=0;
string s = "";
if( s1.size() > s2.size() ) swap(s1, s2);
for(i = s1.size()-1, j = s2.size()-1; i >=0; --i, --j)
{
sum = s1[i] - '0' + s2[j] - '0' + c;
if( sum >= 10 ) {sum -= 10; c = 1;}
else c = 0;
s += sum + '0';
}
for(i =s2.size()-s1.size()-1; i >=0; --i)
{
sum = s2[i] - '0' + c;
if( sum >= 10 ) {sum -= 10; c = 1;}
else c = 0;
s += sum + '0';
}
if( c ) s+= c + '0';
reverse(s.begin(), s.end());
return s;
}


Code:959B Time(s):0.39 Mem(k):832/1044

[此贴子已经被作者于2007-7-21 15:41:13编辑过]


2.3.2.2.2.0
2007-07-21 15:34
etherli
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-9-14
得分:0 
2007-09-14 21:18
爱上鱼的水
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-9-14
得分:0 

怎样在那些做题目的网站里看别人的答案啊?

2007-09-15 10:49
大口木瓜
Rank: 1
等 级:新手上路
帖 子:111
专家分:0
注 册:2007-5-25
得分:0 
路过,留个脚印,下次来访,现在水平个跟不上哦
2007-12-08 19:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-156807-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.336713 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved